簡體   English   中英

如何在自定義頁面上顯示消息 wc_print_notice

[英]How to display message wc_print_notice on a custom page

我有一個從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顯示消息。 我想通過短代碼或任何建議在自定義主題頁面模板上顯示此消息。

怎么樣:

    // 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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM