• Resolved andersjytzler

    (@andersjytzler)


    Hello guys.
    I want to only add this free gift to the cart, if the cart total (incl. tax, excl. shipping), is above 1000.
    I copy pasted this code, that depends on just purchasing 1 product from a specific category. Which is fine, so i tried to add the minimum cart total on top of it.
    But it doesn’t work as intended. I was hoping someone could look it through, and point me in the right direction ??

    function aaptc_add_product_to_cart( $item_key, $product_id ) {
    
    	$product_category_id 	= 18; // cricket bat category id
    
    	$product_cats_ids 	= wc_get_product_term_ids( $product_id, 'product_cat' );
    
        if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) {
            $free_product_id = 2969;  // Product Id of the free product which will get added to cart
            $found 		= false;
    
            //check if product already in cart
            if ( sizeof( WC()->cart->get_cart() ) > 0 && WC()->cart->get_cart_total() > 1000 ) {
                  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->get_cart_total() > 1000 )
                    WC()->cart->add_to_cart( $free_product_id );
            } elseif ( WC()->cart->get_cart_total() > 1000 ) {
                // if no products in cart, add it
                WC()->cart->add_to_cart( $free_product_id );
            }        
        }    
    }
    
    add_action( 'woocommerce_add_to_cart', 'aaptc_add_product_to_cart', 10, 2 );
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting cart total price’ is closed to new replies.