在 WooCommerce 中移动价格变化位置 [已关闭]

问题描述 投票:0回答:1

我已经开始使用 WooCommerce 插件在 Wordepress 中构建电子商店。

我添加了一些具有变体的产品,我注意到价格显示在属性选择字段之后。

对于简单产品,是否可以在标题和简短描述之间移动价格?

其中一款产品的网址是: http://www.roubinisideas.com/test2/product/uncategorized/vintage/

php wordpress woocommerce product-variations product-price
1个回答
3
投票

更新 (2019 年 9 月)

  • 避免可用性重复(2018 年解决了错误)
  • 其他产品类型保持不变(2019)

这是可能的,它是基于我所做的这个答案

这里我更新了要考虑的jQuery代码当默认情况下为可变产品设置变体时

这是代码:

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

代码位于活动子主题(或主题)的任何 php 文件中,或者也位于任何插件 php 文件中。

此代码经过测试,适用于 WooCommerce 3+(也应该适用于 WooCommerce 2.6.x)


相关:将可变价格范围替换为 WooCommerce 3 中所选的变化价格

© www.soinside.com 2019 - 2024. All rights reserved.