gpt4 book ai didi

php - 将产品自定义字段保存为 WooCommerce 管理员手动订单的自定义订单项元数据

转载 作者:行者123 更新时间:2023-12-04 12:32:55 25 4
gpt4 key购买 nike

使用 Pass custom product meta data to the order in Woocommerce 3答案代码,从后端手动创建订单时,是否可以在从后端手动添加产品时保存和显示自定义元数据?
那是我的代码(略有改动):

// Admin products: Display custom Field
add_action( 'woocommerce_product_options_general_product_data', 'product_options_general_product_data_add_field' );
function product_options_general_product_data_add_field() {
global $post;

echo '<div class="options_group">';

woocommerce_wp_select( array(
'id' => '_cost_centre',
'label' => __( 'Cost Centre', 'woocommerce' ),
'options' => array(
'MFEG' => __( 'MFEG', 'woocommerce' ), // Default displayed option value
'YDIT' => __( 'YDIT', 'woocommerce' ),
)
) );

echo '</div>';
}

// Admin products: Save custom Field
add_action( 'woocommerce_process_product_meta', 'product_options_general_product_data_save_field' );
function product_options_general_product_data_save_field( $post_id ){
if( isset( $_POST['_cost_centre'] ) )
update_post_meta( $post_id, '_cost_centre', esc_attr( $_POST['_cost_centre'] ) );
}

// Order items: Save product "Cost centre" as hidden order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
if ( $cost_centre = $values['data']->get_meta('_cost_centre') ) {
$item->update_meta_data( '_cost_centre', $cost_centre ); // Save as order item (visble on admin only)
}
}
当订单由客户端从前端创建时,这可以正常工作。但是当管理员从后端手动创建订单并添加产品时,自定义元数据是不可见的。
手动创建的订单如何解决这个问题,允许添加产品自定义字段作为自定义订单项目数据?

最佳答案

更新 3
手动后台下单可以尝试使用woocommerce_before_save_order_item专用 Action 钩如下(代码基于您的问题代码):

add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
$cost_centre = $item->get_meta('_cost_centre');
// If custom meta data is not saved as order item
if ( empty($cost_centre) ) {
// Get custom meta data from the product
$cost_centre = get_post_meta( $item->get_product_id(), '_cost_centre', true );
$cost_centre = empty($cost_centre) ? 'MFEG' : $cost_centre;

// Save it as custom order item (if defined)
$item->update_meta_data( '_cost_centre', $cost_centre );
}
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。 测试和工作 .

添加:使订单项自定义元数据对客户可见
如果您希望此订单项元数据可见 关于客户订单和电子邮件通知 ,您将替换来自 '_cost_centre' 的订单项元键至 'Cost centre'如下:
add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
$cost_centre = $item->get_meta('_cost_centre');
// If custom meta data is not saved as order item
if ( empty($cost_centre) ) {
// Get custom meta data from the product
$cost_centre = get_post_meta( $item->get_product_id(), 'Cost centre', true );
$cost_centre = empty($cost_centre) ? 'MFEG' : $cost_centre;

// Save it as custom order item (if defined)
$item->update_meta_data( 'Cost centre', $cost_centre );
}
}
这次它将在客户订单和电子邮件中可见。
您还需要将问题代码上的最后一个函数更改为:
// Order items: Save product "Cost centre" as visible order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
if ( $cost_centre = $values['data']->get_meta('_cost_centre') ) {
$item->update_meta_data( 'Cost centre', $cost_centre ); // Save as order item (visible everywhere)
}
}

Note: When the order item custom meta key starts with an underscore, it's hidden.

关于php - 将产品自定义字段保存为 WooCommerce 管理员手动订单的自定义订单项元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64030321/

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