我正在尝试创建一个自定义的简码,以允许我显示库存水平等于或高于设定数值的产品。
因此,理想情况下,我可以按照以下方式放置一个简码: [product_category category =“ secret-wars” 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的类别中的产品,并带有以下短代码:
[minimum_stock category =“ comic-book-publishers”]
如果简码是这样的话,有人知道我如何使它工作:
[products minimum_stock =“ 2” category =“ comic-book-publishers”]
我希望能够使用Woocommerce [产品]短代码,因为它也将具有分页功能,并且商店“ orderby”下拉菜单将出现在产品上方。我还希望能够使用[minimum_stock =“ 2”]来指定minimum_stock数量,而不是在简码股票中进行设置。
任何帮助将不胜感激。
亲切的问候, JP
答案 0 :(得分:0)
好的,我已经完成了大部分工作。
当前格式的短代码使我可以在特定类别中显示具有设定的最低库存量的产品,例如[minimum_stock stock =“ 4” category =“ comic-book-publishers”]
代码如下:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
),
$atts, 'minimum_stock'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
现在,我需要弄清楚如何对短代码应用分页,并通过下拉列表包含Woocommerce订单...该开始一个新问题了! :)
如果有人知道我该怎么做,请告诉我,有关此问题的新问题可以在这里找到:How to add woocommerce-pagination and woocommerce-ordering dropdown to custom shortcode