jeudi 23 juin 2016
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
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire