• Resolved geokongr

    (@geokongr)


    Hello,

    I was able to apply a percentage bulk sale to a specific category and price changes as expected.

    Is it possible to get the sale percentage that the plugin generates so i could use it to style sale-flash.php file? (i.e. show the percentage discount to products page).

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Hermann LAHAMI

    (@mano88)

    That’s normally handled by woocommerce itself. I can provide a quick code to help you:

    
    $product_id=  get_the_ID();
            $product_object=  wc_get_product($product_id);
            $regular_price= $product_object->get_price();
    

    This should give you the product price with our plugin discount. From that, you should be able to extract the original price and calculate the price from there.

    Thread Starter geokongr

    (@geokongr)

    Thank you very much, that solved my case!

    At first i tried to calculate the percentage of sale (sale/regular) * 100 but sale price field is blank on products regardless if the discount from the plugin is active.

    With the code you provided i managed to get the sale price and compare it to the original price.

    Plugin Author Hermann LAHAMI

    (@mano88)

    ??

    Hello @geokongr,

    Do you mind sharing your sale-flash.php code? I am having some struggle in having this work in mine.

    Plugin Author Hermann LAHAMI

    (@mano88)

    sale-flash.php? What do you mean?

    Hello ORION,

    I also need to put my sales badge with percentages instead of the text “On Sale” in Woocommerce. Your plugin doesn’t consider the $sale_price variable which I need to use in order to use percentage with the following code (courtesy of Envantotuts+):

    <?php
        if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
        global $post, $product;
        if ( ! $product->is_in_stock() ) return;
        $sale_price = get_post_meta( $product->id, '_price', true);
        $regular_price = get_post_meta( $product->id, '_regular_price', true);
        if (empty($regular_price)){ //then this is a variable product
            $available_variations = $product->get_available_variations();
            $variation_id=$available_variations[0]['variation_id'];
            $variation= new WC_Product_Variation( $variation_id );
            $regular_price = $variation ->regular_price;
            $sale_price = $variation ->sale_price;
        }
        $sale = ceil(( ($regular_price - $sale_price) / $regular_price ) * 100);
    ?>
    <?php if ( !empty( $regular_price ) && !empty( $sale_price ) && $regular_price > $sale_price ) : ?>
        <?php echo
            apply_filters( 'woocommerce_sale_flash', '<span class="onsale">-' . $sale . '%</span>', $post, $product );
        ?>
    <?php endif; ?>

    I am not sure how exactly I should implement your code in here.

    • This reply was modified 8 years, 2 months ago by tirux.
    Plugin Author Hermann LAHAMI

    (@mano88)

    Ok our plugin does not mess with the badges. It just modify the sale price in order to force woocommerce to recognize the discount. And the sale price is dynamically set so you won’t be able to retrieve it directly from the product properties like that. Instead, i suggest you use $variation->get_sale_price() and $variation->get_price() to get the sale prices and regular price. This should give you the modified prices.

    Thanks for the info, will give it a try.

    Thread Starter geokongr

    (@geokongr)

    Hello @tirux, i’m sorry for the late reply, my gmail categorized notifications in the “Social” tab and i wasn’t aware of it. I answer just for the record with the modified part of the code you mentioned, although i see ORION gave a solid answer too.

    if (empty($regular_price)){ //then this is a variable product
            ...
            $regular_price = $variation ->regular_price; 
            //$sales_price = $variation ->sale_price;
            $product_id = get_the_ID();
            $product_object =  wc_get_product($product_id);
            $sales_price = $product_object->get_price();
    }
    	$sale = ceil(( ($regular_price - $sale_price) / $regular_price ) * 100);
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Get sale percentage’ is closed to new replies.