dtdd25012 2017-10-08 14:02
浏览 42
已采纳

在页面上显示Woocommerce通知

I have created a function to display some products with a shortcode, but the problem I am having is that the errors messages are not shown on that page. For example if some fields are required then it's only displayed on the cart/checkout page.

Here's some of my code:

while ( $query->have_posts() ) : $query->the_post();
global $product;
?>
<div style="border-bottom:thin dashed black;margin-bottom:15px;">
<h2><?php the_title(); ?>  <span><?php echo $product->get_price_html();?></span></h2>
<p><?php the_excerpt();?></p>
<?php global $product;
if( $product->is_type( 'simple' ) ){
woocommerce_simple_add_to_cart();
}

What do I need to add to show the error messages on the page the shortcode is being used on?

  • 写回答

1条回答 默认 最新

  • dsilhx5830 2017-10-08 16:02
    关注

    You need to use dedicated wc_print_notices() function, that displays Woocommerce notices. This function is hooked or used in woocommerce templates for that purpose.

    To make the WooCommerce notice actives in the page of your short code you need to add inside your shortcode this wc_print_notices() function.

    I have reproduced a similar Shortcode as yours below (for testing purpose) where woocommerce notices are printed:

    if( !function_exists('custom_my_products') ) {
        function custom_my_products( $atts ) {
            // Shortcode Attributes
            $atts = shortcode_atts( array( 'ppp' => '12', ), $atts, 'my_products' );
    
            ob_start();
    
            // HERE we print the notices
            wc_print_notices();
    
            $query = new WP_Query( array(
                'post_type'      => 'product',
                'posts_per_page' => $atts['ppp'],
            ) );
    
            if ( $query->have_posts() ) :
                while ( $query->have_posts() ) :
                    $query->the_post();
                    global $product;
                ?>
                    <div style="border-bottom:thin dashed black;margin-bottom:15px;">
                    <h2><?php the_title(); ?>  <span><?php echo $product->get_price_html();?></span></h2>
                    <p><?php the_excerpt();?></p>
                <?php
                    if( $product->is_type( 'simple' ) )
                        woocommerce_simple_add_to_cart();
    
                endwhile;
            endif;
            woocommerce_reset_loop();
            wp_reset_postdata();
    
            return '<div class="my-products">' . ob_get_clean() . '</div>';
        }
        add_shortcode( 'my_products', 'custom_my_products' );
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This is tested and works on WooCommerce 3+

    Notes:

    • In your code you are using 2 times global $product;
    • Remember that in a shortcode you never echo or print anything, but you return some output…
    • Don't forget to reset the loop and the query at the end.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据