Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce中特定装运类别的购物车消息_Php_Wordpress_Woocommerce_Cart_Notice - Fatal编程技术网

Php WooCommerce中特定装运类别的购物车消息

Php WooCommerce中特定装运类别的购物车消息,php,wordpress,woocommerce,cart,notice,Php,Wordpress,Woocommerce,Cart,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') )

我正在尝试为特定装运类别添加购物车消息。根据答案改编,这是我的代码尝试:

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;
    
    $shipping_class = '150';
   
    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        
        if( $cart_item['data']->get_shipping_class() === $shipping_class ){
        wc_clear_notices();
                
        wc_add_notice( sprintf( 'Beers can only be shipped via <a title="Pallet Shiping" href="/shipping-delivery/#Pallet_Shipping" target="_blank" rel="noopener noreferrer">Pallet Shiping</a>. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.'), 'notice' );
        }
    }
}

add_action('woocommerce_-before_-calculate_-totals','cart_-items_-shipping_-class_-message',20,1);
功能购物车\商品\装运\类别\消息($cart){
if(定义了('DOING'u AJAX'))
返回;
$shipping_class='150';
//循环浏览购物车项目
foreach($cart->get\u cart()作为$cart\u项目){
如果($cart\u item['data']->get\u shipping\u class()==$shipping\u class){
wc_清除通知();
wc_添加_通知(sprintf(‘啤酒只能通过。要显示更便宜的UPS运费,请将啤酒从购物车中取出’,‘通知’);
}
}
}

但它似乎不起作用。有什么想法吗?

在您的案例中,您似乎已经在问题代码中定义了装运类别Id,因此您需要使用
WC\u产品
获取装运类别Id()
方法,如下所示:

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

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        // Check cart items for specific shipping class, displaying a notice
        if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
            wc_clear_notices();
            wc_add_notice( sprintf(
                __('Beers can only be shipped via %s. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
                '<a title="Pallet Shipping" href="/shipping-delivery/#Pallet_Shipping" target="_blank" rel="noopener noreferrer">' . __("Pallet Shipping", "woocommerce") . '</a>'
            ), 'notice' );
            break;
        }
    }
}
据此:

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

谢谢你的解决方案。工作完美。但是,当购物车中显示其他通知时,此通知不显示。有可能在购物车上显示两个通知吗?我想出来了。我有wc_清除通知();在另一段代码中,woocommerce\u before\u calculate\u totals钩子似乎在购物车和结账页面上添加了消息。有没有办法只在购物车页面上显示信息?我尝试使用“woocommerce\u before\u cart”钩子,但它产生了一个致命错误:@WanderlustConsulting我在答案末尾添加了一个附加项。
    if ( ! is_cart() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
        return;