向 WooCommerce 运输类别添加费用,但不包括本地取货
我正在使用在不使用区域或统一费率的情况下向 Woocommerce Shipping Class 添加费用答案代码,这允许我添加如果“危险品”运输类别中的任何产品已添加到购物车,则需支付额外的“费用”。
这很棒,因为它允许我支付与包装/运输相关的额外费用,但是当用户选择“本地取货”运输选项时,它还会添加“费用”。
如果选择“本地取件”,我希望将其从额外费用中排除。
因此我使用:
function fees_fees_fees() {
$shippingClasses['dangerous-goods'] = ['description' => 'Dangerous Goods Fee', 'fee' => 130];
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
foreach($shippingClasses as $key => $val) {
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] ) ) {
WC()->cart->add_fee( __($val['description'], 'woocommerce'), $val['fee'] ); }
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'fees_fees_fees' );
但是,我陷入了下一步。有什么建议吗?
I'm using Add fee to Woocommerce Shipping Class without the use of zones or flat rate answer code, that allows me add an additional 'Fee' if any product from the 'Dangerous Goods' shipping class has been added to cart.
This is great as it allows me to cover the additional costs associated to packaging/shipping, however it is also adding the 'fee' when users select 'Local Pickup' shipping option.
I would love to exclude 'Local Pickup' from the additional fee if selected.
Therefore i use:
function fees_fees_fees() {
$shippingClasses['dangerous-goods'] = ['description' => 'Dangerous Goods Fee', 'fee' => 130];
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
foreach($shippingClasses as $key => $val) {
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] ) ) {
WC()->cart->add_fee( __($val['description'], 'woocommerce'), $val['fee'] ); }
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'fees_fees_fees' );
However, I'm stuck on the next step. Any adivce?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WC()->cart
,因为$cart
已传递给回调函数WC()->session-> ;get( 'chosen_shipping_methods' )
可用于获取选择的运输方式,那么只需添加一个 if 条件即可得到:
WC()->cart
is not necessary, as$cart
is already passed to the callback functionWC()->session->get( 'chosen_shipping_methods' )
can be used to get the chosen shipping method, then it is a matter of adding an if conditionSo you get:
只需将检查添加到 slug 即可。
Simply add check to slug.