【发布时间】:2014-02-17 18:01:19
【问题描述】:
Woocommerce
除了为每个产品手动添加追加销售外,有没有办法显示同一产品类别中的随机追加销售产品?
提前非常感谢...
【问题讨论】:
标签: woocommerce
Woocommerce
除了为每个产品手动添加追加销售外,有没有办法显示同一产品类别中的随机追加销售产品?
提前非常感谢...
【问题讨论】:
标签: woocommerce
嘿,我遇到了这个线程,试图弄清楚如何做类似的事情。我使用@ViszinisA 提供的大部分查询来调整 up-sells.php 模板。
up-sells.php 从第 23 行开始:
$cats = get_the_terms($product->id, 'product_cat');
if (!count($cats)) {
return;
}
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 5,
'orderby' => 'rand',
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats[0]->term_id
)
)
);
$prodQuery = new WP_Query( $args );
if ( $prodQuery->have_posts() ) {
$postProds = $prodQuery->posts;
?>
<section class="up-sells upsells products">
<h2><?php esc_html_e( 'You may also like…', 'woocommerce' ) ?></h2>
<br>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $postProds as $postProd ) : ?>
<?php
// $post_object = get_post( $upsell->get_id() );
setup_postdata( $GLOBALS['post'] =& $postProd );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php
}
它应该在“wp_reset_postdata();”之前的第 69 行(ish)结束。确保用上面的代码替换这些行之间的内容,而不仅仅是添加它。
记住:此代码将使您设置的任何手动追加销售都不会显示在单个产品页面上。相反,它从当前产品的术语数组中的第一个类别中抽取 5 个随机产品。如果该类别中的产品少于 5 个,则会显示所有产品。
确保在编辑之前将 up-sells.php 复制到您的子主题目录中。
此代码已在 3.0.0 版本上进行了测试并且可以正常工作。快乐编码!
【讨论】:
只需修改模板/woocommerce/single-product/up-sells.php。
$cats = get_the_terms($product->id, 'product_cat');
if (!count($cats)) {
return;
}
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => 'rand',
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats->term_id
)
)
);
$products = new WP_Query( $args );
【讨论】: