1

The answer to this question - Replace Variable Product Price by the chosen variation price in WooCommerce 3 - was the answer with the solution of the problem.

How do I fix this bug?

If the Variable product has the same price of variations, then there is a duplication of the status "In stock", Like this:

enter image description here

add_action( 'woocommerce_before_single_product', 'check_if_variable_first' );
function check_if_variable_first(){
if ( is_product() ) {
    global $post;
    $product = wc_get_product( $post->ID );
    if ( $product->is_type( 'variable' ) ) {
        // removing the price of variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

// Change location of
add_action( 'woocommerce_single_product_summary', 'custom_wc_template_single_price', 10 );
function custom_wc_template_single_price(){
global $product;

// Variable product only
if($product->is_type('variable')):

// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

if ( $price !== $saleprice && $product->is_on_sale() ) {
    $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}

?>
<style>
    div.woocommerce-variation-price,
    div.woocommerce-variation-availability,
    div.hidden-variable-price {
        height: 0px !important;
        overflow:hidden;
        position:relative;
        line-height: 0px !important;
        font-size: 0% !important;
    }
</style>
<script>
jQuery(document).ready(function($) {
    $('select').blur( function(){
        if( '' != $('input.variation_id').val() ){
            $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
            console.log($('input.variation_id').val());
        } else {
            $('p.price').html($('div.hidden-variable-price').html());
            if($('p.availability'))
                $('p.availability').remove();
            console.log('NULL');
        }
    });
});
</script>
<?php

echo '<p class="price">'.$price.'</p>
<div class="hidden-variable-price" >'.$price.'</div>';

endif;
}

      }
   }
}
aynber
  • 22,380
  • 8
  • 50
  • 63
Bose Loewe
  • 11
  • 2
  • It should be better to give a live link of a variable product, as your theme can customize the html structure and classes names… – LoicTheAztec Dec 06 '17 at 16:23
  • http://fh7988px.bget.ru/?product=product-41 – Bose Loewe Dec 07 '17 at 13:52
  • Hello! Thank you for your answers and for finally wanting to help. And I'm not a bot. Yesterday I showed a link in which you can see the error about which I wrote in the first message. Today, I replaced the code with the one you proposed in the answer yesterday. The error has not been fixed. – Bose Loewe Dec 08 '17 at 08:50
  • http://fh7988px.bget.ru/?product=product-41 – Bose Loewe Dec 08 '17 at 08:52
  • The structure of your html is different, so that's why… I have make a change in the jQuery code, that should make it work with your html structure, just as you expect. – LoicTheAztec Dec 08 '17 at 09:26
  • Many thanks for solving this problem. – Bose Loewe Dec 08 '17 at 16:48

1 Answers1

0

My answer on this thread was working nice in WooCommerce 3 on Storefront theme…

Try instead this revisited code:

add_action( 'woocommerce_before_single_product', 'custom_template_single_price', 1 );
function custom_template_single_price(){
    if ( ! is_product() ) return; // Only on single product pages

    global $product, $post;

    if ( $product->is_type( 'variable' ) ) {
        // removing the price of variable products
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

        // Change location of
        add_action( 'woocommerce_single_product_summary', 'replace_template_single_price', 10 );
    }
}

function replace_template_single_price(){
    global $product;

    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice && $product->is_on_sale() ) {
        $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
    }

    ?>
    <style>
        div.woocommerce-variation-price,
        div.woocommerce-variation-availability,
        div.hidden-variable-price {
            height: 0px !important;
            overflow:hidden;
            position:relative;
            line-height: 0px !important;
            font-size: 0% !important;
        }
    </style>
    <script>
    jQuery(document).ready(function($) {
        $('select').blur( function(){
            if( '' != $('input.variation_id').val() ){
                if($('p.availability'))
                    $('p.availability').remove();
                if($('p.stock'))
                    $('p.stock').remove();
                $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
            } else {
                $('p.price').html($('div.hidden-variable-price').html());
                if($('p.availability'))
                    $('p.availability').remove();
                if($('p.stock'))
                    $('p.stock').remove();
            }
        });
    });
    </script>
    <?php

    echo '<p class="price">'.$price.'</p>
    <div class="hidden-variable-price" >'.$price.'</div>';
}

Tested in WooCommerce 3.2.x and works (on storefront theme)

I have added some jQuery code to remove the stock availability label before setting it. It should work for you too.

For some themes with customizations on the html structure and classes names, you will need to make some changes in the code

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399