显示WooCommerce可变产品中所选变体的自定义字段。

huangapple go评论59阅读模式
英文:

Display a custom field for the selected variation in WooCommerce variable product

问题

I'm using Woocommerce Min/Max Quantities and have set both the "Min" quantity and the "Group of" quantity for a lot of variations. I need to display these on the front end so that customers can see when they choose a variation, eg. 1000g Blue should be in quantities of 20, whereas 250g Blue come in quantities of 25.

The code I'm working with is as follows.

function variation_group_of_quantity_shortcode($atts) {
    // Retrieve the current product's ID
    $product_id = get_the_ID();

    // Get the value of the "variation_group_of_quantity" field for the current product
    $variation_group_of_quantity = get_post_meta($product_id, 'variation_group_of_quantity', true);

    // Return the value as output
    return $variation_group_of_quantity;
}
add_shortcode('variation_group_quantity', 'variation_group_of_quantity_shortcode');

I've used a short code to include the data on the page. The problem is it keeps creating a critical error. I had a piece of code that only displayed the global "Group of" quantity and that worked brilliantly.

Any ideas what I could do make this work? I'd be grateful.

英文:

I'm using Woocommerce Min/Max Quantities and have set both the "Min" quantity and the "Group of" quantity for a lot of variations. I need to display these on the front end so that customers can see when they choose a variation, eg. 1000g Blue should be in quantities of 20, whereas 250g Blue come in quantities of 25.

The code I'm working with is as follows.

function variation_group_of_quantity_shortcode($atts) {
    // Retrieve the current product's ID
    $product_id = get_the_ID();

    // Get the value of the "variation_group_of_quantity" field for the current product
    $variation_group_of_quantity = get_post_meta($product_id, 'variation_group_of_quantity', true);

    // Return the value as output
    return $variation_group_of_quantity;
}
add_shortcode('variation_group_quantity', 'variation_group_of_quantity_shortcode');

I've used a short code to include the data on the page. The problem is it keeps creating a critical error. I had a piece of code that only displayed the global "Group of" quantity and that worked brilliantly.

Any ideas what I could do make this work? I'd be grateful.

答案1

得分: 3

以下是您提供的代码的中文翻译:

代替使用短代码(您永远无法获取所选变体ID),您可以使用以下代码在所选变体价格旁边显示此“variation_group_of_quantity”自定义字段:

add_action( 'woocommerce_available_variation', 'product_variation_group_of_quantity', 10, 3 );
function product_variation_group_of_quantity( $variation_data, $product, $variation ) {
    // 向主产品表单中添加数量组自定义字段(针对每个变体)
    $variation_data['group_of_quantity'] = $variation->get_meta('variation_group_of_quantity');

    if ( $variation_data['group_of_quantity'] ) {
        // 将“数量组” HTML 添加到格式化的价格 HTML
        $variation_data['price_html'] .=  sprintf( 
            '<span id="group-of-quantity">%s %s</span>', 
            __('按数量分组', 'woocommerce'), 
            $variation_data['group_of_quantity'] 
        );
    } 

    return $variation_data;
}

这段代码放入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

您将获得如下效果:

显示WooCommerce可变产品中所选变体的自定义字段。


附加信息

现在您还可以只使用以下函数,它将为每个变体添加您的“按数量分组”自定义字段,从而使您可以使用一些JavaScript(jQuery)在所选的变体上显示此自定义字段的值。

在下面的示例中,自定义字段文本和值将显示在产品META部分(在SKU之后)。

PHP代码:

add_action( 'woocommerce_available_variation', 'product_variation_group_of_quantity', 10, 3 );
function product_variation_group_of_quantity( $variation_data, $product, $variation ) {
    // 向主产品表单中添加数量组自定义字段(针对每个变体)
    $variation_data['grouped_quantity'] = $variation->get_meta('variation_group_of_quantity');

    return $variation_data;
}

JavaScript代码示例(嵌入PHP):

