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

编程入门 行业动态 更新时间:2024-10-26 10:40:56
本文介绍了在单个产品页面中显示相关产品的特定产品属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在WooCommerce中,我为产品设置了 COLLECTION_ID 产品属性。

我想显示单个产品页面上与此 COLLECTION_ID 产品属性值的所有相关产品。

解决方案

您必须在功能内的设置部分中添加自己的设置。对于已定义的产品属性名称,此函数将采用当前产品的第一个对应术语集(因为一个属性在一个产品中可以包含多个术语),并且会在加售产品之前显示所有类似的产品

代码

add_action( 'woocommerce_after_single_product_summary','custom_output_product_collection',12); function custom_output_product_collection(){ ## ---您的设置--- ## $ attribute =颜色; //< ==这里定义您的属性名称 $ limit = 3; //< ==要显示的产品数量 $ cols = 3; //< ==列数 $ orderby = rand; //< ==按参数排序(此处为随机顺序) ## ---代码--- ## global $ post,$ wpdb; //格式化属性 $ attribute = sanitize_title($ attribute); $ taxonomy =‘pa_’。 $ attribute; //获取当前产品的WP_Term对象和已定义的产品属性 $ terms = wp_get_post_terms($ post-> ID,$ taxonomy); $ term = reset($ terms); //获取具有相同产品属性值的所有产品ID(当前产品ID除外) $ product_ids = $ wpdb-> get_col( SELECT DISTINCT tr.object_id 从{$ 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}术语为t ON tt.term_id = t.term_id tt.taxonomy类似于 $ taxonomy且t.term_id ='{$ term-> term_id}'AND tr.object_id!='{$ post- > ID}'); //将数组值转换为逗号分隔的字符串 $ ids = implode(’,’,$ product_ids); ## ---输出--- ## echo'< section class ='。$ attribute。''。$ attribute .'-' 。$ term->产品> < h2>’.__( Collection, woocommerce)。:’。$ term-> name。’< / h2>’; echo do_shortcode( [products ids ='$ ids'columns ='$ cols'limit ='$ limit'orderby ='$ orderby']); echo’< / section>’; }

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。

In WooCommerce, I have set a COLLECTION_ID product attribute for my products.

I would like to display all related products to this COLLECTION_ID product attribute value on single product pages.

解决方案

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".

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). Tested and works.

更多推荐

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

本文发布于:2023-10-12 20:55:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:产品   相关产品   属性   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!