How To Add Checkout Requirements In WooCommerce

Add Tool

Add Checkout Requirements In WooCommerce


Add Checkout Requirements In WooCommerce

Chances are that you have to set some kind of minimum necessities on your WooCommerce store before your customers checkout. What takes after is an assistant on the most capable technique to set these essentials and restrictions, without hoping to use any plugins by any stretch of the creative energy

  • Setting A Minimum Weight Requirement Per Order
  • Setting A Minimum Number Of Products Required Per Order
  • Setting A Minimum Quantity Per Product
  • Setting A Minimum Dollar Amount Per Order

Strategies Used In This Article

There is constantly more than restricted of setting least necessities in WooCommerce; the outcomes may even be indistinguishable. Be that as it may, I consider the systems depicted beneath to be the right (or better) method for doing it. Any recommendations on how you perform these undertakings or further changes are constantly welcome and got well.The accompanying code has been tried in the most recent variants accessible for WordPress (3.9.1) and WooCommerce (2.1.12). We'll be utilizing the fake information accommodated WooCommerce when you introduce the plugin. The code ought to go in your subject's functions.php record and is vigorously remarked so it's less demanding to take after and/or alter if necessary. We will be utilizing the woocommerce_check_cart_items activity gave by WooCommerce to run our capacities and execute our code. Visit the accompanying connection for a complete rundown of WooCommerce activities and channels, otherwise called snares.

Setting A Minimum Weight Requirement Per Order

woocommerce one page checkout

It is regularly helpful to confine your customer(s) from finishing the checkout process without having met a base weight necessity. Minium weight necessities can make you're transportation costs more sensible, and the delivery prepare more streamlined. Remember to change the base weight necessity to whatever works best for you, and remember that weight is figured in whatever weight unit you have set under WooCommerce - > Settings - > Products.

// Set a minimum weight requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_weight_requirements' );
function spyr_set_weight_requirements() {
 // Only run in the Cart or Checkout pages
 if( is_cart() || is_checkout() ) {
  global $woocommerce;
  // Set the minimum weight before checking out
  $minimum_weight = 25;
  // Get the Cart's content total weight
  $cart_contents_weight = WC()->cart->cart_contents_weight;
  // Compare values and add an error is Cart's total weight
     // happens to be less than the minimum required before checking out.
  // Will display a message along the lines of
  // A Minimum Weight of 25kg is required before checking out. (Cont. below)
  // Current cart weight: 12.5kg
  if( $cart_contents_weight < $minimum_weight  ) {
   // Display our error message
   wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>'
    . '<br />Current cart weight: %s%s',
    $minimum_weight,
    get_option( 'woocommerce_weight_unit' ),
    $cart_contents_weight,
    get_option( 'woocommerce_weight_unit' ),
    get_permalink( wc_get_page_id( 'shop' ) )
    ),
   'error' );
  }
 }
}

Setting A Minimum Number Of Products Required Per Order

woocommerce one page checkout

Another significant circumstance is setting a base number of things that must be asked for without a moment's delay before allowing the customer to totally pay for his solicitation and conveyed. Change "20" to whatever works best for your needs. As in the past specimen, you have to guarantee that you simply run this code on the truck and checkout pages. This is we use is_cart() and is_checkout(), which return "honest to goodness" at whatever point we are on those two specific pages. Take in additional about WooCommerce conditionals Tags.

// Set a minimum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
 // Only run in the Cart or Checkout pages
 if( is_cart() || is_checkout() ) {
  global $woocommerce;

  // Set the minimum number of products before checking out
  $minimum_num_products = 20;
  // Get the Cart's total number of products
  $cart_num_products = WC()->cart->cart_contents_count;

  // Compare values and add an error is Cart's total number of products
     // happens to be less than the minimum required before checking out.
  // Will display a message along the lines of
  // A Minimum of 20 products is required before checking out. (Cont. below)
  // Current number of items in the cart: 6 
  if( $cart_num_products < $minimum_num_products ) {
   // Display our error message
         wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' 
          . '<br />Current number of items in the cart: %s.',
          $minimum_num_products,
          $cart_num_products ),
         'error' );
  }
 }
}

Setting A Minimum Quantity Per Product

woocommerce checkout

Setting a base amount for every item is a typical prerequisite for WooCommerce stores, exceptionally on the off chance that you are offering wholesale. Setting a base amount will confine your customer(s) from acquiring a particular item in lesser amounts. For us to check for the base amounts, we have to circle through each and every item in the truck and check it against our base amount per item prerequisites set up. To set these limitations, you have to make an exhibit which holds your standards/confinements inside another cluster. Be cautious when altering this cluster, and ensure that all code is entered precisely so as to avoid blunders and surprising results. The arrangement you have to utilize is as per the following:

