我正在尝试创建一个自定义的短代码,它允许我显示库存水平等于或高于设定数值的产品。
所以理想情况下,我可以沿着以下行放置一个短代码:
[产品类别=“秘密战争”stock=“3”]
它将以3种或更多的库存显示“秘密战争”类别的产品。
更新第1号:
所以我找到了一些loictheaztec的代码,我希望我可以在这里修改我的需求。
Display WooCommerce products with a shortcode using a custom meta_query
我已尝试进行了一些更改,但无法使代码正常工作,以下是我当前拥有的代码:
if( ! function_exists('minimum_stock') ) {
// Add Shortcode
function minimum_stock( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'columns' => '5',
'limit' => '40',
'stock' => '3',
),
$atts, 'minimum_stock'
);
$woocommerce_loop['columns'] = $atts['columns'];
// The WP_Query
$products = new WP_Query( array (
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $atts['limit'],
'meta_query' => array(
'stock' => array(
'key' =>'_stock',
'type' => 'numeric',
'value' => ''
'compare' => '>=',
),
)
));
ob_start();
if ( $products->have_posts() ) { ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "<p>There are no results.</p>"
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'stock', 'minimum_stock' );
}
我也尝试过用一种稍微不同的方式来处理它:
// Add Minimum Stock Shortcode
function minimum_stock_func ( $atts , $args ) {
$a = shortcode_atts( array(
'stock' => $args = array(
'meta_key' => '_stock',
'type' => 'numeric',
'meta_value' => '',
'compare' => '>='
), $atts ) );
return "stock = {$a['stock']}";
}
add_shortcode( 'stock', 'minimum_stock_func' );
如果有人知道我做错了什么,你的帮助将非常感谢!
更新第2号:
好吧,所以我想我要去那里…
我已经设法用这个代码获得了至少3个库存:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $product, $woocommerce, $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
),
$atts, 'minimum_stock'
);
$woocommerce_loop['columns'] = $atts['columns'];
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => array(
array(
'key' => '_stock',
'value' => 3,
'compare' => '>='
)
),
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
$loop = new WP_Query($args);
ob_start();
woocommerce_product_loop_start();
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
这将显示最小库存量为3的类别中的产品,并显示以下短代码:
[最小库存类别=“漫画书出版商”]
如果短代码是这样的话,有人知道我怎样才能让它工作吗?
[产品最低库存量=“2”category=“Comic Book Publishers”]
我想能够使用WooCommerce[产品]的短代码,因为它也将得到分页,商店的“orderby”下拉菜单将出现在产品上方。我还想用[最小库存量]来指定最小库存量,而不是用短代码库存来设置。
任何帮助都将不胜感激。
亲切的问候,
日本药典