如何在自定义页面上显示消息wc_print_notice

我有一个来自https://businessbloomer.com/woocommerce-check-product-category-cart/的代码段:

/* 
 * @snippet       Check if Product Category is in the Cart - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72900
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.1.2
 */

add_action('woocommerce_before_cart','bbloomer_check_category_in_cart');

function bbloomer_check_category_in_cart() {

// Set $cat_in_cart to false
$cat_in_cart = false;

// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    // If Cart has category "download",set $cat_in_cart to true
    if ( has_term( 'download','product_cat',$cart_item['product_id'] ) ) {
        $cat_in_cart = true;
        break;
    }
}

// Do something if category "download" is in the Cart      
if ( $cat_in_cart ) {

// For example,print a notice
wc_print_notice( 'Category Downloads is in the Cart!','notice' );

// Or maybe run your own function...
// ..........

}

}

如果购物车中有产品类别,则会显示一条通知,但会通过购物车页面上的wc_print_notice显示消息。我想通过短代码或任何建议在自定义主题页面模板上显示此消息。

xb588172 回答:如何在自定义页面上显示消息wc_print_notice

怎么样:

    // modify the function above
    if ( $cat_in_cart )
    {
        $msg = 'Category Downloads is in the Cart!';
        wc_print_notice( $msg,'notice' );
        return $msg;
    }
    return null;

...

// create a new function for the shortcode
// it can be after the above function
add_shortcode( 'myshortcode','myshortcode_func' );
function myshortcode_func( $atts ) {
    return (($msg = bbloomer_check_category_in_cart()) !== null)
        ? '<div class="msg">' . esc_html ($msg) . '</div>'
        : ''
    ;
}

然后仅在页面内容中添加简码。

[myshortcode]
本文链接:https://www.f2er.com/3158892.html

大家都在问