当自定义字段的组合值达到指定值时,是否有技术方法可禁用woocommerce中的“添加到购物车”按钮 .
我使用下面的代码来计算自定义字段_item_volume的总值,并希望在值达到68时禁用“添加到购物车”按钮功能 .
add_action('woocommerce_before_calculate_totals', 'display_custom_notice', 50, 1);
function display_custom_notice( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'],
'_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 68 && $total_volume != 0 ){
// Display a custom notice
wc_add_notice( __("Note: Your order total volume has reached 68 m3",
"woocommerce"), 'notice' );
}
}
1 回答
请尝试以下方法:
代码位于活动子主题(或活动主题)的function.php文件中 . 经过测试和工作 .