使过滤器产品特定



>我有一个 wooCommerce 安装,并试图强制购物车返回固定值,无论数量如何。我终于找到了合适的过滤器来做到这一点。

add_filter('woocommerce_cart_subtotal','subtotalchanger',0);
function subtotalchanger($subtotal){
$my_fixed_subtotal = 1.65;
return $my_fixed_subtotal;
}

但是,当尝试针对相关产品时,过滤器将不起作用。

add_filter('woocommerce_cart_subtotal','subtotalchanger',0);
function subtotalchanger($subtotal){
if (is_single('15455')){
$my_fixed_subtotal = 1.65;
return $my_fixed_subtotal;
}
}

我在这里做错了什么?

你是这个意思吗?

function subtotalchanger($subtotal, $compound, $cart ) {
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item ) {        
$product = wc_get_product( $cart_item['product_id'] );
// Get product id
$product_id = $product->get_id();
if ( $product_id == 15455 ) {
$subtotal = 1.65;
}
}
return $subtotal;
}
add_filter( 'woocommerce_cart_subtotal', 'subtotalchanger', 10, 3 ); 

最新更新