1

I'm have started building an e-shop in Wordepress with WooCommerce plugin.

I added some products with variations and I noticed that the price is displayed after attributes select fields.

Is it possible to move the price between Title and short description as for simple products?

The url of one product is: http://www.roubinisideas.com/test2/product/uncategorized/vintage/

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Giorgos
  • 19
  • 1
  • 2

1 Answers1

3

Update (on September 2019):

  • Avoiding availability repetitions (bug solved on 2018)
  • Keep other product types unchanged (2019)

This is possible and it's is based on this answer that I have made:

Here I have updated the jQuery code to take into account when a variation is set by default for a variable product.

Here is the code:

add_action( 'woocommerce_single_product_summary', 'move_single_product_variable_price_location', 2 );

function move_single_product_variable_price_location() {
    global $product;

    // Variable product only
    if( $product->is_type('variable') ):
    
    // removing the price of variable products
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    
    // Add back the relocated (customized) price of variable products
    add_action( 'woocommerce_single_product_summary', 'custom_single_product_variable_prices', 10 );
    
    endif;
}


function custom_single_product_variable_prices(){
    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;
            visibility: hidden !important; 
        }
    </style>
    <script>
        jQuery(document).ready(function($) {
            // When variable price is selected by default
            setTimeout( function(){
                if( 0 < $('input.variation_id').val() && null != $('input.variation_id').val() ){
                    if($('p.availability'))
                        $('p.availability').remove();

                    $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
                    console.log($('div.woocommerce-variation-availability').html());
                }
            }, 300 );

            // On live variation selection
            $('select').blur( function(){
                if( 0 < $('input.variation_id').val() && null != $('input.variation_id').val() ){
                    if($('.price p.availability') || $('.price p.stock') )
                        $('p.price p').each(function() {
                            $(this).remove();
                        });

                    $('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>';
}

Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

This code is tested and works on WooCommerce 3+ (should work on WooCommerce 2.6.x too)


related: Replace the Variable Price range by the chosen variation price in WooCommerce 3

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you LoicTheAztec. It works but when I select a variation then a second price almost displays before Add to cart. Please could you tell me how to remove it? – Giorgos Dec 03 '17 at 17:53
  • Of course, here is the product! http://www.roubinisideas.com/test2/product/uncategorized/vintage/ – Giorgos Dec 03 '17 at 18:00
  • @Giorgos I have added in my code `visibility: hidden !important;` … Try it this way please. it should work… But there is still a bug like. – LoicTheAztec Dec 03 '17 at 18:19
  • Thank you very much! It works!!! Also I removed the "From" from the price and also I removed the undefined after the price removing the availability from the code. – Giorgos Dec 03 '17 at 18:24
  • Please what kind of bug do you mean? – Giorgos Dec 03 '17 at 18:41
  • Hi, I'm using the plugin GermanMarket for WooCommerce and it shows some additional information below the price. The problem now is, that these informations are hidden after I change the variable. Also at the beginning the price is two times. Here's the code with the two prices: https://codepen.io/cray_code/pen/BrVddj and here's how they behave: https://screencast.com/t/zwqNwyEo1 – Cray Apr 02 '18 at 19:06
  • @Cray Sorry but this code works on default Woocommerce basis, and as I don't use "GermanMarket for WooCommerce" plugin, you should need to get and copy those particular additional infos with jQuery in the desired location... – LoicTheAztec Apr 03 '18 at 02:15
  • @LoicTheAztec I will try. But I see no difference in the code around the price... – Cray Apr 03 '18 at 09:16