[英]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 - 解決方案基於他的回答,稍作改動:
set_option
-> update_option
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();
}
}
首先,訂單 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 文件。 它應該有效。
更改運費的一種簡單方法是:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.