WooCommerce Actions and Filters to Manipulate the Cart

Originally published at: http://www.sitepoint.com/woocommerce-actions-and-filters-manipulate-cart/

Welcome to the second article in the series on Mastering WooCommerce Actions and Filters. In the previous article, even though it was very basic, we covered handling a customer’s billing and shipping address as well as what happens when a customer registers through WooCommerce and it sends them to a third party website, like Salesforce.

In this second article, we will manipulate the cart in some clever ways with real world scenarios you may encounter while creating your eCommerce website using WooCommerce.

Adding a Product to the Cart Programatically

Adding a product to the cart programatically only takes one line of code. The only brainstorming you’ll be doing is deciding when or why you’ll want to do it. We’ll talk more about this later in the third part of this article, when we work on our real world scenario.

All it takes to add a product to the cart is the following:

Continue reading this article on SitePoint

Hi there,
I want to say thank you so much for posting this! I feel I’ve been looking everywhere for this explanation as I am trying to add a specific product to a cart if the cart contains items from a certain category.
I have tried pasting the code into my theme’s function.php file, but with no luck so far. I think I am missing some fundamental step…like how to add the parameters for the category required and the product id that will be added to the cart.
I am trying to check the cart for any items from category ID= 21, then if any items in the cart are from that category, add product ID= 506

I think I am missing something here, because I plut the code into the theme-functions file, then when I try to test, I can’t actually see what’s in my cart on the cart or checkout page anymore. The last line of the source code for the page says <div="woocommerce"> then it’s blank with no content or footer or anything.

Any ideas where I am going wrong? I really need to get this working asap, and this is the closest thing I have found that may (hopefully) work!

Thank you so, so much for your time!!

Danielle

Just to be clear, this is the code as it appears in my theme functions file:

    /**
 * Checks the cart to verify whether or not a product from a Category is in the cart
 * @param $category Accepts the Product Category Name, ID, Slug or array of them
 * @return bool
 */
function qualifies_basedon_product_category( $category ) {
    foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
        if( has_term( $category, 'fee', get_id_from_product( $product_in_cart, false ) ) ) {
            return true;
        }
    }
    // Return false in case anything fails
    return false;
}
 
/**
 * Adds a specific product to the cart
 * @param $product_id Product to be added to the cart
 */
function add_incentive_to_cart( $product_id ) {
    // Check the cart for this product
    $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 );
    }
}
 


add_action( 'woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() {
    // Incentive product we are giving away
    $incentive_product_id = 506;
 
    if( qualifies_basedon_product_category( 'fee' ) ) {
        add_incentive_to_cart( $incentive_product_id );
    } 
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.