有没有办法在 WooCommerce 订单页面上的每个产品下显示我的自定义 SKU?
当我编辑产品时,自定义 sku 显示正常,但它没有显示在产品的订单页面中。我需要在订单上显示此信息,以便 Zapier 可以将其与产品的 Visma 帐户软件文章 ID 进行匹配。
此尝试基于解决方案 How to add a (second) custom sku field to WooCommerce products?
// 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 );
答案 0 :(得分:1)
您可以使用钩子 woocommerce_checkout_create_order_line_item
将此产品自定义字段保存为自定义订单商品数据,在下订单时,如下所示(并在订单和电子邮件中随处显示):
// 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”可读标签名称的标签slug(在客户订单和电子邮件通知上):
// 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 文件中。经测试有效。
与此线程相关: