向 WooCommerce 运输类别添加费用,但不包括本地取货

发布于 2025-01-10 20:23:43 字数 1020 浏览 0 评论 0原文

我正在使用在不使用区域或统一费率的情况下向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2025-01-17 20:23:43
  • 不需要使用 WC()->cart,因为 $cart 已传递给回调函数
  • WC()->session-> ;get( 'chosen_shipping_methods' ) 可用于获取选择的运输方式,那么只需添加一个 if 条件

即可得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Get chosen shipping method
    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping_method = substr( $chosen_shipping_methods[0], 0, strpos( $chosen_shipping_methods[0], ':' ) );

    // NOT for local pickup
    if ( $chosen_shipping_method != 'local_pickup' ) {
        // Settings
        $shipping_classes['dangerous-goods'] = [ 'description' => 'Dangerous Goods Fee', 'fee' => 130 ];

        // Loop though cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            $shipping_class = get_the_terms( $cart_item['product_id'], 'product_shipping_class' );
    
            foreach ( $shipping_classes as $key => $val ) {
                if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] ) ) {
                    $cart->add_fee( __( $val['description'], 'woocommerce' ), $val['fee'] );
                }
            }
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
  • The use of WC()->cart is not necessary, as $cart is already passed to the callback function
  • WC()->session->get( 'chosen_shipping_methods' ) can be used to get the chosen shipping method, then it is a matter of adding an if condition

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Get chosen shipping method
    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping_method = substr( $chosen_shipping_methods[0], 0, strpos( $chosen_shipping_methods[0], ':' ) );

    // NOT for local pickup
    if ( $chosen_shipping_method != 'local_pickup' ) {
        // Settings
        $shipping_classes['dangerous-goods'] = [ 'description' => 'Dangerous Goods Fee', 'fee' => 130 ];

        // Loop though cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            $shipping_class = get_the_terms( $cart_item['product_id'], 'product_shipping_class' );
    
            foreach ( $shipping_classes as $key => $val ) {
                if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] ) ) {
                    $cart->add_fee( __( $val['description'], 'woocommerce' ), $val['fee'] );
                }
            }
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
[浮城] 2025-01-17 20:23:43

只需将检查添加到 slug 即可。

 if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] )  &&  $shipping_class[0]->slug != 'local_pickup') {

Simply add check to slug.

 if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, [$key] )  &&  $shipping_class[0]->slug != 'local_pickup') {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文