Created
June 16, 2020 03:26
-
-
Save rajeshsingh520/4d2e346f16e05a009ba0a07923d9456f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| if a slot has already reached the max quanity allowed | |
| for the product and user has this product in his cart then the slot will be removed | |
| */ | |
| class pisol_quanity_limit_for_product{ | |
| private $product_id = 637; // this is the product for order restriction | |
| private $limit = 2; // this is the slot limit of product quantity, | |
| function __construct(){ | |
| add_filter('pisol_disable_order_limit_check', function(){ return true; }); | |
| add_filter('pisol_dtt_time_slot_filter', array($this,'timeFilter'),100, 2); | |
| } | |
| function timeFilter($slots, $date){ | |
| foreach($slots as $key => $slot){ | |
| $orders = $this->getOrdersInSlot($slot['id'], $date); | |
| if(!empty($orders)){ | |
| $product_quantity = $this->productQuantityInOrders($orders); | |
| if($product_quantity >= $this->limit){ | |
| unset($slots[$key]); | |
| } | |
| } | |
| } | |
| return $slots; | |
| } | |
| function getOrdersInSlot( $slot, $date){ | |
| $args = array( | |
| 'limit' => -1, | |
| 'status' => array('pending', 'processing', 'on-hold', 'completed'), | |
| 'meta_query' => array( | |
| 'relation'=>'AND', | |
| array( | |
| 'key' => 'pi_system_delivery_date', | |
| 'compare' => '==', | |
| 'value' => $date, | |
| ), | |
| array( | |
| 'key' => 'pi_delivery_time', | |
| 'compare' => 'LIKE', | |
| 'value' => $slot, | |
| ) | |
| ) | |
| ); | |
| $orders = wc_get_orders($args); | |
| return $orders; | |
| } | |
| function productQuantityInOrders($orders){ | |
| $total_qty = 0; | |
| foreach($orders as $order){ | |
| $order_items = $order->get_items(); | |
| foreach( $order_items as $item ) { | |
| $product_id = $item['product_id']; | |
| $quantity = $item['quantity']; | |
| if($product_id == $this->product_id){ | |
| $total_qty = $total_qty + $quantity; | |
| } | |
| } | |
| } | |
| return $total_qty; | |
| } | |
| } | |
| new pisol_quanity_limit_for_product(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment