Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Last active August 13, 2020 10:04
Show Gist options
  • Save rajeshsingh520/8448b0af225bc251b43275c65d4c8ac1 to your computer and use it in GitHub Desktop.
Save rajeshsingh520/8448b0af225bc251b43275c65d4c8ac1 to your computer and use it in GitHub Desktop.
If there is 50 unit sell limit of product A, quanity based order limit for time slot
<?php
class pisol_quanity_limit_for_product{
private $product_id = 637; // this is the product for which your want to limit the slot
private $limit = 10; // this is the slot limit of product quantity
function __construct(){
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,
)
)
);
$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