Hey
Just wanted to add.
$sale_price = $product->get_sale_price();
The get_sale_price method simply returns the value stored in the “sale price field” in the db (when sale price not available, it gets the stored value of the “regular price”)
Here is the original code of the get_sale_price method in woocommerce:
/**
* Returns the product's sale price.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return string price
*/
public function get_sale_price( $context = 'view' ) {
return $this->get_prop( 'sale_price', $context );
}
It is just the stored value from the db. It does not get processed further.
That is why we have given you the snippet:
$discount = apply_filters(‘advanced_woo_discount_rules_get_product_discount_price_from_custom_price’, $product->get_price(), $product, 1, $sale_price, ‘discounted_price’, true, true);
This processes the value and gives your the discounted price.
As mentioned earlier, we do not store the sale price value to the DB because our plugin calculates the discount on the fly. (during run time). we use a set of woocommerce hooks to calculate and return the discounted price… get_sale_price method does not have any hook… so we use the pricing display hooks to calculate discounted price and display.
This was the reason why you are getting the stored value of the sale / regular price.
Since you are using the get_sale_price method (which does not have any hooks attached… so it returns the db stored value), the following snippet is required (only when you are using in custom scenarios like this..) to get the discounted price using our plugin rules.
$discount = apply_filters(‘advanced_woo_discount_rules_get_product_discount_price_from_custom_price’, $product->get_price(), $product, 1, $sale_price, ‘discounted_price’, true, true);
if($discount !== false){
$sale_price = $discount;
}
Hope this helps you understand further.
Thanks
Team Flycart