Support » Plugin: Discount Rules for WooCommerce » there is no get_sale_price()

  • Hi, in my custom theme I have a custom badge with percent discount, which works on
    $product->get_regular_price();
    and
    $product->get_sale_price();
    On this two variables is calculated percent discount. But products discounted with your plugin (PRO Version) doesn’t have this variables.
    How I can reach the regular price and sale price?
    Or maybe just the percent calculated by plugin? In options there is variable, which can be used as suffix: {price_save_percent} – how I can use this in php code in theme?

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi
    Thanks for reaching out.

    We would like to let you know that our plugin does not update the prices in the backend (Sale price for discounts).
    Our plugin applies discount on the fly dynamically while adding the product to cart. However, it doesn’t change the price in the back-end.

    To get discount of a product, you can give a try using this snippet.
    https://gist.github.com/AshlinRejo/c37a155a42c0e30beafbbad183f0c4e8
    ​​
    Hope this helps!

    Thanks
    Team Flycart

    Thread Starter liderbudowlany

    (@liderbudowlany)

    I use only the option discount product in catalog (not in cart). I tried this snippet from this post https://wordpress.org/support/topic/woocommerce-get_price-and-get_sale_price-not-working/#post-13842899 , but it display wrong price

    Hi
    Thanks for the reply.

    Instead of this,
    apply_filters(‘advanced_woo_discount_rules_get_product_discount_price_from_custom_price’, 10, $product, 1, $sale_price, ‘discounted_price’, true, true);

    Can you try this
    apply_filters(‘advanced_woo_discount_rules_get_product_discount_price_from_custom_price’, $product->get_price(), $product, 1, $sale_price, ‘discounted_price’, true, true);

    Hope this helps!

    Thanks
    Team Flycart

    Thread Starter liderbudowlany

    (@liderbudowlany)

    Still something is wrong, because with this code:

    $sale_price = $product->get_sale_price();//sale price or final price
    $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;
    }

    $sale_price now return value of original price, not after discount.
    Discount works for sure, because in product there is for example:
    69,00 zł 68,31 zł
    but $sale_price return 69

    Hi
    Thanks for the reply.

    Can you please confirm if there is a discount for that product?

    We recommend you to open a ticket providing screenshots and elaborate details so that one of our support engineers will help you in resolving the issue.

    Let’s help you with it.

    Thanks
    Team Flycart

    Plugin Author flycart

    (@flycart)

    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

    Thread Starter liderbudowlany

    (@liderbudowlany)

    Please take a look at my screenshots: https://imgur.com/a/XRKC7q2
    First screenshot presents what discount is set.
    Second screeshot show product in catalog, discount works, there is 30% discount.
    But on top of image there is additional bar, where I want to show percentage discount, so I want calcute this from original price and sale price. I have original price value, but I want to achieve variable with sale price, so using this:

    $sale_price = $product->get_sale_price();
    $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;
    }

    I wanted to achieve this price after plugin discount, but finally $discount shows original price, before discount. So is there some problem with this snippet?

    In bar above product image there is showing now $sale_price, when I achieve true sale price after discount I will change this to variable with calculated percentage discount, but now I’m focused on achieving true sale price.

    Hi @liderbudowlany

    We had a check on the given screenshot and it seems like the configuration is not from our discount rules plugin ?

    Can you please confirm if you are using our plugin and provide us with screenshots indicating the same.
    Really appreciate it.

    Thanks
    Team Flycart

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.