// Product Id and Min. Quantities per Product
// id = Product ID
// min = Minimum quantity
$product_min_qty = array( 
 array( 'id' => 47, 'min' => 100 ),
 array( 'id' => 37, 'min' => 100 ),
 array( 'id' => 34, 'min' => 100 ),
 array( 'id' => 31, 'min' => 100 ),
);

Here is where the magic happens.

// Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_qty_per_product' );
function spyr_set_min_qty_per_product() {
 // Only run in the Cart or Checkout pages
 if( is_cart() || is_checkout() ) { 
  global $woocommerce;

  // Product Id and Min. Quantities per Product
  $product_min_qty = array( 
   array( 'id' => 47, 'min' => 100 ),
   array( 'id' => 37, 'min' => 100 ),
   array( 'id' => 34, 'min' => 100 ),
   array( 'id' => 31, 'min' => 100 ),
  );

  // Will increment
  $i = 0;
  // Will hold information about products that have not
  // met the minimum order quantity
  $bad_products = array();

  // Loop through the products in the Cart
  foreach( $woocommerce->cart->cart_contents as $product_in_cart ) {
   // Loop through our minimum order quantities per product
   foreach( $product_min_qty as $product_to_test ) {
    // If we can match the product ID to the ID set on the minimum required array
    if( $product_to_test['id'] == $product_in_cart['product_id'] ) {
     // If the quantity required is less than than the quantity in the cart now
     if( $product_in_cart['quantity'] < $product_to_test['min'] ) {
      // Get the product ID
      $bad_products[$i]['id'] = $product_in_cart['product_id'];
      // Get the Product quantity already in the cart for this product
      $bad_products[$i]['in_cart'] = $product_in_cart['quantity'];
      // Get the minimum required for this product
      $bad_products[$i]['min_req'] = $product_to_test['min'];
     }
    }
   }
   // Increment $i
   $i++;
  }

  // Time to build our error message to inform the customer
  // About the minimum quantity per order.
  if( is_array( $bad_products) && count( $bad_products ) > 1 ) {
   // Lets begin building our message
   $message = '<strong>A minimum quantity per product has not been met.</strong><br />';
   foreach( $bad_products as $bad_product ) {
    // Append to the current message
    $message .= get_the_title( $bad_product['id'] ) .' requires a minimum quantity of '
       . $bad_product['min_req'] 
       .'. You currently have: '. $bad_product['in_cart'] .'.<br />';
   }
   wc_add_notice( $message, 'error' );
  }
 }
}

Setting Minimum Dollar Amount Per Order

Setting A Minimum Dollar Amount Per Order

To wrap things up, you may need to set a base dollar entirety that must be spent before your customer is allowed to checkout. You should observe that we are figuring the total quality using the subtotal, before conveyance and appraisals are added to the last demand out and out.

// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
 // Only run in the Cart or Checkout pages
 if( is_cart() || is_checkout() ) {
  global $woocommerce;

  // Set minimum cart total
  $minimum_cart_total = 10;

  // Total we are going to be using for the Math
  // This is before taxes and shipping charges
  $total = WC()->cart->subtotal;
  
  // Compare values and add an error is Cart's total
     // happens to be less than the minimum required before checking out.
  // Will display a message along the lines of
  // A Minimum of 10 USD is required before checking out. (Cont. below)
  // Current cart total: 6 USD 
  if( $total <= $minimum_cart_total  ) {
   // Display our error message
   wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
    .'<br />Current cart's total: %s %s',
    $minimum_cart_total,
    get_option( 'woocommerce_currency'),
    $total,
    get_option( 'woocommerce_currency') ),
   'error' );
  }
 }
}

Are you can see, WooCommerce permits you to utilize activities and channels to change the ordinary checkout process. All stores are distinctive and having the capacity to set these limitations when required is essential. For us designers, who need to perform undertakings like these, knowing how to do it is imperative. A debt of gratitude is in order for perusing, remarks or recommendations on the best way to enhance the code are constantly welcome. On the off chance that you have thoughts for a particular WooCommerce alteration, don't be modest and post a connection to it in the remarks for discourse.


Tags: woocommerce one page checkout, woocommerce guest checkout not working, woocommerce, woocommerce subscriptions, woocommerce plugin
Payza

About Masum Billah

Add Checkout Requirements In WooCommerce 1stWebSuite website seo tool is a free website ranking tool site provides a lot of on page and off page seo useful tools for you. On this site, we have a large number of website ranking tools


SIGN UP TO GET UPDATE TO YOUR INBOX




Discuss about: How To Add Checkout Requirements In WooCommerce


adf.ly