Support » Plugin: Advanced Shipping Validation for WooCommerce » Check cart if has product above price excl. tax

  • Resolved Jason Wong

    (@eljkmw)


    How do I configure your plugin to verify if the cart has product that is above a price excluding tax.

    The product price is based on a single (1) quantity, and not of a multiple quantity. For example,

    Product A > 100 <= accepted
    or
    Product B x 2 > 100 <= unaccepted

    But if the above example are combined, then it’s accepted.

    Please advise. Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeroen Sormani

    (@sormano)

    Hi Jason,

    You can use the ‘Subtotal ex. taxes’ condition, but note that this is based on the entire cart subtotal, not on a per-product basis.

    It may be the plugin is not able (by default) to do exactly what you’re looking for, but I’m not entirely sure of your exact requirements.

    Cheers,
    Jeroen

    G’day Jeroen

    If your plugin lacks the ability to meet my requirement, is there any snippet that can pick out the price (excl. tax) from the list of products in the cart?

    I guess it’ll go something like …

    global $woocommerce;
    $lowprice_item = false;
    foreach( $woocommerce->cart->get_cart() as $cart_item ) { 
      $price = $cart_item['data']->get_price();
      if ( $price < 100 )  $lowprice_item = true;
    }
    if( $lowprice_item )  wc_add_notice( 'Low price item found! Please pick an item priced higher than 100.', 'error' );

    I’m unsure if this crude snippet will work, or not.
    Please advise. Thank you.

    I guess this function will be hooked to

    add_action( 'woocommerce_check_cart_items',

    for both cart & checkout pages.

    Plugin Author Jeroen Sormani

    (@sormano)

    Hi Jason,

    Adding a code snippet like that could work for your scenario.
    I can’t test/confirm the working / accuracy of your custom script, but I do believe you’re on the right track.

    Have a great day!
    Jeroen

    Hi Jeroen,

    The following snippet is what I came up with.

    // Check cart has any low price item
    function wc_cart_has_lowprice_item() {
    	global $woocommerce;
    	$lowprice_item = false;
    
    	// get price of each cart item
    	foreach( $woocommerce->cart->get_cart() as $cart_item ) { 
    		$price = $cart_item['data']->get_price();
    	//	echo '<script type="text/javascript">alert("'.$price.'"); </script>';
    
    		if( $price < 100 )	$lowprice_item = true;
    	}
    	
    	return $lowprice_item;
    }
    
    // Add Cart page error message if cart has low price item
    function wc_cart_lowprice_item_errormsg() {
    	if ( wc_cart_has_lowprice_item() && is_cart() ) {
    		wc_add_notice( '<strong>No floral product added!</strong> Please include one of at least RM 100 (ex. GST).', 'error' );
    	}
    }
    add_action( 'woocommerce_check_cart_items', 'wc_cart_lowprice_item_errormsg' );
    
    // Add Checkout page error message if cart has low price item
    function wc_checkout_lowprice_item_errormsg() {
    	if ( wc_cart_has_lowprice_item() && is_checkout() ) {
    		wc_add_notice( '<strong>No floral product added!</strong> Please include one of at least RM 100 (ex. GST).', 'error' );
    	}
    }
    add_action( 'woocommerce_checkout_process', 'wc_checkout_lowprice_item_errormsg' );
    

    The WooCommerce functions are somewhat tricky to apply, which lead me to create two different add_action hooks – one for Shopping Cart, and the other for Checkout. The woocommerce_check_cart_items function applies to both mentioned pages. If an error notice is added, it will not create the checkout form, which leaves an empty checkout page that I don’t want to see.

    If you have some other ideas to optimise this snippet, please let me know.
    Cheers (^_^)v

    • This reply was modified 2 months, 4 weeks ago by  Jason Wong.

    I’ve extended the check product categories for the Shopping Cart page, which is covered by your plugin for the Checkout page only.

    // Check cart has any non main product category
    function wc_cart_has_nonmain_prodcat() {
    	global $woocommerce;
    	$nonmain_item = true;
    
    	// get price of each cart item
    	foreach( $woocommerce->cart->get_cart() as $cart_item ) {
    		$product = $cart_item['data'];
    
    		// if none of the array of product categories is found
    		if ( has_term( array( 'floral-category', 'festival-promotion', 'bridal-bouquet' ), 'product_cat', $product->id ) )
    			$nonmain_item = false;
    	}
    
    	return $nonmain_item;
    }
    
    // Add Cart page error message if cart has non main product category
    function wc_cart_nonmain_prodcat_errormsg() {
    	if ( wc_cart_has_nonmain_prodcat() && is_cart() ) {
    		wc_add_notice( 'No main floral product has been added. Please include at least one to your shopping cart.', 'error' );
    	}
    }
    add_action( 'woocommerce_check_cart_items', 'wc_cart_nonmain_prodcat_errormsg' );
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.