Last active
December 29, 2020 06:29
-
-
Save gvgvgvijayan/183d31131d0b1eab1cc395acc500a80d to your computer and use it in GitHub Desktop.
This gist is used in the tutorial https://www.vijayan.in/how-to-limit-the-cart-quantity-of-discounted-product/
This file contains 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 | |
define( 'MAX_QUANTITY', 3 ); | |
add_filter( | |
'woocommerce_add_to_cart_validation', | |
'validate_discounted_prdct_qty_limitation', | |
10, | |
3 | |
); | |
/** | |
* Hooked functionality to limit the discounted product quantity to checkout. | |
* | |
* @param bool $passed Validation. | |
* @param int $product_id Product ID. | |
* @param int $quantity Product quantity. | |
* | |
* @return bool Return true if quantity is not exceeded else false. | |
*/ | |
function validate_discounted_prdct_qty_limitation( $passed, $product_id, $quantity ) { | |
$product_object = wc_get_product( $product_id ); | |
$parent_id = $product_object->get_parent_id(); | |
if ( ! empty( $parent_id ) ) { | |
$product_id = $parent_id; | |
} | |
return validate_product_qty( $product_id, $quantity, 'add' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment