我正在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);

推荐答案

您的代码中有多个错误,并且您没有使用正确的挂钩和正确的方法.始终使用WC_Order_ItemWC_Order_Item_Shipping可用的setter方法,以更改订单发货项目(S).

当订单状态更改为"已准备发货"时,最好使用the dedicated transition order status hooks个中的一个来更改订单.

try 以下操作:

// Customize order shipping item(s)
add_action( 'woocommerce_order_status_ready-to-ship', 'customize_order_shipping_item_on_ready_to_ship', 10, 2 );
function customize_order_shipping_item_on_ready_to_ship( $order_id, $order ) {
    // Iterating through order shipping items
    foreach( $order->get_items( 'shipping' ) as $item_id => $item ) {
        $method_name = __('LCS Express'); // New shipping method name 

        $item->set_name($method_name);
        $item->set_method_title($method_name);
        $item->set_method_id( sanitize_title($method_name) );
        $item->save();
    }
}

// On edit order shipping item, change 'lcs-express' selected option text to 'LCS Express'
add_action('admin_footer', 'wc_order_admin_footer_script', 10);
function wc_order_admin_footer_script() {
    if( isset($_GET['page'], $_GET['action']) && 'wc-orders' === $_GET['page'] && 'edit' === $_GET['action'] ) :
    ?>
    <script>
    jQuery(function($){
        if( $('select.shipping_method').val() === 'lcs-express' ) {
            $('select.shipping_method option[value=lcs-express]').text('LCS Express');
        }
    });
    </script>
    <?php
    endif;
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试和工作.

相关:

Php相关问答推荐

基于验证动态更改输入字段类(名称密码)&

如何使用`preg_replace`删除重复的空格字符

PHP DateInterval天数不一致

Laravel:让所有用户在一年中的特定月份休息几天

使用.htaccess将任何路由重定向到文件

通过访问单个产品页面更新WooCommerce产品元数据

为什么只有最后一次点击的点赞/心形会按预期改变 colored颜色 ,而其他的保持正常 colored颜色 ?

如何在微软图形API中使用用户S访问令牌或基于租户ID的访问令牌?

上传WordPress模板中的翻译文件(.mo和.po)

更改特定产品标签的 Woocommerce 产品名称

如果所有请求都通过index.php路由,如何使用htaccess文件强制使用HTTPS?

Laravel 在数据库上显示加密列

从 XAMPP 运行 shell 脚本时,意外标记(附近出现语法错误

标签打印机的 CSS

如何将文件上传添加到 WooCommerce 结帐?

在 PHP 8.2 中使用 PHP Spreadsheet 和 CodeIgniter 3 导出 Excel 时出错

PHP 创建 CSV 文件并用波斯语写入

PHP for each 函数打印 td

如何将 2021-04-08T22:25:09.335541Z 作为时间戳保存到 Laravel 中的数据库中?

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表