我正在使用wordpress并使用woocommerce插件工作。在我的单个产品页面上,我想显示相同子类别的相关产品,但无法这样做。
我有一个类别坠落保护,子类别Harness and Belts及其子类别是Work Positioning Belts。在该类别下我有3个产品。我想将这些产品展示为相关产品。
以下是我的相关单品/ related.php
中的代码if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product, $woocommerce_loop;
$related = $product->get_related( $posts_per_page );
if ( sizeof( $related ) == 0 ) return;
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $related,
'post__not_in' => array( $product->id )
) );
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div class="row-fluid">
<div class="related products span9">
<div class="hr"></div>
<h3><?php _e( 'Related Products', 'woocommerce' ); ?></h3>
<?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(); ?>
</div>
答案 0 :(得分:2)
不确定这是否是最佳方式,但我这样做了。
我添加了一个获取相同子类别
的产品的功能function custom_related_products($product){
global $woocommerce;
// Related products are found from category and tag
$tags_array = array(0);
$cats_array = array(0);
// Get tags
$terms = wp_get_post_terms($product->id, 'product_tag');
foreach ( $terms as $term ) $tags_array[] = $term->term_id;
// Get categories
$terms = wp_get_post_terms($product->id, 'product_cat');
foreach ( $terms as $key => $term ){
$check_for_children = get_categories(array('parent' => $term->term_id, 'taxonomy' => 'product_cat'));
if(empty($check_for_children)){
$cats_array[] = $term->term_id;
}
}
// Don't bother if none are set
if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();
// Meta query
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
// Get the posts
$related_posts = get_posts( array(
'orderby' => 'rand',
'posts_per_page' => $limit,
'post_type' => 'product',
'fields' => 'ids',
'meta_query' => $meta_query,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
) );
$related_posts = array_diff( $related_posts, array( $product->id ), $product->get_upsells() );
return $related_posts;
}
然后在related.php中执行此操作
global $product, $woocommerce_loop;
if ( sizeof( $related ) == 0 ) return;
$args = apply_filters('woocommerce_product_related_posts', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => custom_related_products($product),
'post__not_in' => array($product->id)
) );
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?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 do_action('add_all_to_cart_at_once_for_related_products'); ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
我猜你可以在related.php中调用相同的逻辑来使它更简单,但我更喜欢将它保持分离。希望有所帮助!
答案 1 :(得分:1)
上述内容适用于最新的WordPress / WooCommerce(2014年11月),但您需要在/wp-content/themes/my-child-theme/woocommerce/single-product/related.php中覆盖related.php,它需要看起来像这样:
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
$related = $product->get_related( $posts_per_page );
if ( sizeof( $related ) == 0 ) return;
$args = apply_filters('woocommerce_product_related_posts', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => custom_related_products($product),
'post__not_in' => array($product->id)
) );
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2>You Might Also Like...</h2>
<?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(); ?>
</div>
<?php endif;
wp_reset_postdata();
答案 2 :(得分:1)
您需要修改related.php模板文件。 路径:你的主题/ woocommerce /单品/ related.php
将$ args更改为,
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__not_in' => array( $product->id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
) );
其中$ cats_array是,
$cats_array = array(0);
// Get product categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
//Select only the category which doesn't have any children
if( sizeof( $terms ) ){
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
}
并删除/评论以下行
$related = $product->get_related( $posts_per_page );
if ( sizeof( $related ) == 0 ) return;
答案 3 :(得分:0)
我挣扎的时间比我需要的时间长。
找到我最后的related.php文件。您可以将其粘贴到子主题中的通用模板文件中。
<?php
/**
* Related Products
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( 'orderby' => 'rand', 'post__not_in' => array( $post->ID ), 'posts_per_page' => 4, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
if ($r->have_posts()) { ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php
wp_reset_query();
}
}
答案 4 :(得分:0)
为WooCommerce提供了一个很好的解决方案&gt; V3.0.0
只需将 single-product / related.php 文件从WooCommerce模板目录复制到主题目录中的 wocoomerce / templates / single-product / related.php 并复制将以下代码粘贴到复制的related.php文件中:
<?php
if (!defined('ABSPATH')) {
exit;
}
global $post, $product;
if (empty($product) || !$product->exists()) {
return;
}
$subcategories_array = array(0);
$all_categories = wp_get_post_terms($product->id, 'product_cat');
foreach ($all_categories as $category) {
$children = get_term_children($category->term_id, 'product_cat');
if (!sizeof($children)) {
$subcategories_array[] = $category->term_id;
}
}
if (sizeof($subcategories_array) == 1) {
return array();
}
$args = array(
'orderby' => 'rand',
'posts_per_page' => 5,
'post_type' => 'product',
'fields' => 'ids',
'meta_query' => $meta_query,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $subcategories_array
)
)
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()):
?>
<section class="related products">
<h2><?php esc_html_e('Related products', 'woocommerce'); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php
global $post, $product;
$post_object = get_post($product->get_id());
setup_postdata($GLOBALS['post'] = & $post_object);
wc_get_template_part('content', 'product');
?>
<?php endwhile; ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php
endif;
wp_reset_postdata();
答案 5 :(得分:0)
这是我的“ related.php”代码,我从当前产品的wp_term_relationships中获取相关产品,
这对我有用,我在2,3个wordpress网站上申请了相关产品。
* Related Products
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.0.0
*/
global $wpdb;
if(! defined('ABSPATH')) exit; // Exit if accessed directly
$product = lucian_get_global_variables('product');
$woocommerce_loop = lucian_get_global_variables('woocommerce_loop');
$lucian_options = lucian_get_global_variables();
//custom code starts here!! fetch term_taxonomy_id
// $terms = wp_get_post_terms( get_the_id(), 'product_cat' );
// echo '<pre>'; print_r($terms); echo '</pre>';
$cats_array = array();
$product_id = $product->id;
$taxonomy_id = "";
// Get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
//Select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
$taxonomy_id = $cats_array[0];
}
//custom code Ends here!!
$related = wc_get_related_products( $product->get_id(), $limit = $posts_per_page, $exclude_ids = array() );
if(sizeof($related) == 0) return;
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
) );
$products = new WP_Query($args);
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters('woocommerce_related_products_columns', $columns);
//custom code starts here!!!
//fetch Product ids throgh taxonomy id
$results = $wpdb->get_results("SELECT * FROM `wp_term_relationships` WHERE `term_taxonomy_id` = '$taxonomy_id' ");
$tax_id = [];
$count = 1;
// exclude current post id from given array!!
foreach ( $results as $obj ) :
if ($obj->object_id != $product_id) {
if($count <=5)
{
array_push($tax_id,$obj->object_id);
}
$count++;
}
endforeach;
//custom code Ends here!!!
if ( $tax_id ) : ?>
<div class="widget related_products_widget hv vg-icon">
<div class="vg-title"><h3><?php
if(isset($lucian_options['related_icon']) && !empty($lucian_options['related_icon'])) {
echo '<i class="' .esc_attr($lucian_options['related_icon']) .'"></i>';
}?><span><?php echo isset($lucian_options['related_title']) ? esc_html($lucian_options['related_title']) : ''; ?></span></h3></div>
<div class="related products">
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $tax_id as $id ) : ?>
<?php
$post_object = get_post( $id );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</div>
</div>
<?php endif;
wp_reset_postdata();