【发布时间】:2021-02-17 13:15:21
【问题描述】:
我工作的一家在线商店使用 2 种运输方式:基于尺寸的费率和本地取货。
基于尺寸的费率是默认的送货方式,仅当客户点击点击并取货而不是添加到购物车时才使用本地取货(在内部,产品是在这两种情况下都添加到购物车,但在使用点击取货时,会添加额外的购物车商品数据以表明这一点)。
使用 Click and Pickup 预订所有购物车商品时计算总运费没有问题,因为您可以在运行时使用过滤器 woocommerce_package_rates 过滤运费并更改运输方式,以便它将影响所有购物车项目。
但是,如果购物车包含通过 Click and Pickup 和 add to cart 添加的商品,则计算总运费会变得更加困难,因为运费必须根据购物车项目更改方法,并且不会影响所有购物车项目。
所需流量:
购物车物品: 3
预订 x 2 通过点击和取货预订(成本:0,本地取货运输方式)
纸张 x 1以正常方式添加到购物车,(成本:默认尺寸费率,默认运输方式)
总运费:本地取货费用 + 默认运费
当前代码:
function cust_shipping_rates( $rates, $package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$product_ids_with_local_pickup = array(); // 'click and pickup'
// Loop through line items
foreach( $package['contents'] as $line_item ) {
// Get product id
$product_id = $line_item['product_id'];
if ( $line_item['sofs_cap'] ) { // indication it's reserved with 'Click and pick'
array_push($product_ids_with_local_pickup, $product_id);
}
}
if ( count($product_ids_with_local_pickup) > 0 ) {
foreach ( $rates as $rate_key => $rate ) {
if ( in_array( $rate->method_id, array( 'bring_fraktguiden:5800' ) ) ) { // default shipping method
// how can i exclude the reserved products from being affected by this shipping method?
$rates[$rate_key]->set_cost(/* cost */); // changing this affects ALL items, not just the specific ones
}
}
}
return $rates;
}
add_filter('woocommmerce_package_rates', 'cust_shipping_rates', 999, 2);
【问题讨论】:
-
您需要使用
woocommerce_cart_shipping_packages挂钩将购物车物品拆分为运输包裹,请参阅 this related threads 并尝试为自己做点什么。 -
非常感谢!这就是我一直在寻找的过滤器类型,它会被运费迭代。
-
欢迎您!这并不容易,但它是正确的方法......如果您的代码有任何问题,请在新问题中提供它,添加一些解释和细节......
-
过滤器完成了它的工作。我只需要一种方法来排除一些购物车商品,使其不计入运费。:)
-
您应该使用适合您的代码回答您自己的问题(如果需要,可以进行一些解释),因为它可以帮助其他读者。
标签: wordpress woocommerce hook-woocommerce