Woocommerce通知を表示する専用wc_print_notices()
機能を使用する必要があります。この関数は、その目的のために、woocommerceテンプレートにフックされるか、または使用されます。
ショートコードのページでWooCommerce通知を有効にするには、ショートコードの中にこのwc_print_notices()
機能を追加する必要があります。
私は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.phpファイルに入ります(またはテーマ)または任意のプラグインファイルでも使用できます。
これはWooCommerce 3+でテストして動作します
注:あなたは2回global $product;
を使用しているあなたのコードで
- ...
- ショートであなたは決してことを忘れないでくださいエコーまたは何かを印刷しますが、返信出力...
- 最後にループとクエリをリセットすることを忘れないでください。