仅在手动订单的 WooCommerce 管理单个订单中显示产品自定义字段

时间:2023-01-17
本文介绍了仅在手动订单的 WooCommerce 管理单个订单中显示产品自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

以下 仅显示产品自定义字段在 WooCommerce 管理单一订单中 回答我之前的问题,其中:

Following Display a product custom field only in WooCommerce Admin single orders answer to my previous question, which:

  1. 添加自定义 SKU 字段(文章 ID)
  2. 将自定义 SKU(ArticleID)保存为隐藏订单项元数据
  3. 将自定义 SKU(商品 ID)保存为手动订单的隐藏订单项元数据

但是,似乎最后一部分(对于手动订单)与以下我为网关费用添加的其他自定义代码发生冲突:

However, it seems that the last part (for Manual Orders) is causing a conflict with this following other custom code I added for Gateway Fees:

// Assign Card Gateway Percentage Fee to Wholesaler Profiles
add_action('woocommerce_cart_calculate_fees', 'sm_credit_card_fee_role_gateway' );
function sm_credit_card_fee_role_gateway( $cart ){
    if ( is_admin() && !defined('DOING_AJAX') )
        return;

    // Only on checkout page and logged in users
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) || ! is_user_logged_in() )
        return;

    $wpuser_object = wp_get_current_user();
    $allowed_roles = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'wholesaler-ex-vat-registered', 'wholesaler-ex-non-vat-registered', 'shop_manager');

    if ( array_intersect($allowed_roles, $wpuser_object->roles) ){
        $payment_method = WC()->session->get('chosen_payment_method');

        if ( 'cardgatecreditcard' === $payment_method ){
            $percentage = 8.25;
            $description = 'Credit Card';
        }
        elseif ( 'cardgatesofortbanking' === $payment_method ){
            $percentage = 6;
            $description = 'SofortBanking';
        }
        elseif ( 'cardgategiropay' === $payment_method ){
            $percentage = 3.15;
            $description = 'GiroPay';
        }
        elseif ( 'cardgateideal' === $payment_method ){
            $percentage = 2.1;
            $description = 'iDEAL';
        }
    }
    if ( isset($percentage) ) {
        $surcharge = ($cart->cart_contents_total + $cart->shipping_total) * $percentage / 100;
        $cart->add_fee( sprintf( __('%s Fee (%s)', 'woocommerce'), $description, $percentage . '%' ), $surcharge, true );
    }
}

当我尝试更新或更改应用了网关费用的订单的状态时,我收到此致命错误:

When I try to update or change the status of an order that has Gateway Fees applied to it, I get this Fatal Error:

所以要么我的 SKU 代码的第 3 部分需要编写得更好,要么我的网关费用代码需要更新.我该如何进一步解决此问题?

So either part 3 of my SKU code needs to be written better, or my gateway fee code needs to be updated. How do I go about troubleshooting this further?

这是致命错误:

Fatal error: Uncaught Error: Call to undefined method WC_Order_Item_Fee::get_product() in …/public_html/wp-content/themes/oceanwp-child/functions.php:920 
Stack trace: 
#0 …/public_html/wp-includes/class-wp-hook.php(287): action_before_save_order_item_callback(Object(WC_Order_Item_Fee)) 
#1 …/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) 
#2 …/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) 
#3 …/public_html/wp-content/plugins/woocommerce/includes/admin/wc-admin-functions.php(324): do_action('woocommerce_bef...', Object(WC_Order_Item_Fee)) 
#4 …/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-items.php(54): wc_save_order_items(6053, Array) 
#5 …/public_html/wp-includes/class-wp-hook.php(289): WC_Meta_B in …/public_html/wp-content/themes/oceanwp-child/functions.php on line 920

第 920 行是Save SKU"ArticleID"的一部分作为手动订单的隐藏订单项元数据":

The line Line 920 which is part of "Save SKU "ArticleID" as Hidden Order Item Meta Data for Manual Orders":

$product = $item->get_product(); // Get the WC_Product Object

推荐答案

您只需要定位最后一个函数上的行项目,这样:

You need to target only line items on the last function, this way:

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

    $articleid = $item->get_meta('articleid');

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

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

代码位于活动子主题(或活动主题)的functions.php 文件中.现在应该可以使用了.

Code goes in functions.php file of the active child theme (or active theme). It should works now.

与此线程相关:

  • 更改订单项显示元WooCommerce 管理订单页面中的关键标签
  • 仅在WooCommerce 管理员单个订单
  • 更改订单项显示元WooCommerce 管理订单页面中的关键标签

这篇关于仅在手动订单的 WooCommerce 管理单个订单中显示产品自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 下一篇:在 Woocommerce 中随处保存和显示特定的支付网关附加字段

相关文章

最新文章