Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在单个产品页面中显示特定产品属性值的相关产品_Php_Wordpress_Woocommerce_Product_Custom Taxonomy - Fatal编程技术网

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

Php 在单个产品页面中显示特定产品属性值的相关产品,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,在WooCommerce中,我为我的产品设置了COLLECTION\u IDproduct属性 我想在单个产品页面上显示此集合\u ID产品属性值的所有相关产品。有什么帮助吗?您必须在函数中的“设置”部分添加自己的设置。对于定义的产品属性名称,此函数将采用当前产品的第一个对应术语集(因为一个属性在一个产品中可以有多个术语),并将在“Upsells products”和“Related products”之前显示所有类似的产品 代码 add_action( 'woocommerce_after_s

在WooCommerce中,我为我的产品设置了
COLLECTION\u ID
product属性


我想在单个产品页面上显示此
集合\u ID
产品属性值的所有相关产品。有什么帮助吗?

您必须在函数中的“设置”部分添加自己的设置。对于定义的产品属性名称,此函数将采用当前产品的第一个对应术语集(因为一个属性在一个产品中可以有多个术语),并将在“Upsells products”和“Related products”之前显示所有类似的产品

代码

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>';
}
add_action('woocommerce_在单个产品汇总之后,'custom_输出产品集合',12);
函数自定义\输出\产品\集合(){
##---您的设置--##
$attribute=“Color”//slug.“产品”>
“.”收藏“,”woocommerce“:”.$term->名称“;
echo do_短代码(“[products ID='$ids'columns='$cols'limit='$limit'orderby='$orderby']”);
回声';
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好


这是一个很棒的Loic代码段。是否可以修改代码,以便只提取同一产品类别中相同颜色的产品?