【问题标题】:Move the variation price location in WooCommerce在 WooCommerce 中移动变化价格位置
【发布时间】:2018-05-17 01:00:09
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce price variations


    【解决方案1】:

    更新 (2019 年 9 月)

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

    这是可能的,它基于我所做的this answer

    这里我更新了 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 上运行)


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

    【讨论】:

    • 谢谢 LoicTheAztec。它可以工作,但是当我选择一个变体时,几乎会在添加到购物车之前显示第二个价格。请告诉我如何删除它?
    • @Giorgos 我已经在我的代码中添加了visibility: hidden !important; ...请尝试这种方式。它应该可以工作......但仍然有一个像这样的错误。
    • 非常感谢!有用!!!此外,我从价格中删除了“发件人”,并且在价格从代码中删除了可用性之后,我也删除了未定义。
    • 请问您是什么错误?
    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多