Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Created June 16, 2020 03:26
Show Gist options
  • Select an option

  • Save rajeshsingh520/4d2e346f16e05a009ba0a07923d9456f to your computer and use it in GitHub Desktop.

Select an option

Save rajeshsingh520/4d2e346f16e05a009ba0a07923d9456f to your computer and use it in GitHub Desktop.
<?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