Blog

Woocommerce Dynamically add/remove products from cart by using hooks and fucntions

Ever needed to dynamically add/remove products to a visitor’s cart based on their cart contents?
The below functions will help you do just that. Place them in the functions.php of your theme or (better) in a separate plugin and you are all set. Just make sure to modify the qualifies_for_incentive function to work based on your rules.


/**
 * Gets Variation ID if available otherwise it will get the Product ID
 * @param $product Product
 * @param bool $check_variations Whether or not to check for variation IDs
 * @return mixed
 */
function get_id_from_product( $product, $check_variations = false ) {
    if( $check_variations ) {
        return ( isset( $product['variation_id'] )
            && ! empty( $product['variation_id'])
            && $product['variation_id'] != 0 )
            ? $product['variation_id']
            : $product['product_id'];
    } else {
        // No variations, just need the product IDs
        return $product['product_id'];
    }
}

/**
 * Checks the existence of a specific product in the cart
 * @param $product_required The Product ID required to be in the cart
 * @return bool
 */
function qualifies_basedon_specific_product( $product_required ) {
    if( is_cart() || is_checkout () ) {
        foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
            if( $product_required == get_id_from_product( $product_in_cart ) ) {
                return true;
            }
        }
        return false;
    }
}

/**
 * Checks for quantity of products in cart
 * @param $product
 * @param $quantity
 * @return bool
 */
function get_product_quantity( $product, $quantity ) {

  if( is_cart() || is_checkout() ) {
    if( qualifies_basedon_specific_product( $product ) ) {
      foreach( WC()->cart->cart_contents as $item => $values ) {
        if( $product == $values['product_id'] && $quantity == $values['quantity'] ) {
          return true;
        }
      }
    }
    return false;
  }
}

/**
 * Checks the cart for the Total excluding taxes
 * @param $total_required
 * @return bool
 */
function qualifies_basedon_cart_total( $total_required ) {
    if( is_cart() || is_checkout () ) {
        if( WC()->cart->subtotal_ex_tax >= $total_required ) {
            return true;
        }
    }
    return false;
}

/**
 * Adds a specific product to the cart. Accepts a quantity and price override
 * @param $product_id Product to be added to the cart
 * @param $quantity Add X of counts of $product_id
 * @param $price Set price of $product_id before adding
 * @return bool
 */
function add_product_to_cart( $product_id, $quantity, $price ) {
  $cart_id = WC()->cart->generate_cart_id( $product_id );
  $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
  // Add the product only if it's not in the cart already
  if( ! $prod_in_cart ) {
    WC()->cart->add_to_cart( $product_id, $quantity );
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      if( $cart_item['data']->id == $product_id ){
          $cart_item['data']->set_price( $price );
      }
  	}
  }
}

/**
 * Removes a specific product from the cart
 * @param $product_id Product ID to be removed from the cart
 */
function remove_product_from_cart( $product_id ) {
     $prod_unique_id = WC()->cart->generate_cart_id( $product_id );
    // Remove it from the cart by un-setting it
    unset( WC()->cart->cart_contents[$prod_unique_id] );
}

add_action( 'woocommerce_check_cart_items', 'qualifies_for_incentive' );
add_action( 'woocommerce_before_cart', 'qualifies_for_incentive');
function qualifies_for_incentive() {
    $incentive_product_id = 652;
    if( qualifies_basedon_specific_product( 79 ) && qualifies_basedon_cart_total( 895 ) ) {
    		if( get_product_quantity( 79, 3 ) ) {
    			add_product_to_cart( $incentive_product_id, 3, 1 );
    		}
    } else {
        remove_product_from_cart( $incentive_product_id );
    }
}

Leave a Reply