亲爱的StackOverflow朋友, 我使用这个Woocommerce短代码
[sale_products per_page =“12”]
仅显示页面上的待售产品。这个短代码完美无缺,只过滤出售的产品,包括简单和可变项目。
现在我想得到相同的结果,但我只需要展示预定的促销产品,换句话说,只展示销售价格安排在其中的产品
Wordpress>产品>一般>销售价格>时间表
我试图找出“正常销售产品”和“预定销售产品”之间的产出差异,但遗憾的是我什么都没注意到。
感谢您的帮助!
=================================
好的,以后参考这里是完整的工作代码。感谢@Palmered解决了我的问题!
我在我的functions.php中添加了这段代码
/* This creates a Woocommerce shortcode for displaying only scheduled on sale products, both simple and variable ones */
function scheduled_sale_products( $atts ) {
global $woocommerce_loop,$wpdb;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
), $atts ) );
// Get products on sale
$product_ids_raw = $wpdb->get_results(
"SELECT posts.ID, posts.post_parent
FROM `$wpdb->posts` posts
INNER JOIN `$wpdb->postmeta` ON (posts.ID = wp_postmeta.post_id)
INNER JOIN `$wpdb->postmeta` AS mt1 ON (posts.ID = mt1.post_id)
WHERE
posts.post_status = 'publish'
AND (mt1.meta_key = '_sale_price_dates_to' AND mt1.meta_value >= ".time().")
GROUP BY posts.ID
ORDER BY posts.post_title ASC LIMIT 0,12");
$product_ids_on_sale = array();
foreach ( $product_ids_raw as $product_raw )
{
if(!empty($product_raw->post_parent))
{
$product_ids_on_sale[] = $product_raw->post_parent;
}
else
{
$product_ids_on_sale[] = $product_raw->ID;
}
}
$product_ids_on_sale = array_unique($product_ids_on_sale);
$meta_query = array();
$meta_query[] = WC()->query->visibility_meta_query();
$meta_query[] = WC()->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
$args = array(
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => $meta_query,
'post__in' => array_merge( array( 0 ), $product_ids_on_sale )
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
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 endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'scheduled_sale_products', 'scheduled_sale_products' );
现在新的Woocommerce短代码已准备好使用
[scheduled_sale_products per_page="12"]
答案 0 :(得分:3)
我的建议是:
将sale_products短代码复制到您的主题(functions.php)或自定义插件(插件文件夹中的php文件)。从/wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php复制名为sale_products的完整功能。它从第560行开始。
第1步:复制
之间的所有内容public static function sale_products( $atts ) {
global $woocommerce_loop;
和
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
进入你的主题或插件。
步骤2:从
更改功能声明public static function sale_products( $atts ) {
到
function scheduled_sale_products( $atts ) {
第3步:将短代码挂钩到wordpress。
add_shortcode( 'scheduled_sale_products', 'scheduled_sale_products' );
第4步:更改查询。
替换此行:
$product_ids_on_sale = wc_get_product_ids_on_sale();
使用:
$product_ids_raw = $wpdb->get_results(
"SELECT posts.ID, posts.post_parent
FROM `$wpdb->posts` posts
INNER JOIN `$wpdb->postmeta` ON (posts.ID = wp_postmeta.post_id)
INNER JOIN `$wpdb->postmeta` AS mt1 ON (posts.ID = mt1.post_id)
WHERE
posts.post_status = 'publish'
AND (mt1.meta_key = '_sale_price_dates_to' AND mt1.meta_value >= ".time().")
GROUP BY posts.ID
ORDER BY posts.post_title ASC LIMIT 0,12");
$product_ids_on_sale = array();
foreach ( $product_ids_raw as $product_raw )
{
if(!empty($product_raw->post_parent))
{
$product_ids_on_sale[] = $product_raw->post_parent;
}
else
{
$product_ids_on_sale[] = $product_raw->ID;
}
}
$product_ids_on_sale = array_unique($product_ids_on_sale);
现在你应该能够使用这样的短代码:
[scheduled_sale_products per_page="12"]