I was trying to modify the WooCommerce Is_Purchasable option so that, item B is purchasable if item A is added to the cart.
I managed to disable add-to-cart button for item B with the code below. But when item A is added to the cart, the page won’t load.
Here’s the code:
function wc_product_is_in_the_cart( $ids ) {
$cart_ids = array();
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values['data'];
$cart_ids[] = $cart_product->id;
}
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
return true;
} else {
return false;
}
}
function wc_product_is_purchasable ( $is_purchasable, $product ) {
$product_ids = array( '249' );
if ( ! wc_product_is_in_the_cart( $product_ids ) ) {
return ($product->id == 2983 ? false : $is_purchasable);
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'wc_product_is_purchasable', 10, 2 );
I’ve tried a bunch of ways, but nothing seems working. How should I go on with this?
Try this snippet.