| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | use TierPricingTable\PriceManager; |
| 5 | 5 | use TierPricingTable\TierPricingTablePlugin; |
| 6 | 6 | use WC_Cart; |
| 7 | 7 | use WC_Product; |
| 8 | +use WP_Error; | |
| 8 | 9 | |
| 9 | 10 | class MinQuantity extends AbstractAddon { |
| 10 | 11 | |
| 11 | 12 | public function getName(): string { |
| @@ -134,8 +135,30 @@ | ||
| 134 | 135 | |
| 135 | 136 | return $passed; |
| 136 | 137 | |
| 137 | 138 | }, 10, 4 ); |
| 139 | + | |
| 140 | + // The Store API (Cart and Checkout blocks) counterpart of the filter above, WooCommerce 11.2+. | |
| 141 | + // The minimum already reaches the Store API's numeric limits through woocommerce_quantity_input_args; | |
| 142 | + // this keeps the rule in force when another extension raises those limits. A WP_Error rejects the | |
| 143 | + // change; the shopper sees its message in the block. | |
| 144 | + add_filter( 'woocommerce_store_api_cart_item_quantity_validation', function ( $valid, $quantity, $product ) { | |
| 145 | + | |
| 146 | + if ( is_wp_error( $valid ) || ! ( $product instanceof WC_Product ) ) { | |
| 147 | + return $valid; | |
| 148 | + } | |
| 149 | + | |
| 150 | + $minimum = PriceManager::getPricingRule( $product->get_id() )->getMinimum(); | |
| 151 | + | |
| 152 | + if ( $minimum && $quantity && $quantity < $minimum ) { | |
| 153 | + return new WP_Error( 'tiered_pricing_table_minimum_quantity', | |
| 154 | + // translators: %s: minimum quantity | |
| 155 | + sprintf( __( 'Minimum quantity for the product is %s', 'tier-pricing-table' ), $minimum ) ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + return $valid; | |
| 159 | + | |
| 160 | + }, 9, 3 ); // before the maximum and step checks, so a too-small quantity gets the minimum message | |
| 138 | 161 | |
| 139 | 162 | add_filter( 'woocommerce_available_variation', function ( $variation ) { |
| 140 | 163 | $pricingRule = PriceManager::getPricingRule( (int) $variation['variation_id'] ); |
| 141 | 164 | |