gpt4 book ai didi

php - 仅在 WooCommerce Admin 手动订单的单个订单中显示产品自定义字段

转载 作者:行者123 更新时间:2023-12-04 08:12:19 24 4
gpt4 key购买 nike

正在关注 Display a product custom field only in WooCommerce Admin single orders回答我之前的问题:

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

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

// 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 );
}
}

当我尝试更新或更改已应用网关费用的订单状态时,我收到此 fatal error :

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

这是 fatal error :

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 行是“将 SKU“ArticleID”保存为手动订单的隐藏订单项元数据”的一部分:

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

最佳答案

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

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 文件。它现在应该可以工作了。

与此线程相关:

关于php - 仅在 WooCommerce Admin 手动订单的单个订单中显示产品自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65892831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com