Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Last active January 7, 2020 18:00
Show Gist options
  • Save BFTrick/7137572 to your computer and use it in GitHub Desktop.
Save BFTrick/7137572 to your computer and use it in GitHub Desktop.
If you want to use WooCommerce Force Sells to add only one of the force sold products to the cart.
// only add one force sell product to the cart no matter how many of the original product are added
function my_wc_force_sell_add_to_cart_product( $product ){
$product['quantity'] = 1;
return $product;
}
add_filter( 'wc_force_sell_add_to_cart_product', 'my_wc_force_sell_add_to_cart_product' );
// when a synced force sell product is updated always set it to 1
function my_wc_force_sell_update_quantity( $quantity, $product ){
return 1;
}
add_filter( 'wc_force_sell_update_quantity', 'my_wc_force_sell_update_quantity' );
@PeterSikora
Copy link

Hi,
I detected a problem. When I add this code to the functions.php I see the following thing:
If I delete the main product in the cart the "forced" product still remain and there is no chance to delete it because there is no delete button for the forced prodoct. Only for the main prododuct there is a delete button.
Do you have an idea how to fix it?

Many thanks!
Regards,
Peter

@schricka
Copy link

schricka commented Jan 3, 2015

// when a synced force sell product is updated set it to 1
// if products is removed set $quantity to 0
function my_wc_force_sell_update_quantity( $quantity){
return ($quantity==0) ? 0:1;
}

@DanielleDarling
Copy link

Any ideas on how to allow only one of the force sold items into the entire cart....not just per product? So if I have the same item as a force sell for several products, and I only want one of the force sell items to be added to the cart in its entirety, not one for each different product it's attached to...

@expertdeveloperit
Copy link

I have single product( type of service) and 8-10 adson(type of products). I want to force single product (service) when user add adson to cart.
The issue is I want only single service product into cart but it added multiple times when user add different adson to cart.
The above code is not working, Can you please let me know how I can fix it ?
Thanks in advance.
Expert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment