0

I'm using the following code to add a free product to the basket when over a certain cart total.

When I drop below the cart total it hits the if statement but won't remove the product.

I think this may be because the quantity is set to 1 on the basket form and my code isn't overriding that quantity to set it to 0 (and remove it).

Is there another way to remove it or override it?

/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/

function aapc_add_product_to_cart() {
    global $woocommerce;

    $cart_total = 20;
  $free_product_id = 85028;  // Product Id of the free product which will get added to cart

    if ( $woocommerce->cart->total >= $cart_total ) {

    echo "Over the limit";
    $quantity = 1;
    WC()->cart->add_to_cart( $free_product_id, $quantity );

    } elseif($woocommerce->cart->total < $cart_total) {

    echo "Under the limit";
    $quantity = 0;    
    WC()->cart->remove_cart_item( $free_product_id, $quantity );

  }
}

add_action( 'template_redirect', 'aapc_add_product_to_cart' );

I've tried using this question but cannot get it to work, it won't even add the item to my basket.

I'm using Woocommerce version 3.6.5 if that helps.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rob
  • 6,304
  • 24
  • 83
  • 189
  • It seems, in your case, that there is something that is making trouble as the [linked answer thread](https://stackoverflow.com/a/39831885/3730754) still works perfectly on last Woocommerce version. It can be another customization made by you, your theme or a plugin. The `template_redirect` hook will not work on Ajax mini-cart actions (remove) or Ajax cart page actions (change quantity, remove), as it work on page load. The hooks `woocommerce_before_calculate_totals` or `woocommerce_calculate_totals` are convenient, because they are Ajax enabled. – LoicTheAztec Sep 12 '19 at 09:43
  • @LoicTheAztec Just got it working for the basket page, not sure why the linked answers don't work but this does (see my answer). Could I potentially have the linked answer to cover the ajax parts... so my fix for the basket and your answer for the ajax parts? – Rob Sep 12 '19 at 09:59
  • When using `woocommerce_before_calculate_totals` hook you can't use `cart->total` (as it doesn't display anything). Instead you need to use `cart->subtotal`… That is the main problem I think. – LoicTheAztec Sep 12 '19 at 10:28

2 Answers2

0

I'm not sure on how Woocommerce works but I assume the remove_cart_item 'quantity' parameter takes the amount of the item to remove from the basket. So set the value of $quantity to 1 and it should remove one of the free product from the basket. If there happens to be more than one free item in the basket then you'll need to set your $quantity value accordingly.

Furthermore, you are adding a free item every time a product is added when the basket value is higher than $cart_total. Maybe add a check to see if it already exists in the basket before adding it?

Hope I understood your question correctly

Josh Wood
  • 461
  • 3
  • 14
  • That's what I initially thought but it doesn't remove it so I thought I could specifically set the quantity of an item (i.e set it to zero). That's true with always adding a free item over 20, I'll sort that. Just need a way around the first part. – Rob Sep 12 '19 at 09:31
  • @Rob try and see if any answers on the following post can be of any use to you? https://stackoverflow.com/questions/30569881/remove-item-with-product-id-woocommerce – Josh Wood Sep 12 '19 at 09:33
  • Documentation for the method: https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html#_remove_cart_item – Josh Wood Sep 12 '19 at 09:34
0
/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/
function aapc_add_product_to_cart() {
    global $woocommerce;

    $cart_total = 20;
  $free_product_id = 85028;  // Product Id of the free product which will get added to cart

    if ( WC()->cart->total >= $cart_total ) {

    $found      = false;
    //check if product already in cart
    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->get_id() == $free_product_id )
              $found = true;
        }
        // if product not found, add it
        if ( ! $found )
            WC()->cart->add_to_cart( $free_product_id );
    } else {
        // if no products in cart, add it
        WC()->cart->add_to_cart( $free_product_id );
    }

    } elseif(WC()->cart->total < $cart_total) {

    $quantity = 0;
    $prod_unique_id = WC()->cart->generate_cart_id( $free_product_id );
    // Remove it from the cart by un-setting it
    unset( WC()->cart->cart_contents[$prod_unique_id] );
    WC()->cart->remove_cart_item( $free_product_id );

  }
}
add_action( 'woocommerce_after_calculate_totals', 'aapc_add_product_to_cart' );
Rob
  • 6,304
  • 24
  • 83
  • 189
  • Your answer code is based partially on [this answer](https://stackoverflow.com/questions/49562815/auto-add-a-product-to-cart-in-woocommerce-3/49563363#49563363) *(and you could upvote it as its useful)*… It will not handle Ajax cart actions (update quantity and remove item) and you sill need to reload after making manipulations on cart items. Also you should remove old outdated `global $woocommerce;` and replace `$woocommerce->cart` by `WC()->cart` everywhere. – LoicTheAztec Sep 12 '19 at 10:29
  • @LoicTheAztec Ok I've amended my answer, it only works with for both the bag and the checkout (ajax) using `woocommerce_after_calculate_totals`. On the checkout, it works if I go over the threshold but when I go under it, it's not reloading the ajax basket??? – Rob Sep 12 '19 at 10:57