add_action( 'woocommerce_after_variations_form', 'display_variation_group_of_quantity', 11 );
function display_variation_group_of_quantity() {
    ?>
    <script type="text/javascript">
    jQuery( function($){
        var text  = '<?php _e('按数量分组', 'woocommerce'); ?>',
            sel   = 'grouped_quantity',
            html  = '<span class="'+sel+'"></span>',
            form  = $('form.variations_form');

        // 在META产品部分,在SKU之后,添加特定的HTML行以显示自定义字段值
        $('div.product_meta .sku_wrapper').after(html)

        // 在选择变体时显示您的自定义字段HTML
        form.on('show_variation', function( event, data ){
            $('span.'+sel).html(text+'<strong>'+data.grouped_quantity+'</strong>');
            console.log(data.grouped_quantity); // 用于测试 - 需要删除
        });
        // 在取消选择变体时设置空值
        form.on('hide_variation', function(){
            $('span.'+sel).html('');
        });
    });
    </script>
    <?php
}

这段代码也应放入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

您将获得如下效果:

显示WooCommerce可变产品中所选变体的自定义字段。

英文:

Instead of using a shortcode (where you will never get the selected variation ID), you could display this 'variation_group_of_quantity' custom field next to the selected variation price using the following code:

add_action( &#39;woocommerce_available_variation&#39;, &#39;product_variation_group_of_quantity&#39;, 10, 3 );
function product_variation_group_of_quantity( $variation_data, $product, $variation ) {
    // Adding to the main product form the group of quantity custom field (for each variation)
    $variation_data[&#39;group_of_quantity&#39;] = $variation-&gt;get_meta(&#39;variation_group_of_quantity&#39;);

    if ( $variation_data[&#39;group_of_quantity&#39;] ) {
        // Adding &quot;Group of quantity html to the formatted price html
        $variation_data[&#39;price_html&#39;] .=  sprintf( 
            &#39;&lt;span id=&quot;group-of-quantity&quot;&gt;%s %s&lt;/span&gt;&#39;, 
            __(&#39;Grouped by quantity of&#39;, &#39;woocommerce&#39;), 
            $variation_data[&#39;group_of_quantity&#39;] 
        );
    } 

    return $variation_data;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

You will get something like:

显示WooCommerce可变产品中所选变体的自定义字段。


ADDITION

Now you can also only use the following function, that will add to the main product for each variation your "grouped by quantity" custom field.

This will allow you with some JavaScript (jQuery) to display this custom field value for the selected variation where you like.

In the example below the custom field text + value will be displayed in the product META section (after the SKU).

The php code:

add_action( &#39;woocommerce_available_variation&#39;, &#39;product_variation_group_of_quantity&#39;, 10, 3 );
function product_variation_group_of_quantity( $variation_data, $product, $variation ) {
    // Adding to the main product form the group of quantity custom field (for each variation)
    $variation_data[&#39;grouped_quantity&#39;] = $variation-&gt;get_meta(&#39;variation_group_of_quantity&#39;);

    return $variation_data;
}

The JavaScript code example (embedded with PHP):

add_action( &#39;woocommerce_after_variations_form&#39;, &#39;display_variation_group_of_quantity&#39;, 11 );
function display_variation_group_of_quantity() {
    ?&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    jQuery( function($){
        var text  = &#39;&lt;?php _e(&#39;Grouped by quantity of &#39;, &#39;woocommerce&#39;); ?&gt;&#39;,
            sel   = &#39;grouped_quantity&#39;,
            html  = &#39;&lt;span class=&quot;&#39;+sel+&#39;&quot;&gt;&lt;/span&gt;&#39;,
            form  = $(&#39;form.variations_form&#39;);

        // In META Product Section, add after the sku, specific html line for the custom field value
        $(&#39;div.product_meta .sku_wrapper&#39;).after(html)

        // On select variation display your custom field html
        form.on(&#39;show_variation&#39;, function( event, data ){
            $(&#39;span.&#39;+sel).html(text+&#39;&lt;strong&gt;&#39;+data.grouped_quantity+&#39;&lt;/strong&gt;&#39;);
            console.log(data.grouped_quantity); // For testing - To be removed
        });
        // On unselect variation set an empty value
        form.on(&#39;hide_variation&#39;, function(){
            $(&#39;span.&#39;+sel).html(&#39;&#39;);
        });
    });
    &lt;/script&gt;
    &lt;?php
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

You will get something like:

显示WooCommerce可变产品中所选变体的自定义字段。

huangapple
  • 本文由 发表于 2023年6月26日 22:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定