- WooCommerce Actions and Filters to Manipulate the Cart
- Adding a Product to the Cart Programatically
- Removing a Product from the Cart Programatically
- Emptying the Cart Programatically
- Incentive Products
- The Problem
- The Solution
- Only One Requirement
- Which Hook is running after Woocommerce update cart button action
WooCommerce Actions and Filters to Manipulate the 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:
As a note of caution, make sure you don’t run this on an action that runs on every page, like the template_redirect action or you’ll be adding one of these products to the cart every page load or you reloads. Avoid doing this whenever possible:
Removing a Product from the Cart Programatically
I’ve seen this question being asked an infinite number of times in various forums and websites with very little answers. Hopefully this will help you the next time you want to remove a product from the cart and again, the only brainstorming you’ll be doing is when or why you would want to remove a product from the cart. The following code will prevent anyone from checking out with a product from your store. I don’t know why you would want to do something like that but it will demonstrate the steps for removing the product from the cart which is not as simple as the previous example when we added the product to the cart.
cart->cart_contents as $prod_in_cart ) < // Get the Variation or Product ID $prod_id = ( isset( $prod_in_cart['variation_id'] ) && $prod_in_cart['variation_id'] != 0 ) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id']; // Check to see if IDs match if( $prod_to_remove == $prod_id ) < // Get it's unique ID within the Cart $prod_unique_id = WC()->cart->generate_cart_id( $prod_id ); // Remove it from the cart by un-setting it unset( WC()->cart->cart_contents[$prod_unique_id] ); > > > >
Emptying the Cart Programatically
To better illustrate how to empty the cart programatically, let’s add a button to the cart which would allow customers to click on it and clear their cart.
Let’s use the woocommerce_proceed_to_checkout action an echo our very own ‘Submit’ button which will clear the cart for the current customer.
The next step is to listen for the button to be clicked so that when it is clicked, we clear the cart. For that, we are going to hook into the template_redirect action.
You’ll notice now that after pressing the button, the cart-empty.php is displayed instead of the regular template.
Now that we’ve established how to add or remove a product from the cart, even emptying the cart completely, let’s move on to building our real world scenario where knowing this kind of stuff makes a big difference.
Incentive Products
In our real world scenario, we’re going to put all of this to work by building a system where you could give away a product as an incentive to all of your customers. Well, not exactly to all of your customers, just those who qualify based on a specific requirement.
The Problem
We need to be able to give out a product of your choice as an incentive to your customers.
The Solution
Build a system which will allow you to give away your incentive product based on the following:
- Having a specific product in the cart
- Having a minimum total amount for your order
- Having a minimum weight in your cart
- Having a product from a specific category
Because we are going to be building this the right way, not only will you be able to give away the product for customer qualifying to one of these criteria, but you’ll also be able to mix these up and really narrow down who gets the product and who doesn’t.
Not only will you be able to offer your customers the incentive product by qualifying to one of those criteria, you’ll have the power to combine them. In order words, for example, you’ll be able to test for someone having at least $100 total in their cart and a product from the ‘Clothing’ category.
Let’s take a quick look at the functions we’ll be writing in a minute and what each does in our problem/solution scenario.
- get_id_from_product( $product, $check_variations = true ) – Gets the product ID and returns it. Takes variation IDs into account so we check for these before checking for the actual Product ID.
- qualifies_basedon_specific_product( $product_required ) – Checks whether or not a customer qualifies for the incentive by having the specified product ID as one of the items in the cart.
- qualifies_basedon_weight( $weight_required ) – Checks whether or not a customer qualifies for the incentive by having a minimum weight in the cart.
- qualifies_basedon_cart_total( $total_required ) – Checks whether or not the customer qualifies for the incentive by having a minimum total amount before taxes are calculated.
- qualifies_basedon_product_category( $category ) – Checks whether or not the customer qualifies for the incentive by having a product from a certain category in the cart.
- add_incentive_to_cart( $product_id ) – Adds the incentive product to the cart if the customer qualified for it
- remove_incentive_from_cart( $product_id ) – Removes the incentive product to the cart if the customer failed to qualify for the product.
- qualifies_for_incentive() – This is where the magic will happen because it will have the rules that need to be matched in order for the customer to qualify for the incentive. This function will handle the logic for our incentive program.
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 ) < /* * We only want to run this on the cart or checkout page */ 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 in case anything fails return false; > > /** * Checks the cart for the weight required to qualify for the incentive * @param $weight_required Weight Required * @return bool */ function qualifies_basedon_weight( $weight_required ) < /* * We only want to run this on the cart or checkout page */ if( is_cart() || is_checkout () ) < if( $weight_required >= WC()->cart->cart_contents_weight ) < return true; >> // Return false in case anything fails return false; > /** * Checks the cart for the Total excluding taxes * @param $total_required * @return bool */ function qualifies_basedon_cart_total( $total_required ) < /* * We only want to run this on the cart or checkout page */ if( is_cart() || is_checkout () ) < if( WC()->cart->subtotal_ex_tax >= $total_required ) < return true; >> // Return false in case anything fails return false; > /** * 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, 'product_cat', 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 ); > > /** * Removes a specific product from the cart * @param $product_id Product ID to be removed from the cart */ function remove_incentive_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] ); >
As you can see, these functions return ‘True’ or ‘False’ so it’s going to make it really easy for us to mix it up and create an incentive program that is really flexible. What’s left to do now is come up with the rules you want to set for your customers to qualify for the incentive product and write the qualifies_for_incentive() function which will be tied to the woocommerce_check_cart_items WooCommerce action.
Below are some examples of how you can use these functions to create something really unique.
Only One Requirement
Here are a few examples setting only one requirement.
Which Hook is running after Woocommerce update cart button action
i need to know which hook is running after clicking the update cart button in the cart page . That is in cart page we have 4 button , update cart , continue shopping , proceed to checkout , apply coupon . So i want to know which hook is run after update cart button is clicked . When customer click the update cart button after changing the quantity then i have to run a special function that can change total price in the cart , If some condition met , i will change the total price in the cart , and this total price need to pass to the checkout page also / Please help . For example
add_filter('after_update_cart_function_finished', 'special_function'); function special_function()< $applied_coupons= WC()->cart->get_applied_coupons(); if(!empty($applied_coupons))< $new_value=WC()->cart->get_cart_subtotal(); $discounted=WC()->cart->coupon_discount_totals; $discounted_value=array_values($discounted)[0]; $new_value=$new_value-$discounted_value+100; WC()->cart->set_total_price($new_value); After this update all post_meta value that regarding to order total > >
function action_woocommerce_cart_totals_after_order_total( ) < $applied_coupons= WC()->cart->get_applied_coupons(); if(!empty($applied_coupons))< $new_value=WC()->cart->get_cart_subtotal(); $discounted=WC()->cart->coupon_discount_totals; $discounted_value=array_values($discounted)[0]; $new_value=$new_value-$discounted_value; if($new_value <100)< $new_value=$new_value+5; >?> .new-price-new else < ?> >; add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_cart_totals_after_order_total', 10, 0 );
And this function have many problems , i write this function because of some reason or some other function woocommerce is not calculating coupon price correctly , so i write this function for to manually update the product price in cart .Here if the cart value is more than 100 we provide free shipping other vise we will add 5 . Even this function is not working properly . Woocommerce Create new discount functionality