Php 在Woocommerce中以编程方式自定义运费
我想根据woocommerce的购物车数量计算定制运费,我的要求如下: 购物车数量Php 在Woocommerce中以编程方式自定义运费,php,wordpress,woocommerce,shipping,wordpress-plugin-creation,Php,Wordpress,Woocommerce,Shipping,Wordpress Plugin Creation,我想根据woocommerce的购物车数量计算定制运费,我的要求如下: 购物车数量 0-10=>4.99英镑(运费率=固定费率:12) 10-20=>3.99英镑(运费=固定费率:13) 20-30=>2.99英镑(运费=14英镑) 30-40=>1.99英镑(运费=15英镑) 超过40=>免费配送(配送率=17) 注:我们不计算烟草类别的运费。如果有人购买烟草产品,烟草 项目价格不用于运费计算。要做到这一点,所有的烟草项目 添加到免费送货类别(id=150) 设置自定义查询以完成上述要求,但它
add_filter('woocommerce_package_rates','custom_shipping_rates_基于_shipping_class',11,2);
功能自定义装运费率基于装运类别($rates,$package){
if(定义了('DOING'u AJAX'))
返回;
//这里定义要查找的装运类别
$class=数组(150);
//此处定义要更改费率的装运方法
$shipping_rate_id=array('flat_rate:12','flat_rate:13','flat_rate:14','flat_rate:15');
//初始化
$item\u price=$item\u qty=$item\u total=0;
//循环浏览购物车项目
foreach($cart\u item\u key=>$cart\u item的包['contents']{
$item_shipping_class_id=$cart_item['data']->get_shipping_class_id();
如果(!在数组中($item\u shipping\u class\u id,$class)){
$item_price+=$cart_item['data']->get_price();//具有目标装运类别的行项目价格总和
$item_qty+=$cart_item['quantity'];//具有目标装运类别的行项目价格总和
$item\u总计=$item\u价格*$item\u数量;
}
}
//循环运费
foreach($rate作为$rate\u key=>$rate的费率){
if(在数组中($rate\u key,$shipping\u rate\u id)){
如果($item_total>0&&$item_total<10){
$rates[$rate_key]->成本=4.99;
未结算($费率['固定费率:13');
未结算($费率['固定费率:14');
未结算($rates['flat_rate:15']);
未结算($费率['统一费率:17');
}
其他($item_total>10.01&$item_total<20){
$rates[$rate\u key]->成本=3.99;
未结算($rates['flat_rate:12']);
未结算($费率['固定费率:14');
未结算($rates['flat_rate:15']);
未结算($费率['统一费率:17');
}
其他($item_total>20.01&$item_total<30){
$rates[$rate_key]->成本=2.99;
未结算($rates['flat_rate:12']);
未结算($费率['固定费率:13');
未结算($rates['flat_rate:15']);
未结算($费率['统一费率:17');
}
其他($item_total>30.01&$item_total<40){
$rates[$rate\u key]->成本=1.99;
未结算($rates['flat_rate:12']);
未结算($费率['固定费率:13');
未结算($费率['固定费率:14');
未结算($费率['统一费率:17');
}否则{
$rates[$rate\u key]->成本=0;
未结算($rates['flat_rate:12']);
未结算($费率['固定费率:13');
未结算($费率['固定费率:14');
未结算($rates['flat_rate:15']);
}
}
}
返回美元汇率;
}
以上代码并非适用于所有场景,请帮助我。如果您知道每个配送方式的配送费率id值,则意味着在WooCommerce插件(WooCommerce>设置>配送>配送区域>编辑>配送方式)中,您已经创建并分配了配送成本(或免费)对于每个单独的装运方式 所以你不需要再改变每一个的成本 此外,对于免费配送,配送费率id将为
免费配送:17
而不是统一配送费率:17
,除非您创建了零成本配送方法(统一配送费率而不是免费配送) 工作代码为:
add_filter('woocommerce_package_rates', 'change_shipping_method_based_on_cart_total', 11, 2);
function change_shipping_method_based_on_cart_total( $rates, $package ) {
// set the shipping class id to exclude
$shipping_class_id = 150;
// initializes the total cart of products
$total_cart = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
// if the product has the shipping class id equal to "$shipping_class_id" it is excluded from the count
if ( $product->get_shipping_class_id() == $shipping_class_id ) {
continue;
}
$qty = $cart_item['quantity'];
$price = $cart_item['data']->get_price();
// add the product line total to the total
$total_cart += $price * $qty;
}
switch ( true ) {
case $total_cart < 10: // £4.99
// only the "flat_rate:12" shipping method will be enabled
unset($rates['flat_rate:13']);
unset($rates['flat_rate:14']);
unset($rates['flat_rate:15']);
unset($rates['flat_rate:17']); // or "unset( $rates['free_shipping:17'] );"
break;
case $total_cart >= 10 && $total_cart < 20: // £3.99
// only the "flat_rate:13" shipping method will be enabled
unset($rates['flat_rate:12']);
unset($rates['flat_rate:14']);
unset($rates['flat_rate:15']);
unset($rates['flat_rate:17']); // or "unset( $rates['free_shipping:17'] );"
break;
case $total_cart >= 20 && $total_cart < 30: // £2.99
// only the "flat_rate:14" shipping method will be enabled
unset($rates['flat_rate:12']);
unset($rates['flat_rate:13']);
unset($rates['flat_rate:15']);
unset($rates['flat_rate:17']); // or "unset( $rates['free_shipping:17'] );"
break;
case $total_cart >= 30 && $total_cart < 40: // £1.99
// only the "flat_rate:15" shipping method will be enabled
unset($rates['flat_rate:12']);
unset($rates['flat_rate:13']);
unset($rates['flat_rate:14']);
unset($rates['flat_rate:17']);
break;
case $total_cart >= 40: // free
// only the "flat_rate:17" or "free_shipping:17" shipping method will be enabled
unset($rates['flat_rate:12']);
unset($rates['flat_rate:13']);
unset($rates['flat_rate:14']);
unset($rates['flat_rate:15']);
break;
}
return $rates;
}
add_filter('woocommerce_package_rates','change_shipping_method_基于_cart_total',11,2);
功能更改\u运输\u方法\u基于\u购物车\u总计($rates,$package){
//将装运类别id设置为“排除”
$shipping\u class\u id=150;
//初始化产品的总购物车
$total_cart=0;
foreach(WC()->cart->get_cart()作为$cart_项目){
$product=$cart_项目['data'];
//如果产品的装运类别id等于“$shipping\u class\u id”,则不包括在计数中
如果($product->get_shipping_class_id()==$shipping_class_id){
继续;
}
$qty=$cart_物料['qty'];
$price=$cart_item['data']->get_price();
//将产品线总计添加到总计中
$total_cart+=$price*$qty;
}
开关(真){
案例$total_cart<10://4.99英镑
//仅启用“固定费率:12”装运方式
未结算($费率['固定费率:13');
未结算($费率['固定费率:14');
未结算($rates['flat_rate:15']);
未结算($rates['flat_rate:17']);//或“未结算($rates['free_shipping:17'])”
打破
案例$total_cart>=10和$total_cart<20://3.99英镑
//只有“固定费率:13”装运方式将被启用
未结算($rates['flat_rate:12']);
未结算($费率['固定费率:14');
未结算($rates['flat_rate:15']);
未结算($rates['flat_rate:17']);//或“未结算($rates['free_shipping:17'])”
打破
案例$total_cart>=20和$total_cart<30://2.99英镑
//只有“固定费率:14”装运方式将被启用
未结算($rates['flat_rate:12']);
未结算($费率['固定费率:13');
未结算($rates['flat_rate:15']);
未结算($rates['flat_rate:17']);//或“未结算($rates['free_shipping:17'])”
打破
案例$total_cart>=30和$total_cart<40://1.99英镑
//只有“统一费率:15”的装运方式才适用