【问题标题】:Display in additional information tab, some product settings custom fields values在附加信息选项卡中显示,一些产品设置自定义字段值
【发布时间】:2017-12-30 00:51:00
【问题描述】:

我在配送选项卡的 WooCommerce 产品设置页面中添加了一些自定义字段。

我需要让它在附加信息选项卡的产品页面上可见,并且它可以自动用于比较插件?

我需要在现有的重量和尺寸字段中添加单位。

谢谢。

【问题讨论】:

  • 其中一个单位是从三个不同参数创建的维度。
  • @LoicTheAztec - 我使用了您的代码,link,我将在产品页面上的附加信息选项卡中看到它,例如尺寸和重量参见图片link,并且它会自动用于比较职能。您可以在构建站点link 上查看它。谢谢。

标签: php wordpress woocommerce product dimensions


【解决方案1】:

更新 2

根据this answer 对您的一个问题提出的问题,这里是在前端单个产品页面“附加信息”选项卡上获取此自定义产品字段数据的方法。

你会明白的:

对于此产品设置自定义字段:

由于自定义字段保存在产品元数据中,我们使用 Wordpress get_post_meta() 函数以这种方式获取值:

add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
function custom_data_in_product_add_info_tab( $product ) {

    //Product ID - WooCommerce compatibility
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    // Get your custom fields data
    $custom_field1 = get_post_meta( $product_id, '_custom_meta_field1', true );
    $custom_field2 = get_post_meta( $product_id, '_custom_meta_field2', true );

    // Set your custom fields labels (or names)
    $label1 = __( 'Your label 1', 'woocommerce');
    $label2 = __( 'Your label 2', 'woocommerce');

    // The Output
    echo '<h3>'. __('Some title', 'woocommerce') .'</h3>
    <table class="custom-fields-data">
        <tbody>
            <tr class="custom-field1">
                <th>'. $label1 .'</th>
                <td>'. $custom_field1 .'</td>
            </tr>
            <tr class="custom-field2">
                <th>'. $label2 .'</th>
                <td>'. $custom_field2 .'</td>
            </tr>
        </tbody>
    </table>';
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 WooCommerce 3+ 上经过测试并且有效

现在要在您的比较功能中使用该数据是另一个问题,您应该在一个新问题中提供此比较功能所涉及的代码......


相关答案:

Add custom dimension fields to each variation settings for variable products

【讨论】:

  • 感谢您的回答。我将代码放在 function.php 中,但它不会显示表格。我已将产品的一些数据添加到要显示的字段中,但我看不到它。我对这段代码有疑问。添加的字段将添加到现有表中还是作为新表?也许有问题。你可以看看HERE 它的样子。我将“附加信息”选项卡重命名为“产品数据”。
  • 这部分是什么:` $custom_field1 = 'first-value'; $custom_field2 = '第二值'; ` 而它用固定信息完成变量并覆盖正确的数据?
  • 在附加信息选项卡添加的自定义字段中,仍然不会在产品页面的前端显示表格或参数。您可以在我上面发布的链接上看到它。并且对于维度字段也不需要更改字段数组的定义,它可以接受三个参数,而不仅仅是一个(3x 占位符)?
  • 对不起,它不会显示表格...我不知道为什么。我已复制您的代码并粘贴到 function.php。我发现回声线的末尾不是“;”但这没有帮助。全部都是PHP函数不是有问题吗?将其添加到现有表中是否可能且更容易?虽然对我来说会更好。我放在那里的尺寸代码并对其进行了一些更改,现在它显示在后端。见HERE
  • 我需要更改上面的注释,维度代码仅在视觉上起作用,但它不存储数据。我只放了“7.5”之类的数字。
【解决方案2】:

我试图做同样的事情,但我想将自定义字段添加到现有表中,而不是创建新表。

如果您想将其添加到附加信息表中,您可以使用 woocommerce_display_product_attributes 过滤器

function yourprefix_woocommerce_display_product_attributes($product_attributes, $product){
    $product_attributes['customfield'] = [
        'label' => __('custom', 'text-domain'),
        'value' => get_post_meta($product->get_ID(), 'customfield', true),
    ];
    return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'yourprefix_woocommerce_display_product_attributes', 10, 2);

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 2021-02-03
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多