WordPress和WooCommerce变化价格

时间:2019-11-24 作者:DeadcatNoonies

我在WordPress中使用Woocommerce,注意到产品页面上没有显示变化价格,但在我将商品添加到购物车时会显示变化价格。无论尺寸选择如何,变化价格都是相同的。我读到这是Woocommerce的一个问题,但我想知道是否有人找到了一种方法来展示价格。目前,作为一种变通方法,我不得不在产品描述中添加价格。如有任何帮助,我们将不胜感激。我也不太熟悉WordPress和Woocommerce,但如果回复超出我的理解范围,我有资源可以帮助我。提前谢谢你。

2 个回复
SO网友:pacmanito

您应该从标准故障排除协议开始-检查问题是否仍然出现在一些标准主题(例如Storefront或Twenty17)中,并且所有额外的插件都已禁用。

SO网友:aye cee

如果我理解正确,您希望所选的变动价格取代所示的变动价格范围,即使在您的情况下,它们都是相同的价格。我已经能够在我自己的Woocommerce商店中通过将其添加到功能中来实现这一点。php

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

相关推荐