Advertisement
businessdad

Codeable Task 46269 - Holiday Checkout minimums

Nov 15th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. /**
  2.  * Codeable Task 46269 - Holiday Checkout minimums
  3.  * Ensures that the minimum quantities for specific products are respected.
  4.  *
  5.  * @link https://app.codeable.io/tasks/46269
  6.  */
  7. function aelia_set_min_qty_per_product() {
  8.     // Only run in the Cart or Checkout pages
  9.     if( is_cart() || is_checkout() ) {
  10.         // Product Id and Min. Quantities per Product
  11.         $product_min_qty = array(
  12.             22328 => 5, //Braised Turkey Hindquarters
  13.             22305 => 5, //Roasted Turkey Breast
  14.             22336 => 5, //Shrimp Cocktail
  15.             22335 => 5, //Roasted Leg Of Lamb
  16.             22332 => 5, //Center-Cut Pork Loin
  17.             22330 => 5, //New York Strip Loin
  18.         );
  19.  
  20.         // Will hold information about products that have not
  21.         // met the minimum order quantity
  22.         $bad_products = array();
  23.  
  24.         // Loop through the products in the Cart
  25.         foreach( wc()->cart->cart_contents as $product_in_cart ) {
  26.             $product_id = $product_in_cart['product_id'];
  27.  
  28.             // If there is a minimum quantity for the product and the cart item is
  29.             // below such quantity, add the cart item's product to the "bad" list
  30.             if( isset( $product_min_qty[$product_id] ) && ( $product_in_cart['quantity'] < $product_min_qty[$product_id] ) ) {
  31.                 $bad_products[] = array(
  32.                     'id' => $product_id,
  33.                     'in_cart' => $product_in_cart['quantity'],
  34.                     'min_req' => $product_min_qty[$product_id],
  35.                 );
  36.             }
  37.         }
  38.  
  39.         // Time to build our error message to inform the customer
  40.         // About the minimum quantity per order.
  41.         if( !empty( $bad_products ) ) {
  42.             // Lets begin building our message
  43.             $message = '<strong>A minimum quantity per product has not been met.</strong><br />';
  44.             foreach( $bad_products as $bad_product ) {
  45.                 // Append to the current message
  46.                 $message .= get_the_title( $bad_product['id'] ) .' requires a minimum quantity of '
  47.                          . $bad_product['min_req']
  48.                          .' lbs. You currently have: '. $bad_product['in_cart'] .' lbs.<br />';
  49.             }
  50.             wc_add_notice( $message, 'error' );
  51.         }
  52.     }
  53. }
  54. add_action( 'woocommerce_check_cart_items', 'aelia_set_min_qty_per_product' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement