如何在WooCommerce订单中显示产品自定义字段(自定义SKU) [英] How to show a product custom field (custom SKU) in WooCommerce orders

查看:90
本文介绍了如何在WooCommerce订单中显示产品自定义字段(自定义SKU)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在WooCommerce订单页面上的每种产品下显示我的自定义SKU?

Is there a way to display my custom SKU under each product on the WooCommerce order page?

当我编辑产品时,自定义sku可以正常显示,但不会显示在产品的订购页面中.我需要在订单上显示此信息,以便Zapier可以将其与产品的Visma帐户软件ArticleID匹配.

The custom sku displays fine when I edit the product, but it does not display in the order page for the product. I need this information to show on the order so that Zapier can match it with the Visma Account Software ArticleID of the product.

此尝试基于解决方案如何向WooCommerce产品添加(第二个)自定义sku字段?

// Add Custom SKU Field

function my_add_custom_sku() {
    $args = array(
        'label' => __( 'ArticleID', 'woocommerce' ),
        'placeholder' => __( 'Enter Visma ArticleID Here', 'woocommerce' ),
        'id' => 'articleid',
        'desc_tip' => true,
        'description' => __( 'Visma ArticleID is for integration with Zapier.', 'woocommerce' ),
    );
    woocommerce_wp_text_input( $args );

}
add_action( 'woocommerce_product_options_sku', 'my_add_custom_sku' );

// Save
function my_save_custom_meta( $product ){
    if( isset($_POST['articleid']) ) {
        $product->update_meta_data( 'articleid', sanitize_text_field( $_POST['articleid'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'my_save_custom_meta', 10, 1 );

推荐答案

您可以使用钩子 woocommerce_checkout_create_order_line_item 将此产品自定义字段保存为自定义订单商品数据,在下订单时,如下所示(并在订单和电子邮件中随处显示):

You could use the hook woocommerce_checkout_create_order_line_item to save this product custom field as custom order Item data, when order is placed, as follows (and display it everywhere on orders and emails):

// Save as custom order item meta data and display on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_articleid_on_orders_and_emails', 10, 4 );
function add_articleid_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {
    $articleid = $values['data']->get_meta('articleid'); // Get product "articleid"

    // For product variations when the "articleid" is not defined
    if ( empty($articleid) && $values['variation_id'] > 0 ) {
        $product    = wc_get_product( $values['product_id'] ); // Get the parent variable product
        $articleid = $product->get_meta( 'articleid' );  // Get parent product "articleid"
    }

    if ( ! empty($articleid) ) {
        $item->add_meta_data( 'articleid', $articleid ); // add it as custom order item meta data
    }
}

以下内容将"articleid"更改为显示带有"ArticleID"的标签块可读的标签名称(在客户订单和电子邮件通知中):

And the following to change "articleid" displayed label slug with "ArticleID" readable label name (on customer orders and email notifications):

// Replace the label (slug) by a readable label name on orders and emails
add_filter( 'woocommerce_display_item_meta', 'filter_order_item_articleid_displayed_label', 100, 3 );
function filter_order_item_articleid_displayed_label( $html, $item, $args ) {
    // Not on admin
    if ( ! is_admin() && $item->get_meta('articleid') ) {
        $html = str_replace('articleid', __('ArticleID', 'woocommerce'), $html);
    }

    return $html;
}

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

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

与此线程相关:

这篇关于如何在WooCommerce订单中显示产品自定义字段(自定义SKU)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