我正在使用WooCommerce最小/最大数量,并且已经为许多变化设置了"最小"数量和"组"数量.我需要在前端显示这些,以便客户可以看到当他们 Select 一个变体,例如.1000克蓝的数量应该是20个,而250克蓝的数量是25个.

我正在使用的代码如下所示.

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

我使用了一个简短的代码来包括页面上的数据.问题是,它一直在制造一个严重的错误.我有一段代码,它只显示全球"一组"数量,而且工作得很好.

你知道我能做些什么让这件事成功吗?我会很感激的.

推荐答案

您可以使用以下代码在所选差异价格旁边显示此‘Variant_Group_of_Quantity’自定义字段,而不是使用短代码(您将永远无法获得所选差异ID):

add_action( 'woocommerce_available_variation', 'product_variation_group_of_quantity', 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['group_of_quantity'] = $variation->get_meta('variation_group_of_quantity');

    if ( $variation_data['group_of_quantity'] ) {
        // Adding "Group of quantity html to the formatted price html
        $variation_data['price_html'] .=  sprintf( 
            '<span id="group-of-quantity">%s %s</span>', 
            __('Grouped by quantity of', 'woocommerce'), 
            $variation_data['group_of_quantity'] 
        );
    } 

    return $variation_data;
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

您将得到如下内容:

enter image description here


加法

现在您也只能使用以下功能,这将添加到每个变化的主要产品您的"按数量分组"自定义域.

这将允许您使用一些JavaScript(JQuery)在您喜欢的地方显示所选变体的这个定制字段值.

在下面的示例中,自定义字段Text+Value将显示在产品元部分中(SKU之后).

Php代码:

add_action( 'woocommerce_available_variation', 'product_variation_group_of_quantity', 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['grouped_quantity'] = $variation->get_meta('variation_group_of_quantity');

    return $variation_data;
}

Java代码示例(嵌入了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('Grouped by quantity of ', 'woocommerce'); ?>',
            sel   = 'grouped_quantity',
            html  = '<span class="'+sel+'"></span>',
            form  = $('form.variations_form');

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

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

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

您将得到如下内容:

enter image description here

Php相关问答推荐

在WooCommerce产品档案页面中显示产品销售百分比

模型中的Laravel自定义查询字段

WordPress:将自定义单选输入字段添加到用户配置文件

Yahoo Finance API文件_GET_CONTENTS 429请求太多

向在添加到购物车上动态创建的WooCommerce产品添加发货类别

EBay Inventory API createOrReplaceInventoryItem出错

在Laravel中修改JSON输出

我必须对所有的SQL查询使用预准备语句吗?

Apache只允许index.php-不从根目录工作

如何从该字符串中删除这些图标转换的ASCII问号字符?

如何在PHP中组合日期范围

根据发货类别在WooCommerce产品页面中显示快捷代码

Woocommerce单一产品Ajax添加到带有自定义购物车项目数据的购物车

如何批量处理消息总线中的消息

既然setSQLLogger()已被弃用,如何使用中间件在 Doctrine 3 中记录查询执行时间?

.htaccess 重写规则问题

在PHP WordPress插件中使用变量连接路径时的问题解决方法

如何在 Laravel 中修改嵌套数组的键?

如何使用 WhatsApp Cloud API 向 WhatsApp 发送消息,而无需在发送消息之前注册收件人号码?

在 WooCommerce 结帐后更新用户元字段