【问题标题】:Adding Custom Meta Data from Products to Orders items in Woocommerce将自定义元数据从产品添加到 Woocommerce 中的订单项目
【发布时间】:2018-01-03 23:04:10
【问题描述】:

我正在尝试将我的 woocommerce 产品的自定义元数据添加到我的 Woocommerce Admin 中的订单列,但此代码不适用于 Wordpress 主题中的 function.php。

// 为 PD 编号订购获取 Meta
add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 1, 3);
函数adding_custom_data_in_order_items_meta($post_id,$cart_item_key){

    // 商品对应的商品ID:
    $product_id = $post_id['product_id'];
    //$pd_number = $post_id['_pd_number'];
    //$pd_number = $_POST['_pd_number'];
    $pd_number = get_post_meta( $post_id[ 'product_id' ], '_pd_number', true );

    if ( !empty($pd_number) )
        wc_add_order_item_meta($post_id, '_pd_number', $pd_number, true);
}

谢谢

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案1】:

    从 Woocommerce 3 开始,建议使用更好的挂钩 (请参阅 this answer 线程)。

    您的代码中有一些错误。试试这个:

    // Add the the product custom field as item meta data in the order
    add_action( 'woocommerce_add_order_item_meta', 'pd_number_order_meta_data', 10, 3 );
    function pd_number_order_meta_data( $item_id, $cart_item, $cart_item_key ) {
        // get the product custom field value
        $pd_number = get_post_meta( $cart_item[ 'product_id' ], '_pd_number', true );
    
        // Add the custom field value to order item meta
        if( ! empty($pd_number) )
            wc_update_order_item_meta( $item_id, '_pd_number', $pd_number );
    }
    

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

    这应该适用于从 2.5.x 到 3+ 的 WooCommerce 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多