
WooCommerce で 在庫がある商品の一覧を表示するショートコード
本記事にはアフィリエイト広告が含まれます。
目次
ショートコード
全文
特定のカテゴリ&在庫がある商品
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48  | 
						// 販売中の曲だけの表示 function get_instock_products() { 	global $post; 	$args = array( 		'post_type'             => 'product', 		'post_status'           => 'publish', 		'ignore_sticky_posts'   => 1, 		'posts_per_page'        => -1, 		'tax_query'             => array( 			array( 				'taxonomy'      => 'product_cat', 				'field' => 'term_id', //This is optional, as it defaults to 'term_id' 				'terms'         => 51, 				'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. 			), 			array( 				'taxonomy'      => 'product_visibility', 				'field'         => 'slug', 				'terms'         => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 				'operator'      => 'NOT IN' 			) 		), 		'meta_query' => array( 			array( 				'key' => '_stock_status', 				'value' => 'instock' 			), 			array( 				'key' => '_backorders', 				'value' => 'no' 			), 		) 	); 	$products = get_posts($args); 	do_action( 'woocommerce_before_shop_loop' ); 	$html .= woocommerce_product_loop_start(); 	foreach($products as $post): // ループの開始 	setup_postdata($post); // 投稿のセットアップ 	$html .= wc_get_template_part( 'content', 'product' ); 	endforeach; // ループの終了 	$html .= woocommerce_product_loop_end(); 	return $html; } add_shortcode( 'instock_products' , 'get_instock_products' );  | 
					
カテゴリの指定箇所
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14  | 
								'tax_query'             => array( 			array( 				'taxonomy'      => 'product_cat', 				'field' => 'term_id', //This is optional, as it defaults to 'term_id' 				'terms'         => 51, 				'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. 			), 			array( 				'taxonomy'      => 'product_visibility', 				'field'         => 'slug', 				'terms'         => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 				'operator'      => 'NOT IN' 			) 		),  | 
					
在庫の指定箇所
| 
					 1 2 3 4 5 6 7 8 9 10  | 
								'meta_query' => array( 			array( 				'key' => '_stock_status', 				'value' => 'instock' 			), 			array( 				'key' => '_backorders', 				'value' => 'no' 			), 		)  | 
					
ページ分割がされていない等色々課題はありますが
なんとか在庫商品のみの表示ページが出来ました・・
全商品一覧とは別に、在庫有りの商品だけを表示したかったので色々探したのですが、一筋縄ではいかない感じですね・・
理想的にはチェックボックスなどで「在庫ありの商品だけを表示する」が出来たら良いのですがそれは無理そうかな・・