• Resolved raha1988ab

    (@raha1988ab)


    Hi

    We are using a code on our website that you previously developed to swap the display of the sale price and the regular price of a product. The issue with this code is that for variable products, it merges the regular price and the sale price into one. With this code, both output prices are the same, meaning the sale price and the regular price are identical. The code is as follows:

    /* Change order of Sale and promotion price */
    function custom_pric_sale_price_html( $price, $product ) {
    if ( $product->is_on_sale() ) {
    $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
    $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
    $price = '<del>' . wc_price( $regular_price ) . '</del> <br> <ins>' . wc_price( $sale_price ) . '</ins>';
    }
    return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'custom_pric_sale_price_html', 10, 2 );

    I also found these codes but they don’t work

    function custom_pric_sale_price_html( $price, $product ) {
    if ( $product->is_on_sale() ) {
    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();

    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $regular_price ) );
    $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $sale_price ) );

    $price = '<del>' . wc_price( $regular_price_display ) . '</del> <br> <ins>' . wc_price( $sale_price_display ) . '</ins>';
    } else {
    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
    $price = wc_price( $regular_price_display );
    }

    return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'custom_pric_sale_price_html', 10, 2 );

    and

    function custom_pric_sale_price_html( $price, $product ) {
    if ( $product->is_on_sale() ) {
    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();

    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $regular_price ) );
    $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $sale_price ) );

    $price = '<del>' . wc_price( $regular_price_display ) . '</del> <br> <ins>' . wc_price( $sale_price_display ) . '</ins>';
    } else {
    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
    $price = wc_price( $regular_price_display );
    }
    return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'custom_pric_sale_price_html', 10, 2 );

    and

    function custom_pric_sale_price_html( $price, $product ) {
    if ( $product->is_on_sale() ) {
    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();

    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $regular_price ) );
    $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $sale_price ) );

    $price = '<del>' . wc_price( $regular_price_display ) . '</del> <br> <ins>' . wc_price( $sale_price_display ) . '</ins>';
    }
    return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'custom_pric_sale_price_html', 10, 2 );

    And

    function custom_pric_sale_price_html( $price, $product ) {
    if ( $product->is_on_sale() && $product->is_type('variable') ) {
    // Get the variation prices
    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();

    // Check if sale price and regular price are not the same
    if ( $regular_price !== $sale_price ) {
    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $regular_price ) );
    $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $sale_price ) );
    $price = '<del>' . wc_price( $regular_price_display ) . '</del> <br> <ins>' . wc_price( $sale_price_display ) . '</ins>';
    } else {
    // If they are the same

    and

    function custom_pric_sale_price_html( $price, $product ) {
    if ( $product->is_on_sale() && $product->is_type('variable') ) {
    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();

    if ( $regular_price !== $sale_price ) {
    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $regular_price ) );
    $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $sale_price ) );
    $price = '<del>' . wc_price( $regular_price_display ) . '</del> <br> <ins>' . wc_price( $sale_price_display ) . '</ins>';
    } else {
    $price = wc_price( wc_get_price_to_display( $product, array( 'price' => $regular_price ) ) );
    }
    }
    return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'custom_pric_sale_price_html', 10, 2 );

    How can I resolve this issue?

    • This topic was modified 2 weeks, 5 days ago by raha1988ab.
Viewing 1 replies (of 1 total)
  • Plugin Support Rajesh K. (woo-hc)

    (@rajeshml)

    Hello @raha1988ab,

    Thank you for reaching out to WooCommerce Support.

    For reference, this particular forum is meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I will leave this thread open for a bit to see if anyone can chime in and help you out further.

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thanks!

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