Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. function bonus_product() {
  2. if (is_admin()) return;
  3.  
  4. //## OPTIONS
  5. $options = (object) array(
  6. 'bonus_product_id' => 1891, //bonus product to add
  7. 'required_products_id' => array(1873), //at least on of the specific product(s) needs to be represented in the cart
  8. );
  9.  
  10. //function variables
  11. $cart_items = WC()->cart->get_cart();
  12. $bonus_product_found = false;
  13. $required_product_found = false;
  14.  
  15. //check if the cart is not empty
  16. if(sizeof($cart_items) > 0) {
  17. //checking for required products. loop through the cart items
  18. foreach ($cart_items as $key => $item) {
  19. //bonus product already in the cart?
  20. if($item['product_id'] == $options->bonus_product_id) {
  21. $bonus_product_found = true;
  22. }
  23. //one of required products in the cart?
  24. if(in_array($item['product_id'], $options->required_products_id)) {
  25. $required_product_found = true;
  26. }
  27. }
  28.  
  29. //adding/removing bonus product
  30. //add bonus product to the cart
  31. if(!$bonus_product_found && $required_product_found) {
  32. WC()->cart->add_to_cart($options->bonus_product_id);
  33. }
  34. //remove bonus product from the cart if none of required items is in the cart
  35. if($bonus_product_found && !$required_product_found) {
  36. $cart = WC()->instance()->cart;
  37. $cart_id = $cart->generate_cart_id($options->bonus_product_id);
  38. $cart_item_id = $cart->find_product_in_cart($cart_id);
  39. $cart->set_quantity($cart_item_id, 0);
  40. }
  41. }
  42. }
  43.  
  44. add_action( 'init', 'bonus_product' );
  45.  
  46. $bonus_options = (object) array(
  47. 'bonus_product_id' => 1891,
  48. 'required_products_id' => array( 1873 )
  49. );
  50.  
  51. // this function called whenever there is a product added to cart
  52. function add_bonus_product( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
  53. global $bonus_options;
  54. // is the product is eligible for bonus product?
  55. if( in_array( $product_id, $bonus_options->required_products_id) ) {
  56. // add the bonus product to cart
  57. WC()->cart->add_to_cart( $bonus_options->bonus_product_id, 1, 0, array(), array( "parent_product_line_item" => $cart_item_key ) );
  58. // later if user removes the product from cart we can use the "parent_product_line_item" to remove the bonus product as well
  59. }
  60. }
  61. add_action( 'woocommerce_add_to_cart', 'add_bonus_product', 10, 6 );
  62.  
  63. // this function will be called whenever there is a product removed from cart
  64. function remove_bonus_product( $cart_item_key, $cart ) {
  65. $cart_items = WC()->cart->get_cart();
  66. foreach ( $cart_items as $key => $item ) {
  67. if( $item["parent_product_line_item"] == $cart_item_key ) {
  68. // ok this cart item is a bonus item to the product that being removed from the cart
  69. // So remove this too
  70. WC()->cart->remove_cart_item( $key );
  71. }
  72. }
  73. }
  74.  
  75. add_action( 'woocommerce_cart_item_removed', 'remove_bonus_product', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement