使用WooCommerce德国市场变量产品显示独特的价格

时间:2018-05-18 11:50:42

标签: php wordpress woocommerce price variations

我想删除WooCommerce中变量产品的双倍价格字段。 目前有一个显示价格范围的总价格字段,并且有一个价格字段显示所选变体的价格。

我现在想用所选变体的价格替换整体价格。并且仅在未选择变化时显示总价格。

@LoicTheAztec在这里有一个很好的答案:https://stackoverflow.com/a/47618953/1788961

他使用以下代码更改输出:

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

    endif;
}

问题是,由于插件“德国市场”,我使用了一些不同的HTML结构。因此,上述解决方案对我不起作用。 我试图从上面的答案编辑代码,但我无法使其工作。

我想问题是不同的HTML结构和一些额外的样式。 该插件还为库存,税收和运输添加了一些额外的字段。上面答案的解决方案隐藏了这些领域。

这是一个带有插件“德国市场”的变量产品的演示:http://demo.marketpress.com/hamburg-de/shop/clothing/ship-your-idea-2/

这是我的可变产品页面的HTML结构(您可能会注意到,我已经将价格的位置更改为变体表格上方):

<form class="variations_form cart" action="https://example.com/product/test/" method="post" enctype="multipart/form-data" data-product_id="7474" data-product_variations="....." current-image="2492">
    <div class="woocommerce-variation single_variation">
        <div class="woocommerce-variation-description"></div>

        <div class="woocommerce-variation-price"><span class="price"><del><span class="woocommerce-Price-amount amount">169,00&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></span></del><ins><span class="woocommerce-Price-amount amount">119,00&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></span></ins></span>
            <div class="wgm-info woocommerce-de_price_taxrate ">Enthält 19% Mwst.</div>
            <div class="versandkostenfrei product_free_delivery">versandkostenfrei</div>
            <div class="wgm-info woocommerce_de_versandkosten">zzgl. <a class="versandkosten" href="https://example.com/versand/" target="_blank">Versand</a></div>
            <div class="wgm-info shipping_de shipping_de_string">
                <small>
                    <span>Lieferzeit: sofort versandfertig, Lieferfrist 1-3 Tage</span>
                </small>
            </div>
        </div>

        <div class="woocommerce-variation-availability">
            <p class="stock in-stock ">Vorrätig</p>
        </div>
    </div>
    <table class="variations" cellspacing="0">
        <tbody>
            <tr>
                <td class="label"><label for="pa_farbe">Farbe</label></td>
                <td class="value">
                    <div id="picker_pa_farbe" class="select swatch-control"><!-- variations --></div>
                </td>
            </tr>
        </tbody>
    </table>


    <div class="single_variation_wrap">
        <div class="woocommerce-variation-add-to-cart variations_button woocommerce-variation-add-to-cart-enabled">
            <div class="quantity">
                <label class="screen-reader-text" for="quantity_5afeba61bc89d">Anzahl</label>
                <input type="number" id="quantity_5afeba61bc89d" class="input-text qty text" step="1" min="1" max="95" name="quantity" value="1" title="Menge" size="4" pattern="[0-9]*" inputmode="numeric" aria-labelledby="">
            </div>
            <button type="submit" class="single_add_to_cart_button button alt">In den Warenkorb</button>
            <input type="hidden" name="add-to-cart" value="7474">
            <input type="hidden" name="product_id" value="7474">
            <input type="hidden" name="variation_id" class="variation_id" value="7478">
        </div>
        <input type="hidden" name="wlid" id="wlid">
        <input type="hidden" name="add-to-wishlist-type" value="variable">
        <input type="hidden" name="wl_from_single_product" value="1">
    </div>
</form>

1 个答案:

答案 0 :(得分:1)

我已经将它与德国市场插件一起使用了,但它并没有像插件那样显示带税提示的价格......您需要对代码进行更多自定义并在插件中搜索一下源代码......

这不起作用因为插件已经删除了Woocommerce挂钩的价格,用其他钩子优先级的其他自定义挂钩函数替换它们......

所以试试这个,因为它以正常的价格显示解决它,并允许你拥有与我最初的答案相同的功能:

// removing the price of variable products
remove_action( 'woocommerce_single_product_summary', array( 'WGM_Template', 'woocommerce_de_price_with_tax_hint_single'), 7 );

// 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;
                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 '<div class="hidden-variable-price" >'.$price.'</div>';

    endif;
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试并正常工作。