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

v1uwarro  于 2023-01-12  发布在  PHP
关注(0)|答案(2)|浏览(148)

工作程序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 -该解决方案基于his answer,仅做了少许更改:

  1. set_option -〉update_option
  2. set_total_tax抛出了一个错误,所以我对这部分做了一些修改(This thread helped me)。
    最后的代码(在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();
    }
}
vcirk6k6

vcirk6k61#

第一个订单ID不是连续的因为它们是基于也用于WordPress页面,帖子和所有其他自定义帖子,如Woocommerce产品和优惠券的帖子ID。所以我们需要对您的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文件。它应该工作。

nfs0ujit

nfs0ujit2#

更改运费的简单方法如下:

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;
}

相关问题