text 检查购物车中是否有任何产品具有特定的运输等级

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 检查购物车中是否有任何产品具有特定的运输等级相关的知识,希望对你有一定的参考价值。

/**
 * Check if the cart has product with a certain Shipping Class
 * @param string $slug the shipping class slug to check for
 * @return bool true if a product with the Shipping Class is found in cart
 */
function cart_has_product_with_shipping_class( $slug ) {
 
    global $woocommerce;
 
    $product_in_cart = false;
 
    // start of the loop that fetches the cart items
 
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
 
        $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_shipping_class' );
 
        if ( $terms ) {
            foreach ( $terms as $term ) {
                $_shippingclass = $term->slug;
                if ( $slug === $_shippingclass ) {
                     
                    // Our Shipping Class is in cart!
                    $product_in_cart = true;
                }
            }
        }
    }
 
    return $product_in_cart;
}


//For example, to check for the Shipping Class ‘free-shipping’, use this:


if ( cart_has_product_with_shipping_class( 'free-shipping' ) ) {
 
    // Yes, the cart has a product with the 'free-shipping' Shipping Class.
 
} else {
 
    // The cart does NOT have a product with the 'free-shipping' Shipping Class.
 
}

以上是关于text 检查购物车中是否有任何产品具有特定的运输等级的主要内容,如果未能解决你的问题,请参考以下文章

根据运输类别和商品数量添加 Woocommerce 费用

如何在 Woocommerce 中检查产品是不是具有特定的产品属性

在 WooCommerce 购物车页面中的每个产品下方添加运输类别

WooCommerce 中特定产品类别的最低购物车数量

如何在 Shopify 中显示元字段

WooCommerce 中特定运输类别、最低金额和地理定位国家/地区的购物车消息