在 WooCommerce 中每 10 个订单设置自定义运费

Set a custom shipping cost on every 10th Order in WooCommerce

WP 5.3.3

我需要在创建订单后以编程方式更改运费。

此代码不影响:

add_action('woocommerce_new_order', 'custom_shipping_costs',  1, 1);
function custom_shipping_costs($order_id)
{
    $order = wc_get_order($order_id);
    $shippingItems = (array) $order->get_items('shipping');
    foreach ($shippingItems as $item) {
        $item->cost = 0;
    }
    $order->save();
}

有什么帮助吗?

更新 1:

重要的是我需要在每第 n 个订单中更改运费。 像这样:

if ($order_id % 10 == 0) {
    // change shipping price for every 10-th order
}

更新2(解决方案):

感谢@LoicTheAztec - 该解决方案基于 ,稍作改动:

  1. set_option -> update_option
  2. set_total_tax 抛出错误,所以我对这部分进行了一些更改 ()。

最终代码(进入您的活动子主题或活动主题的 functions.php 文件):

// Set a count based on placed orders for shipping items cost change
add_action('woocommerce_checkout_create_order', 'action_wc_checkout_create_order', 10, 2);
function action_wc_checkout_create_order($order, $data)
{
    // get $order count for shipping item change
    $orders_count = (int) get_option('wc_orders_count_for_shipping');

    // Increase count for next order (starting count at 2 as this hook is triggered after shipping items hook)
    update_option('wc_orders_count_for_shipping', $orders_count > 0 ? $orders_count + 1 : 2);
}

// Set shipping cost to zero every 10-th orders when order is placed
add_action('woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4);
function action_wc_checkout_create_order_shipping_item($item, $package_key, $package, $order)
{
    $orders_count = (int) get_option('wc_orders_count_for_shipping');

    // Every 10-th orders 
    if ($orders_count > 0 && ($orders_count % 10) === 0) {
        $item->set_total(0);
        $item->set_taxes(['total' => '0']);
        //$item->set_total_tax('0');
        $item->save();
        $order->calculate_totals();
    }
}

更改运费的简单方法是:

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
    foreach($rates as $key => $rate ) {
        $rates[$key]->cost = 10;
    }
    return $rates;
}

按数量或总价添加条件语句

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {
    $new_cost = ( WC()->cart->subtotal < 10 ) ? 4.5 : 2.5;
    foreach($rates as $key => $rate ) {
        $rates[$key]->cost = $new_cost;
    }
    return $rates;
}

首先,因为 订单 ID 不是连续的,因为它们基于 post ID,该 ID 也用于 Wordpress 页面,posts 和所有其他自定义 post,例如 Woocommerce 产品和优惠券。因此,我们需要对您的 WooCommerce 订单启用顺序计数,以便每 10 个订单进行更改。

在将订单数据保存到数据库之前下订单时,以下将每 10 个订单将运费设置为零:

// Set a count based on placed orders for shipping items cost change
add_action( 'woocommerce_checkout_create_order', 'action_wc_checkout_create_order', 10, 2 );
function action_wc_checkout_create_order( $order, $data ) {
    $orders_count = (int) get_option('wc_orders_count_for_shipping'); // get $order count for shipping item change

    // Increase count for next order (starting count at 2 as this hook is triggered after shipping items hook)
    set_option('wc_orders_count_for_shipping', $orders_count > 0 ? $orders_count + 1 : 2 ); 
}

// Set shipping cost to zero every 10-th orders when order is placed
add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4 );
function action_wc_checkout_create_order_shipping_item( $item, $package_key, $package, $order ) {
    $orders_count = (int) get_option('wc_orders_count_for_shipping');

    // Every 10-th orders 
    if( $orders_count > 0 && ( $orders_count % 10 ) === 0 ) {
        $item->set_total( '0' );
        $item->set_taxes( [ 'total' => '0' ] );
        $item->set_total_tax( '0' );
    }   
} 

代码进入您的活动子主题(或活动主题)的 functions.php 文件。它应该有效。