简体   繁体   English

在单个产品页面中显示特定产品属性值的相关产品

[英]Display Related products for a specific product attribute value in single product pages

In WooCommerce, I have set a COLLECTION_ID product attribute for my products.在 WooCommerce 中,我为我的产品设置了COLLECTION_ID产品属性。

I would like to display all related products to this COLLECTION_ID product attribute value on single product pages.我想在单个产品页面上显示与此COLLECTION_ID产品属性值相关的所有产品。 Any help?有什么帮助吗?

You will have to add your own settings in the "Settings section" inside the function.您必须在功能内的“设置部分”中添加您自己的设置。 For the defined product attribute name, this function will take the first corresponding term set for the current product (as an attribute can have many terms in a product) and will display all similar products just before "Upsells products" and "Related products".对于定义的产品属性名称,此函数将取当前产品的第一个对应术语集(因为一个属性可以在一个产品中包含多个术语),并在“Upsells products”和“Related products”之前显示所有类似产品。

The code代码

add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){

    ## --- YOUR SETTINGS --- ##

    $attribute = "Color"; // <== HERE define your attribute name
    $limit     = "3";     // <== Number of products to be displayed
    $cols      = "3";     // <== Number of columns
    $orderby   = "rand";  // <== Order by argument (random order here)

    ## --- THE CODE --- ##

    global $post, $wpdb;

    // Formatting the attribute
    $attribute = sanitize_title( $attribute );
    $taxonomy  = 'pa_' . $attribute;

    // Get the WP_Term object for the current product and the defined product attribute
    $terms = wp_get_post_terms( $post->ID, $taxonomy );
    $term = reset($terms);

    // Get all product IDs that have  the same product attribute value (except current product ID)
    $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}'" );

    // Convert array values to a coma separated string
    $ids = implode( ',', $product_ids );

    ## --- THE OUTPUT --- ##

    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>';
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据特定产品属性值显示 WooCommerce 相关产品 - Display WooCommerce related products based on specific product attribute value 获取特定产品属性并将其显示在Woocommerce单一产品页面中 - Get specific product attribute and display it in Woocommerce Single Product Pages 显示Woocommerce中按产品属性过滤的相关产品 - Display related products filtered by product attribute in Woocommerce Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce 更改 WooCommerce 单个产品页面中的副标题“相关产品” - Change subtitle “Related Products” in WooCommerce single product pages 在Woocommerce存档页面中显示特定产品属性 - Display specific product attribute in Woocommerce archive pages 在单个产品页面上显示相关产品中的自定义属性 - Display custom attributes in related products on single product page 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 将特定产品的多个选项卡添加到 WooCommerce 单个产品页面 - Adding multiple tabs for specific products to WooCommerce single product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM