I have checkbox that when clicked should add a product to the cart ans when unclicked should remove it.
Developer before me solved the problem using Fee’s which should be changed to product. He used
add_action( 'woocommerce_cart_calculate_fees', 'checkout_radio_choice_product', 20, 1 );
I think I got the code to work I am just struggling to find the right hook. woocommerce_add_to_cart is not the right one. Can anyone point me in the right direction?
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);
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Got it to work using woocommerce_before_calculate_totals hook
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0