如何在wooccommerce中从php代码中排除产品类别



我使用这段代码来显示从欧元到起始欧元的可变产品的价格范围。我取最大价格除以10,因为我的最大变化是一包10。

add_filter( 'eha_variable_sale_price_html',
'pugliami_variation_price_format_min', 9999, 4 );
function pugliami_variation_price_format_min( $price, $min_price, $maxprices, $regular_price ) {
$maxprices = sprintf( __( 'Starting from %1$s', 'woocommerce' ),
wc_price( $maxprices /10) );
return $maxprices ;
}

但这个代码的问题是,如果我有一个带容量的可变产品,而不是包,这将取容量的最高价格,除以10,我不想要它,我只想要带包的可变产品。

带包装的产品:https://poppersplanet.com/produit/rush-original-10ml/?lang=en

无包装产品:https://poppersplanet.com/produit/lubricant-gel-x-man-silicone/?lang=en

有没有一种方法可以删除php代码的特殊产品,或者只对产品类别使用此代码。

提前Thx寻求任何帮助:(

看起来您正在使用ELEX WooCommerce动态定价和折扣插件或类似插件,因为eha_variable_sale_price_html不是标准的WooCommCommerce挂钩名称。

在任何情况下,您都应该能够访问全局$product对象,您可以查询该对象以获取其id或产品类别。在此基础上,你可以建立你的支票,以确定你是否需要将最高价格除以10。

例如,基于产品类别的标准如下所示(您需要根据您的情况调整Quantity Product类别名称(:

add_filter( 'eha_variable_sale_price_html', 'pugliami_variation_price_format_min', 9999, 4 );
function pugliami_variation_price_format_min( $price, $min_price, $maxprices, $regular_price ) {
global $product;
$categories = get_the_terms( $product->get_id(), 'product_cat' );
$is_quantity_product = false;
foreach ( $categories as $category ) {
if ( 'Quantity Product' === $category->name ) {
$is_quantity_product = true;
break;
}
}
if ( $is_quantity_product ) {
return sprintf( __( 'Starting from %1$s', 'woocommerce' ), wc_price( $maxprices / 10 ) );
}
return sprintf( __( 'Starting from %1$s', 'woocommerce' ), wc_price( $min_price ) );
}

类似地,您可以检查产品的id是否包含在价格基于数量的产品的id数组中。

最新更新