Skip to content

Instantly share code, notes, and snippets.

@gvgvgvijayan
Last active December 29, 2020 06:26
Show Gist options
  • Save gvgvgvijayan/49f5861d766e53f4ac981b1b2711e303 to your computer and use it in GitHub Desktop.
Save gvgvgvijayan/49f5861d766e53f4ac981b1b2711e303 to your computer and use it in GitHub Desktop.
<?php
/**
* Helper function to check discounted product add to cart limit reached.
*
* @param int $product_id Product ID.
* @param int $quantity Product quantity.
* @param string $action_type Type of the action. Allowed 'add' or 'update'.
*
* @return bool Return true if quantity exceeded else false.
*/
function is_discounted_prdct_qty_exceeded( $product_id, $quantity, $action_type ) {
$product_obj = wc_get_product( $product_id );
$regular_price = $product_obj->get_regular_price();
$sale_price = $product_obj->get_sale_price();
$cart_id = WC()->cart->generate_cart_id( $product_id );
$find_product_in_cart = WC()->cart->find_product_in_cart( $cart_id );
if ( ! empty( $sale_price ) &&
(float) $sale_price < (float) $regular_price ) { // Validate is discounted product.
if ( 'add' === $action_type &&
! empty( $find_product_in_cart ) ) {
$cart_item = wc()->cart->get_cart_item( $cart_id );
$quantity = $cart_item['quantity'] + $quantity;
}
if ( $quantity > MAX_QUANTITY ) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment