Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Balakrishnan-flycart/9d82386dfd608a1a8e437b0535628bdf to your computer and use it in GitHub Desktop.
Save Balakrishnan-flycart/9d82386dfd608a1a8e437b0535628bdf to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Get discount details against product
/**
* Get the discount details of given product with custom price
* @param $price float/integer
* @param $product object (Product object Example: wc_get_product($product_id))
* @param $quantity int
* @param $custom_price float/integer (0 for calculate discount from product price)
* @param $return_details string (Default value 'discounted_price' accepted values = 'discounted_price','all')
* @param $manual_request boolean (Default value false: pass this as true for get discount even if there is no item in cart)
* @param $is_cart boolean (Default value true)
* @return array|float|int - is product has no discounts, returns $price;else return array of discount details based on $return_details
*/
//apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $price, $product, $quantity, $custom_price, $return_details, $manual_request, $is_cart);
$discount = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $price_html, $product, 1, 0, 'all', 'true');
if( $discount !== false && isset($discount['total_discount_details']) && !empty($discount['total_discount_details']) ){
$total_discounts = $discount['total_discount_details'];
foreach($total_discounts as $rule_id => $detail){
//Your custom code can go here
if ( class_exists('\Wdr\App\Controllers\ManageDiscount')) {
$rules = \Wdr\App\Controllers\ManageDiscount::$available_rules;
$start_date = $end_date = array();
if(!empty($rules) && is_array($rules)){
foreach ($rules as $rule){
if($rule->rule->enabled == 1 && $rule_id == $rule->rule->id){
$start_date_timestamp = $rule->rule->date_from;
$end_date_timestamp = $rule->rule->date_to;
//Date format
$format = "Y-m-d H:i:s";
$start_date[$rule_id] = date($format, $start_date_timestamp);
$end_date[$rule_id] = date($format, $end_date_timestamp);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment