WooCommerce calculates and adds shipping charges based on order quantity
The default shipping rules of WooCommerce are relatively simple, and may not be able to handle complex shipping setup needs, there are some plug-ins that can add customized delivery methods to achieve more flexible shipping setup, such as the more commonly used WooCommerce Weight Based Shipping, which allows us to charge shipping costs based on the weight of the order. In addition to the plugin, we can also utilize the Hook provided by WooCommerce to modify the default delivery method to suit our needs.
比如有这样一个需求:购买 5 件商品之内时(含5件),运费为 12 元,超过 5 件商品时,每增加 5 件,加收运费 4 元(超出部分不足5 件时按5件计算)。
With a little bit of analysis, we can derive the calculation of the shipping cost of an order. Let the number of items in the order be X, and the final shipping cost will be $12 + ceil ((x - 5) / 5) * 4.
Since the formula is simple, we can just revamp WooCommerce's [flat rate] delivery method and add this formula of ours to calculate the order shipping cost.
首先,为统一费率配送方式添加附加运费设置
The code below adds an additional shipping cost setting to WooCommerce's default flat rate delivery method, and the user can set the amount of the increase as appropriate.
add_filter('woocommerce_shipping_instance_form_fields_flat_rate', function ($fields)
{
$fields[ 'cost_addon' ] = [
'title' => __('Shipping cost added for each additional 5 items', 'woocommerce'), 'type' => 'text'
'type' => 'text', 'placeholder' => '
'desc_tip' => true,];
return $fields.
}).;根据运费规则计算运费
有了上面的设置,我们就可以根据运费公式来获取设置,计算运费了,实例代码如下,需要的朋友可以根据自己的具体业务逻辑进行改造。
add_action('woocommerce_flat_rate_shipping_add_rate', function ($flat_rate_class, $rate)
{
global $woocommerce.
$cart = $woocommerce->cart;
$package = $cart->get_shipping_packages();
$quantity = $cart->cart_contents_count;
$basic_cost = $flat_rate_class->get_option('cost');
$addon_cost = $flat_rate_class->get_option('cost_addon');
$addon_qty = $quantity - 5;
if ($addon_qty > 0 && ! empty($addon_cost)) {
$addon_charge_qty = ceil($addon_qty / 5);
$addon_cost = $addon_cost * $addon_charge_qty;
$addon_rate = [
'id' => $flat_rate_class->get_rate_id(),
'label' => __('Flat rate', 'woocommerce'),
'cost' => $basic_cost + $addon_cost,
'taxes' => false, 'calc_tax' => false, 'calc_tax' => false
'calc_tax' => 'per_order', 'meta_data' => false, 'calc_tax' => false
'meta_data' => [],
'price_decimals' => wc_get_price_decimals(), .
].
$flat_rate_class->add_rate($addon_rate);
}
}, 10, 2);In addition to the two Hooks used in this article, WooCommerce also provides similar Hooks to help us modify or add other features to fulfill our needs. However, the use of plug-ins is still the most convenient way, when the default functionality does not meet our needs, first look for a plug-in that can be realized, if not, then check the code, find Hook, write code to achieve the function we want is not too late.
Learn, I usually use plugins as well, they are still a bit more flexible.
Sometimes, installing a feature-rich plugin for a small requirement is unacceptable for people with code cleanliness.
So try to buy paid plugins that are more full-featured. Hahaha
May I ask where this code is written?
function file