当用户购买具有每月创建订单的经常性选项的产品时,没有设置发货方法.只有父订单有发货方式,而所有子订单没有发货.

我想在创建循环订单时将父发货方法添加到所有子订单.

在这里,我try 了代码,但不起作用,我想添加父订单时的发货方法创建子订单.

add_action('woocommerce_new_order', 'add_colissimo_method', 10, 1);

function add_colissimo_method($order_id) {
    $order = wc_get_order($order_id);

  
    if ($order->get_shipping_method()) {
        return; 
    }


        $subscriptions = wcs_get_subscriptions_for_order($renewal_order_id);

        if (!empty($subscriptions)) {
    
            $parent_order_id = $subscriptions[0]->get_parent_id();

            if (!empty($parent_order_id)) {
                $parent_order = wc_get_order($parent_order_id);
                $parent_shipping_method = $parent_order->get_shipping_method();
                update_post_meta($renewal_order_id, '_shipping_method', $parent_shipping_method);
            }
     }

    $order->save();
    
}

推荐答案

你没有用正确的方法做这件事,你需要使用WC Subscriptions filter hooks.

try 以下(code is commented)个选项:

add_filter( 'wcs_new_order_created', 'add_shipping_items_to_renewal_orders', 10, 3 );
function add_shipping_items_to_renewal_orders( $order, $subscription, $type ) {
    // Targeting renewal orders only
    if ( $type === 'renewal_order' ) {
        // Get the parent order (initial order)
        $parent_order = $subscription->get_related_orders('all', 'parent');
        $parent_order = reset($parent_order);

        // Set the array for tax calculations
        $calculate_tax_for = array(
            'country'   => $parent_order->get_shipping_country(),
            'state'     => $parent_order->get_shipping_state(),
            'postcode'  => $parent_order->get_shipping_postcode(),
            'city'      => $parent_order->get_shipping_city(),
        );

        // Loop through parent order "shipping" items
        foreach( $parent_order->get_items('shipping') as $parent_item ) {
            $item  = new WC_Order_Item_Shipping(); // create a new empty order item "shipping"
            $item->set_method_title( $parent_item->get_method_title() ); // set the existing Shipping method title
            $item->set_method_id( $parent_item->get_method_id() ); // set an existing Shipping method rate ID
            $item->set_total( $parent_item->get_total() );  // set an existing Shipping method total
            $item->calculate_taxes( $calculate_tax_for );  // Calculate taxes for the item

            $order->add_item( $item ); // Add the shipping item to the renewal order
        }
        $order->calculate_totals(); // recalculate totals and save
    }
    return $order;
}

应该能行得通.

相关:Add update or remove WooCommerce shipping order items


Php相关问答推荐

为什么PHP输出\n而不是实际创建一个新元素?

无法添加ProxyClass__setInitialized()上的忽略

如何更改数据表行背景 colored颜色

如何在Foreach语句中使用php和li按降序排序?

使用PHP curl的Amazon SP API批处理调用

如何将WooCommerce产品从一个产品复制到另一个产品S

在php中,方法之间的属性链接是如何工作的

函数存储到变量中

添加不存在的订单元数据以扩展WooCommerce针对特定产品的管理订单搜索

在WooCommerce中以编程方式添加特定产品后,将价格设置为零

PHP 中使用 JSON 的自定义会话处理程序会因错误而 destruct 会话

Google Drive API 服务->文件->get('root') 失败并显示找不到文件. (范围问题?)

PHP:数组的等边

致命错误:未捕获 Google_Exception:客户端机密 JSON 文件无效.在 /somepath/lib/vendor/google/apiclient/src/Google/Client.php

Firefox 115 会话行为:为什么页面无法找到要加载的现有会话 ID?

防止使用woocommerce_checkout_process下单

使用其ID查询和捕获与订单相关的其他详细信息的shopware 6方法

将Shopware 6后端中定义的值导入MultiFilter

Laravel auth()->id() 在生产服务器中不工作

php/html OnSubmit 禁用输入类型提交未给出预期结果