Public Ticket #2895277

Pricing Display

Closed
Comments
2 Tickets 1
Tim started the conversation 5 years ago on October 15, 2021 at 9:49am

Hi I'm having a problem displaying a "You Save" figure in my product page. If a user is logged out it shows correctly "You Save: £xx (xx%)". It also shows correctly if a user is logged in and there are no prices entered in the b2b king price tiers for the various user groups and just the "regular price" / "Sale Price" are filled.

Currently the site is set without prices in the various tiers for logged in users, so please note you will be able to see the calculation working as intended

Below is the code snippet I'm using to display the discount amount string. Please could you have a look and let me know what I need to do to make the calculation work when a price is entered into a B2BKing price tier. Thanks

    // For simple products
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
    global $product;

    if( $product->is_type('simple') && $product->is_on_sale() ) {
        $regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
        $active_price  = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );

        $saved_amount  = $regular_price - $active_price;
        $percentage    = round( $saved_amount / $regular_price * 100 );

        echo '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
    }
}

// For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 9999, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {

    if( $variation->is_on_sale() ) {
        $saved_amount  = $data['display_regular_price'] - $data['display_price'];
        $percentage    = round( $saved_amount / $data['display_regular_price'] * 100 );

        $data['price_html'] .= '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';

    }
    return $data;
}


2,762
KingsPlugins replied 5 years ago on October 17, 2021 at 8:50pm

HI Tim,

Thanks for using our plugins,


1) For customer support, kindly please provide us with your purchase code for the plugin. To get the code, you can go to https://codecanyon.net/downloads -> Click as it's shown in this image: 0?ui=2&ik=5a46e45e67&attid=0.3&permmsgid=msg-a:r-1923782612169588203&th=1761c0675bf89b03&view=fimg&sz=s0-l75-ft&attbid=ANGjdJ-zLMjn4syMQHE8EgxICLzCEo7MY-gzbNtDXzEyXGlgWDd6b9aUEtZexm03yOgZ9BwG43TM_dt9_phxajGStl_b05qEzbxpr_RgkODtTU2sZdzVgX9lOFBZyAY&disp=emb&realattid=ii_ki5by8ks2

-> The text file downloaded should have something like this inside: Item Purchase Code: b3e5fe72-00b0-4019-a8e2-06fb26264742

Thank you for the help,


2)

What exactly happens when you add the price tiers?

Does the "You Save" figure not display at all? Or does it display but the calculation is incorrect?


Kind regards,

Stefan


2 Tickets 1
Tim replied 5 years ago on October 18, 2021 at 3:10pm

Hi Stefan,

Thanks for the reply.
I don't have a purchase code for the plugin, I downloaded it straight from my wordpress plugins module. Isn't 'product pre-orders for woocommerce' a free plugin?

To answer your question no.2, when the price tiers are added the "you save" displays except the price/ percentage calculation is just 0

2,762
KingsPlugins replied 5 years ago on October 18, 2021 at 3:25pm

Hi Tim,

"product pre-orders for woocommerce" is not our plugin. Our plugin is B2BKing. There is indeed a free version of the plugin here https://wordpress.org/plugins/b2bking-wholesale-for-woocommerce/


This support forum here is for the premium version of the plugin. 

The support forum for the free version is available at https://wordpress.org/support/plugin/b2bking-wholesale-for-woocommerce/


However there is no "Price tiers" functionality in the free version - that is only a premium feature.


Kind regards,

Stefan

2,762
KingsPlugins replied 5 years ago on October 18, 2021 at 4:13pm

No worries, now it all makes sense! Thank you for sharing the purchase code,

Please try to replace your current snippet with the following:

// For simple products
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
    global $product;
    if( $product->is_type('simple') && $product->is_on_sale() ) {
        $original_price = (float) wc_get_price_to_display( $product, array('price' => get_post_meta($product->get_id(), '_regular_price', true) ) ); 
        $regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
        $active_price  = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );
        $active_price = min($regular_price, $active_price);
        $saved_amount  = $original_price - $active_price;
        $percentage    = round( $saved_amount / $original_price * 100 );
        echo '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
    }
}
// For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 9999, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {
    if( $variation->is_on_sale() ) {
        $original_price = (float) wc_get_price_to_display( $product, array('price' => get_post_meta($variation->get_id(), '_regular_price', true) ) ); 
        $saved_amount  = $original_price - $data['display_price'];
        $percentage    = round( $saved_amount / $original_price * 100 );
        $data['price_html'] .= '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
    }
    return $data;
}
2 Tickets 1
Tim replied 5 years ago on October 19, 2021 at 10:14am

Hi Stefan,

That code works perfectly!! Thank you so much for your work on it!!
 PS. B2BKing is a great product by the way, easy to use yet powerful enough to handle a variety of complex setups. Brilliant work!

Thanks again,

Tim

1

Sign In