微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于购物车项目数的WooCommerce中特定运输类别的购物车消息

如何解决基于购物车项目数的WooCommerce中特定运输类别的购物车消息

这是根据Cart Message for a Specific Shipping Class in WooCommerce答案和Show or hide shipping methods based on number of cart items for specific shipping class答案改编而成的。

这是我的代码尝试:


add_action( 'woocommerce_before_calculate_totals','cart_items_shipping_class_message',20,1 );
function cart_items_shipping_class_message( $cart ){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $shipping_class_id = '150'; // Your shipping class Id
    
    $allowed_max_qty     = 4; // Max allowed quantity for the shipping class
      
    $related_total_qty   = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
            $related_total_qty += $cart_item['quantity'];
        }
    }
    
    // When total allowed quantity is more than allowed (for items from defined shipping classes)
    if ( $related_total_qty > $allowed_max_qty ) {
            wc_clear_notices();
            wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates,remove Beers from the Cart.','woocommerce'),'notice' );
            break;
        }
    }
}


但是它似乎没有用。有什么想法吗?

更新:我开始工作了。这是代码


add_action( 'woocommerce_before_calculate_totals','notice' );
        }
}


解决方法

工作正常。


add_action( 'woocommerce_before_calculate_totals','cart_items_shipping_class_message',20,1 );
function cart_items_shipping_class_message( $cart ){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $shipping_class_id = '150'; // Your shipping class Id
    
    $allowed_max_qty     = 4; // Max allowed quantity for the shipping class
      
    $related_total_qty   = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
            $related_total_qty += $cart_item['quantity'];
        }
    }
    
    // When total allowed quantity is more than allowed (for items from defined shipping classes)
    if ( $related_total_qty > $allowed_max_qty ) {
            wc_clear_notices();
            wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates,remove Beers from the Cart.','woocommerce'),'notice' );
        }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。