Php 在页面上显示商业通知

Php 在页面上显示商业通知,php,wordpress,woocommerce,shortcode,product,Php,Wordpress,Woocommerce,Shortcode,Product,我创建了一个函数来显示一些带有短代码的产品,但我遇到的问题是错误消息没有显示在该页面上。例如,如果某些字段是必需的,则它仅显示在购物车/结帐页面上 以下是我的一些代码: while ( $query->have_posts() ) : $query->the_post(); global $product; ?> <div style="border-bottom:thin dashed black;margin-bottom:15px;"> <h2>&

我创建了一个函数来显示一些带有短代码的产品,但我遇到的问题是错误消息没有显示在该页面上。例如,如果某些字段是必需的,则它仅显示在购物车/结帐页面上

以下是我的一些代码:

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();
}
while($query->have_posts()):$query->the_post();
全球$产品;
?>

您需要使用专用的功能,显示电子商务通知。为此,此函数在woocommerce模板中被钩住或使用

要使WooCommerce通知在短代码页面中处于活动状态,您需要在短代码中添加此函数

我复制了一个类似的短代码,如下所示(出于测试目的),其中打印了woocommerce通知:

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' );
}
如果(!function_存在('custom_my_products')){
功能自定义\我的\产品($atts){
//短代码属性
$atts=shortcode_atts(数组('ppp'=>'12',),$atts,'my_products');
ob_start();
//我们在这里打印通知
wc_打印通知();
$query=新的WP\U查询(数组(
“post_类型”=>“产品”,
“每页帖子”=>$atts['ppp'],
) );
如果($query->have_posts()):
而($query->have_posts()):
$query->the_post();
全球$产品;
?>