Skip to content

Instantly share code, notes, and snippets.

@gvgvgvijayan
Last active December 29, 2020 06:29
Show Gist options
  • Save gvgvgvijayan/183d31131d0b1eab1cc395acc500a80d to your computer and use it in GitHub Desktop.
Save gvgvgvijayan/183d31131d0b1eab1cc395acc500a80d to your computer and use it in GitHub Desktop.
<?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