您必须在函数内的“设置”部分添加自己的设置。对于定义的产品属性名称,此函数将采用当前产品的第一个对应术语集
(因为属性在产品中可以有许多术语)
并将在“追加销售产品”和“相关产品”之前展示所有类似产品。
代码
add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){
$attribute = "Color";
$limit = "3";
$cols = "3";
$orderby = "rand";
global $post, $wpdb;
$attribute = sanitize_title( $attribute );
$taxonomy = 'pa_' . $attribute;
$terms = wp_get_post_terms( $post->ID, $taxonomy );
$term = reset($terms);
$product_ids = $wpdb->get_col( "SELECT DISTINCT tr.object_id
FROM {$wpdb->prefix}term_relationships as tr
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'" );
$ids = implode( ',', $product_ids );
echo '<section class="'.$attribute.' '.$attribute.'-'.$term->slug.' products">
<h2>'.__( "Collection", "woocommerce" ).': '.$term->name.'</h2>';
echo do_shortcode("[products ids='$ids' columns='$cols' limit='$limit' orderby='$orderby']");
echo '</section>';
}
代码进入功能。活动子主题(或活动主题)的php文件。已测试并正常工作。