我正在try 在WooCommerce管理订单编辑页面,当订单状态更改为"准备购物",将运输项目名称更改为"LCS Express".此外,我试图更新航运选定的选项,当编辑订单航运项目,包括"LCSexpress "航运方法
以下是我的代码:
function customize_order_item_display($product, $item, $item_id) {
// Get the order ID
$order_id = $item->get_order_id();
// Get the order object
$order = wc_get_order($order_id);
// Check if the payment status is "ready-to-ship"
if ($order->get_status() === 'ready-to-ship') {
// Modify the shipping method
$new_shipping_method = 'LCS Express'; // Replace with your desired shipping method
// Get the shipping method data
$shipping_method_data = $item->get_data()['shipping'];
// Set the new shipping method name
$shipping_method_data['name'] = $new_shipping_method;
// Set the new shipping method ID
$shipping_method_data['method_id'] = sanitize_title($new_shipping_method);
// Update the item with the modified shipping method data
$item->set_props(array('shipping' => $shipping_method_data));
$item->save();
// Modify the shipping input fields using JavaScript
?>
<script type="text/javascript">
jQuery(document).ready(function($){
// Set the shipping method name in the input field
$('input.shipping_method_name').val('<?php echo esc_js($new_shipping_method); ?>');
// Set the shipping method ID in the select field
$('select.shipping_method').val('<?php echo esc_js($shipping_method_data['method_id']); ?>');
});
</script>
<?php
}
}
add_action('woocommerce_admin_order_item_values', 'customize_order_item_display', 10, 3);