Samrat Samrat - 5 months ago 38
PHP Question

WooCommerce: Custom function adding free product - Cart page getting broken for orders up to 3000

WooCommerce Cart Page breaks as soon as I add a product to cart If the value of the cart goes above 3000.

Below is the code I am using in

functions.php
of my theme:

add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );
// Cart's Total Excluding Taxes
function qualifies_for_incentive() {
// Incentive product we are giving away
$incentive_product_id = 6971;
if( qualifies_basedon_cart_total( 250 ) ) {
add_incentive_to_cart( $incentive_product_id );
} else {
remove_incentive_from_cart( $incentive_product_id );
}

if( qualifies_basedon_cart_total( 500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 1750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 2750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 3750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 4750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 5750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 6750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 7500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 8750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9250 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9500 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 9750 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 10000 ) ) {
add_incentive_to_cart( $incentive_product_id );
}
}

/**
* 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( qualifies_basedon_cart_total( 250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,1 );
} else {
remove_incentive_from_cart( $incentive_product_id );
}
if( qualifies_basedon_cart_total( 500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,2 );
}
if( qualifies_basedon_cart_total( 750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,3 );
}
if( qualifies_basedon_cart_total( 1000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,4 );
}
if( qualifies_basedon_cart_total( 1250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,5 );
}
if( qualifies_basedon_cart_total( 1500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,6 );
}
if( qualifies_basedon_cart_total( 1750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,7 );
}
if( qualifies_basedon_cart_total( 2000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,8 );
}
if( qualifies_basedon_cart_total( 2250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,9 );
}
if( qualifies_basedon_cart_total( 2500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,10 );
}
if( qualifies_basedon_cart_total( 2750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,11 );
}
if( qualifies_basedon_cart_total( 3000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id, 12);
}
if( qualifies_basedon_cart_total( 3250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,13 );
}
if( qualifies_basedon_cart_total( 3500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,14 );
}
if( qualifies_basedon_cart_total( 3750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,15 );
}
if( qualifies_basedon_cart_total( 4000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,16 );
}
if( qualifies_basedon_cart_total( 4250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,17 );
}
if( qualifies_basedon_cart_total( 4500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,18 );
}
if( qualifies_basedon_cart_total( 4750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,19 );
}
if( qualifies_basedon_cart_total( 5000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,20 );
}
if( qualifies_basedon_cart_total( 5250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,21 );
}
if( qualifies_basedon_cart_total( 5500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,22 );
}
if( qualifies_basedon_cart_total( 5750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,23 );
}
if( qualifies_basedon_cart_total( 6000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,24 );
}
if( qualifies_basedon_cart_total( 6250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,25 );
}
if( qualifies_basedon_cart_total( 6500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,26 );
}
if( qualifies_basedon_cart_total( 6750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,27 );
}
if( qualifies_basedon_cart_total( 7000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,28 );
}
if( qualifies_basedon_cart_total( 7250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,29 );
}
if( qualifies_basedon_cart_total( 7500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,30 );
}
if( qualifies_basedon_cart_total( 7750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,31 );
}
if( qualifies_basedon_cart_total( 8000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,32 );
}
if( qualifies_basedon_cart_total( 8250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,33 );
}
if( qualifies_basedon_cart_total( 8500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,34 );
}
if( qualifies_basedon_cart_total( 8750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,35 );
}
if( qualifies_basedon_cart_total( 9000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,36 );
}
if( qualifies_basedon_cart_total( 9250 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,37 );
}
if( qualifies_basedon_cart_total( 9500 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,38 );
}
if( qualifies_basedon_cart_total( 9750 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,39 );
}
if( qualifies_basedon_cart_total( 10000 ) ) {
WC()->cart->remove_cart_item( $prod_in_cart );
WC()->cart->add_to_cart( $product_id,40 );
}

}

/**
* 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] );
}

/**
* 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 >= $total_required ) {
return true;
}
}
// Return false in case anything fails
//return false;
}


Website: http://qa.fastandup.in

Please I need help about this issue. What I am doing wrong?

Thanks.

Reference: WooCommerce Actions and Filters to Manipulate the Cart

Answer

SHORTENING REPETITIVE CODE with a PHP FOR loop:

This doesn't answer directly to your question, but your code is very long and redundant. With a simple you can drastically shorten the first 2 functions...

It seems that your code could come partially from this thread:
Add a product to woocommerce cart if other cart items are in a specific category


Update:

Then I have MERGED all your code in your first hooked function qualifies_for_incentive().
So the code below does exactly the same thing as all your code (20 lines without comments):

add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );

function qualifies_for_incentive() { // Cart's Total Excluding Taxes
    if( ( is_cart() || is_checkout () ) {
        $free_product = 6971; // Incentive product we are giving away
        $cart_id = WC()->cart->generate_cart_id( $free_product ); // (equivalent to $prod_unique_id)
        $free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
        $cart_amount = WC()->cart->subtotal;

        if( $cart_amount < 250 ) {
            // removing free product by unsetting it
            unset( WC()->cart->cart_contents[$cart_id] );
            exit(); 
        }
        // The shortening "FOR LOOP": 
        // $amount goes by step of 250 until 10000 while 
        // $number is increased by 1 each step until 40.
        for( $amount = 250, $number=1; $amount <= 10000, $number<=40; $amount += 250, $number++ ){
            if( ( $cart_amount >= $amount ) {
                // Removing existing "free products" from the cart.
                WC()->cart->remove_cart_item( $free_products_in_cart ); 
                // Adding to cart a $number of free products 
                WC()->cart->add_to_cart( $free_product, $number );
            }
        } 
    }
    exit();
}

This way it avoid to get some kind of lost when reviewing it.
See it visually in action here.

It's the generated code by echoing what's inside the loop with that php file:

<html><head><title>The FOR loop in action (PHP)</title></head>
  <body>
	<p><br> - - - - - - - - - - - - - - - - - The FOR loop - - - - - - - - - - - - - - - - - <br></p>
	<div>
      <code><?php
	    for( $amount = 250, $number=1; $amount <= 10000, $number<=40; $amount += 250, $number++ ){
		  echo '
		    if( $cart_amount >= $amount ) {
				WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
				WC()->cart->add_to_cart( $free_product, '.$number.' ); <br>
		    } <br>';
		}
    ?></code>
	</div>
	<p><br> - - - - - - - - - - - - - - - - - - - E N D  - - - - - - - - - - - - - - - - - - - <br></p>
  </body>
</html>

After that I am inspecting your code to understand why you have this issue...


Update2: Found what is wrong, I think

Expalanation: With your code, highest is the total cart amount, more the cart is getting through multiple repetitive removing and adding free items. So when he riches 3000, it's just break…

To avoid this problem you need to define in each if statement the "in between" amount instead of the minimal amount only.

Here is my new compact code (only 24 lines without comments):

add_action('woocommerce_check_cart_items', 'qualifies_for_incentive' );

function qualifies_for_incentive() { // Cart's Total Excluding Taxes
    if( ( is_cart() || is_checkout () ) {
        $free_product = 6971; // Incentive product we are giving away
        $cart_id = WC()->cart->generate_cart_id( $free_product ); // (equivalent to $prod_unique_id)
        $free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
        $cart_amount = WC()->cart->subtotal; // cart subtotal

        // Cart subtotal is less than 250
        if( $cart_amount < 250 ) {
            // removing free product by unsetting it
            unset( WC()->cart->cart_contents[$cart_id] );
            exit(); 
        }
        // Cart amount is up to 249. 
        // The min amount $i begins at 250 and goes by step of 250 until 9750 while
        // the max amount $j begins at 500 and goes by step of 250 until 10000 while 
        // the quantity of free products $k increases by 1 each step until it reaches 39.
        for( $i = 250, $j = 500, $k = 1; $j <= 10000; $i += 250, $j += 250, $k++ ){

            // NOW THERE WILL BE NO MULTIPLE ADDINGS AND REMOVINGS   <=========== @@@
            // Dedfined "in between" values to avoid this issue.
            // For each step of 250 amount…
            if( $cart_amount >= $i && $cart_amount < $j ) {
                // Removing existing "free products" from the cart.
                WC()->cart->remove_cart_item( $free_products_in_cart ); 
                // Adding to cart a number $k of free products 
                WC()->cart->add_to_cart( $free_product, $k ); 
            }
        }
        // For cart amount up to 10000 The quantity is fixed: 40 free products
        if( $cart_amount >= 10000 ) {
            // Removing existing "free products" from the cart.
            WC()->cart->remove_cart_item( $free_products_in_cart ); 
            // Adding to cart 40 free products 
            WC()->cart->add_to_cart( $free_product, 40 );
        }
    }
    exit();
}

Here is what my code do visually in action here from the .

It's the generated code by echoing what's inside the loop with that php file:

<html><head><title>The FOR loop in action (PHP)</title></head>
  <body>
	<p><br> - - - - - - - - - - - - - - - - - The FOR loop - - - - - - - - - - - - - - - - - <br></p>
	<div>
      <code><?php
	    for( $i = 250, $j = 500, $k = 1; $j <= 10000; $i += 250, $j += 250, $k++ ){
		  echo '
		    if( $cart_amount >= '.$i.'  && $cart_amout < ( '.$j.' ) { <br>
			    WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
			    WC()->cart->add_to_cart( $free_product, '.$k.' ); <br>
		    } <br>';
		}
		    echo '
		    if( $cart_amount >= 10000 ) { <br>
			    WC()->cart->remove_cart_item( $free_products_in_cart ); <br>
			    WC()->cart->add_to_cart( $free_product, 40 ); <br>
		    } <br>';
    ?></code>
	</div>
	<p><br> - - - - - - - - - - - - - - - - - - - E N D  - - - - - - - - - - - - - - - - - - - <br></p>
  </body>
</html>