| 1 |
<?php namespace TierPricingTable\Addons\MinQuantity; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\AbstractAddon; |
| 4 |
use TierPricingTable\PriceManager; |
| 5 |
use TierPricingTable\TierPricingTablePlugin; |
| 6 |
use WC_Cart; |
| 7 |
use WC_Product; |
| 8 |
|
| 9 |
class MinQuantity extends AbstractAddon { |
| 10 |
|
| 11 |
public function getName(): string { |
| 12 |
return __( 'Minimum order quantity validation', 'tier-pricing-table' ); |
| 13 |
} |
| 14 |
|
| 15 |
public function run() { |
| 16 |
|
| 17 |
add_action( 'woocommerce_before_calculate_totals', function ( WC_Cart $cart ) { |
| 18 |
foreach ( $cart->get_cart_contents() as $cartItemKey => $cartItem ) { |
| 19 |
if ( $cartItem['data'] instanceof WC_Product ) { |
| 20 |
|
| 21 |
$productId = ! empty( $cartItem['variation_id'] ) ? $cartItem['variation_id'] : $cartItem['product_id']; |
| 22 |
|
| 23 |
$pricingRule = PriceManager::getPricingRule( $productId ); |
| 24 |
$min = $pricingRule->getMinimum(); |
| 25 |
|
| 26 |
if ( ! $min ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
if ( $this->getProductCartQuantity( $cartItem['product_id'], 'product', $cart ) < $min ) { |
| 31 |
$cart->cart_contents[ $cartItemKey ]['quantity'] = $min; |
| 32 |
|
| 33 |
// translators: %1$s: item name, %2$s: minimum quantity |
| 34 |
wc_add_notice( sprintf( __( 'Minimum quantity for the %1$s is %2$d', 'tier-pricing-table' ), |
| 35 |
$cartItem['data']->get_name(), $min ), 'error' ); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} ); |
| 40 |
|
| 41 |
add_filter( 'woocommerce_quantity_input_args', function ( $args, $product = null ) { |
| 42 |
|
| 43 |
if ( $product instanceof WC_Product && TierPricingTablePlugin::isSimpleProductSupported( $product ) ) { |
| 44 |
$pricingRule = PriceManager::getPricingRule( $product->get_id() ); |
| 45 |
$min = $pricingRule->getMinimum(); |
| 46 |
|
| 47 |
if ( ! $min ) { |
| 48 |
return $args; |
| 49 |
} |
| 50 |
|
| 51 |
$min = max( 1, $min - $this->getProductCartQuantity( $product->get_id() ) ); |
| 52 |
$args['min_value'] = $min; |
| 53 |
} |
| 54 |
|
| 55 |
return $args; |
| 56 |
}, 9999, 2 ); |
| 57 |
|
| 58 |
// Quantity field in the cart |
| 59 |
add_filter( 'woocommerce_quantity_input_args', function ( $args, ?WC_Product $product ) { |
| 60 |
|
| 61 |
if ( ! apply_filters( 'tiered_pricing_table/minimum_quantity/control_cart_quantity_field', true, |
| 62 |
$product ) ) { |
| 63 |
return $args; |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! is_cart() ) { |
| 67 |
return $args; |
| 68 |
} |
| 69 |
|
| 70 |
if ( $product instanceof WC_Product && TierPricingTablePlugin::isSimpleProductSupported( $product ) ) { |
| 71 |
$pricingRule = PriceManager::getPricingRule( $product->get_id() ); |
| 72 |
$min = $pricingRule->getMinimum(); |
| 73 |
|
| 74 |
if ( ! $min ) { |
| 75 |
return $args; |
| 76 |
} |
| 77 |
|
| 78 |
$args['min_value'] = $pricingRule->getMinimum(); |
| 79 |
} |
| 80 |
|
| 81 |
return $args; |
| 82 |
}, 10, 3 ); |
| 83 |
|
| 84 |
add_filter( 'woocommerce_add_to_cart_validation', function ( $passed, $productId, $quantity ) { |
| 85 |
$productId = intval( $productId ); |
| 86 |
$quantity = intval( $quantity ); |
| 87 |
|
| 88 |
$pricingRule = PriceManager::getPricingRule( $productId ); |
| 89 |
$min = $pricingRule->getMinimum(); |
| 90 |
|
| 91 |
if ( ! $min ) { |
| 92 |
return $passed; |
| 93 |
} |
| 94 |
|
| 95 |
$min = max( 1, $min - $this->getProductCartQuantity( $productId ) ); |
| 96 |
|
| 97 |
if ( $quantity < $min ) { |
| 98 |
|
| 99 |
// translators: %s: minimum quantity |
| 100 |
wc_add_notice( sprintf( __( 'Minimum quantity for the product is %s', 'tier-pricing-table' ), $min ), |
| 101 |
'error' ); |
| 102 |
|
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return $passed; |
| 107 |
|
| 108 |
}, 10, 3 ); |
| 109 |
|
| 110 |
add_filter( 'woocommerce_update_cart_validation', function ( $passed, $cart_item_key, $values, $quantity ) { |
| 111 |
|
| 112 |
$product = $values['data'] ?? null; |
| 113 |
|
| 114 |
if ( ! ( $product instanceof WC_Product ) ) { |
| 115 |
return $passed; |
| 116 |
} |
| 117 |
|
| 118 |
$pricingRule = PriceManager::getPricingRule( $product->get_id() ); |
| 119 |
|
| 120 |
if ( ! $pricingRule->getMinimum() ) { |
| 121 |
return $passed; |
| 122 |
} |
| 123 |
|
| 124 |
$min = max( 1, $pricingRule->getMinimum() - $this->getProductCartQuantity( $values['product_id'] ) ); |
| 125 |
|
| 126 |
if ( $quantity && $quantity < $pricingRule->getMinimum() ) { |
| 127 |
|
| 128 |
// translators: %s: minimum quantity |
| 129 |
wc_add_notice( sprintf( __( 'Minimum quantity for the product is %s', 'tier-pricing-table' ), $pricingRule->getMinimum() ), |
| 130 |
'error' ); |
| 131 |
|
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
return $passed; |
| 136 |
|
| 137 |
}, 10, 4 ); |
| 138 |
|
| 139 |
add_filter( 'woocommerce_available_variation', function ( $variation ) { |
| 140 |
$pricingRule = PriceManager::getPricingRule( (int) $variation['variation_id'] ); |
| 141 |
|
| 142 |
$min = $pricingRule->getMinimum(); |
| 143 |
|
| 144 |
if ( ! $min ) { |
| 145 |
return $variation; |
| 146 |
} |
| 147 |
|
| 148 |
$min = max( 1, $min - $this->getProductCartQuantity( (int) $variation['variation_id'], 'variation' ) ); |
| 149 |
|
| 150 |
$variation['min_qty'] = $min; |
| 151 |
$variation['qty_value'] = $min; |
| 152 |
|
| 153 |
return $variation; |
| 154 |
} ); |
| 155 |
|
| 156 |
add_action( 'wp_head', function () { |
| 157 |
|
| 158 |
if ( ! is_product() ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
?> |
| 163 |
<script> |
| 164 |
// Handle Minimum Quantities by Tiered Pricing Table |
| 165 |
(function ($) { |
| 166 |
|
| 167 |
$(document).on('found_variation', function (event, variation) { |
| 168 |
if (typeof variation.qty_value !== "undefined") { |
| 169 |
// update quantity field with a new minimum |
| 170 |
$('form.cart').find('[name=quantity]').val(variation.qty_value) |
| 171 |
} |
| 172 |
|
| 173 |
if (typeof variation.min_qty !== "undefined") { |
| 174 |
// update quantity field with a new minimum |
| 175 |
$('form.cart').find('[name=quantity]').attr('min', variation.min_qty); |
| 176 |
} |
| 177 |
}); |
| 178 |
|
| 179 |
})(jQuery); |
| 180 |
</script> |
| 181 |
<?php |
| 182 |
} ); |
| 183 |
} |
| 184 |
|
| 185 |
protected function getProductCartQuantity( $productId, $type = 'product', $cart = null ) { |
| 186 |
$qty = 0; |
| 187 |
|
| 188 |
$cart = $cart ? $cart : wc()->cart; |
| 189 |
|
| 190 |
if ( $cart && is_array( $cart->cart_contents ) ) { |
| 191 |
foreach ( $cart->cart_contents as $cartItem ) { |
| 192 |
|
| 193 |
if ( 'variation' === $type ) { |
| 194 |
$compare = ! empty( $cartItem['variation_id'] ) ? $cartItem['variation_id'] : 0; |
| 195 |
} else { |
| 196 |
$compare = $cartItem['product_id']; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $compare == $productId ) { |
| 200 |
$qty += $cartItem['quantity']; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return apply_filters( 'tiered_pricing_table/minimum_quantity/item_quantity', $qty, $productId ); |
| 206 |
} |
| 207 |
|
| 208 |
public function getDescription(): string { |
| 209 |
return __( 'Turning it off might be useful in case of incompatibility with a 3rd-party MOQ plugin you might use.', |
| 210 |
'tier-pricing-table' ); |
| 211 |
} |
| 212 |
|
| 213 |
public function getSlug(): string { |
| 214 |
return 'minimum-quantity'; |
| 215 |
} |
| 216 |
} |
| 217 |
|