我有一个复选框,单击时应将产品添加到购物车,而未单击时应将其删除。我之前的开发人员使用费解决了这个问题,费应该改为产品。他用过
add_action( \'woocommerce_cart_calculate_fees\', \'checkout_radio_choice_product\', 20, 1 );
我想我得到了代码,我只是在努力寻找正确的钩子。woocommerce\\u add\\u to\\u cart不是正确的。有人能给我指出正确的方向吗?
function checkout_radio_choice_product()
{
if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
return;
$mp = WC()->session->get( \'magic_product\' ); //true or false
if( $mp) {
$product_id = 11446;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[\'data\'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
else {
WC()->cart->remove_cart_item( $product_id);
}
}