首页 > 解决方案 > Woocommerce产品自定义字段保存功能与保存时订单附加费冲突

问题描述

我正在使用此处的代码https://stackoverflow.com/a/64031464/8692331从前端或后端按顺序保存产品的自定义数据。

订购时,我们对“手提包”收取定制费用 ( https://ibb.co/Ytv9KBR )。我们发现,当在订单中添加“手提包”时,我们无法在后端对该订单进行任何更改,并出现致命错误。

这一行“$pro_weight = get_post_meta($item->get_product_id(), '_pweight', true);” 导致此函数的致命错误:

// Save custom field value in order when manually create
add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
    $pro_weight = $item->get_meta('_pweight');
    // If custom meta data is not saved as order item
    if ( empty($pro_weight) ) {
        // Get custom meta data from the product
        $pro_weight = get_post_meta( $item->get_product_id(), '_pweight', true );
        $pro_weight = empty($pro_weight) ? 'Net Weight' : $pro_weight;
        
        // Save it as custom order item (if defined)
        $item->update_meta_data( 'Net Weight', $pro_weight );
    }
}

这是错误:

[28-Jun-2021 13:35:23 UTC] PHP Fatal error:  Uncaught Error: Call to undefined method WC_Order_Item_Fee::get_product_id() in /home/username/public_html/wp-content/plugins/additional_functions.php:75
Stack trace:
#0 /home/username/public_html/wp-includes/class-wp-hook.php(292): action_before_save_order_item_callback(Object(WC_Order_Item_Fee))
#1 /home/username/public_html/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters('', Array)
#2 /home/username/public_html/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#3 /home/username/public_html/wp-content/plugins/woocommerce/includes/admin/wc-admin-functions.php(341): do_action('woocommerce_bef...', Object(WC_Order_Item_Fee))
#4 /home/username/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-items.php(54): wc_save_order_items(3368, Array)
#5 /home/username/public_html/wp-includes/class-wp-hook.php(294): WC_Meta_Box_Order_Items::save(3368)
#6 /home/username/public_html/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters('', Array)
#7 /home/usersname in /home/username/public_html/wp-content/plugins/additional_functions.php on line 75

第 75 行是“$pro_weight = get_post_meta($item->get_product_id(), '_pweight', true);”

可以做些什么来解决这个问题?

提前致谢。

更新:

我使用此处的帮助修复了此问题仅在 WooCommerce Admin 单个订单中显示产品自定义字段

add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
    // Targeting only order item type "line_item"
    if ( $item->get_type() !== 'line_item' )
        return; // exit

    $pro_weight = $item->get_meta('_pweight');

    if ( ! $pro_weight ) {
        $product = $item->get_product(); // Get the WC_Product Object
        
        // Get custom meta data from the product
        $pro_weight = $product->get_meta('_pweight');
        
        // For product variations when the "articleid" is not defined
        if ( ! $pro_weight && $item->get_variation_id() > 0 ) {
            $product   = wc_get_product( $item->get_product_id() ); // Get the parent variable product
            $pro_weight = $product->get_meta( '_pweight' );  // Get parent product "articleid"
        }

        // Save it as custom order item (if defined for the product)        
        if ( $pro_weight ) {
            $item->update_meta_data( 'Net Weight', $pro_weight );
        }
    }
}

由于某些订单需要支付额外费用,因此代码只需针对“line_item”类型。

标签: phpwordpresswoocommercecustom-fieldsorders

解决方案


推荐阅读