问题描述
我想将所有产品的属性插入到它们的简短描述中,以便客户可以打开快速查看并检查这些属性.
我已经尝试过这个答案:显示特定的产品属性值在档案类别页面上
还有这个:Woocommerce - 显示单个产品属性(s) 在前端使用短代码
我无法让它发挥作用.我想应该是因为 WooCommerce 更新到了 3.0+ 版
有人知道制作方法吗?
谢谢
Update 3(简单产品的自动化,WC兼容性)
//WC 3+ 和自动化增强的兼容性add_action('woocommerce_shop_loop_item_title', 'custom_attributes_display', 20);函数 custom_attributes_display(){全球$产品;//只针对简单的产品if(!$product->is_type('simple')) 返回;$loop_count = 0;echo '';//获取属性分类 slugs(现已更新和动态)$attributes_taxonomy = $product->get_attributes();//或者设置一个分类slug(键)和名称(值)的索引数组来选择哪些,例如://$attributes_taxonomy = array('pa_nopeus' => 'Nopeus', 'pa_liito' => 'Liito, 'pa_vakaus' => 'Vaukaus' );foreach( $attributes_taxonomy as $taxonomy => $attribute ) {//获取属性的术语名称(如果有多个值,则在逗号分隔的字符串中设置)$attribute_terms = wp_get_post_terms( get_the_id(), $taxonomy, array( 'fields' => 'names' ) );$terms_string = implode(',', $attribute_terms);//仅当产品的属性存在时才显示if( count( $attribute_terms ) > 0 ){//更新回声 $terms_string;//用|"分隔每个数字(现在更新和动态)$attr_count = count( $attributes_taxonomy );$loop_count++;if( $loop_count < $attr_count && $attr_count > 1 ) echo ' |';}}回声'</div>';}<小时>
更新仅适用于 WooCommerce 3.0+ 版.
//适用于 WooCommerce 3.0+ 版(仅限)add_action('woocommerce_shop_loop_item_title', 'custom_attributes_display', 20);函数 custom_attributes_display(){//仅用于产品类别档案页面如果(is_product_category()){全球$产品;//属性名称数组$attribute_names = array('pa_nopeus', 'pa_liito', 'pa_vakaus', 'pa_feidi');foreach( $attribute_names as $key => $attribute_name ) {//对于 WooCommerce 3.0+ 版$product_id = $product->get_id();//厕所 3.0+//获取属性的值(适用于 WC 3.0+)$wc_term = wc_get_product_terms( $product_id, $attribute_name);$attribute_value = array_shift($wc_term);//仅当产品的属性存在时才显示if(!empty($attribute_value) || '0' == $attribute_value ){//更新回声 $attribute_value;//用/"分隔每个数字if($key < 3) echo '/';}}}}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
它现在应该可以在 WC 3.0+ 中运行
<小时>与此答案相关的代码:Display档案类别页面上的特定产品属性值
I would like to insert the attributes from all products to their short description, so the client can open a quickview and check this attributes.
I already tried this answer: Display specific product attribute values on archives category pages
Also this one: Woocommerce - Display single product attribute(s) with shortcodes in Frontend
And I wasn't able to make it work. I think it should be because WooCommerce got updated to version 3.0+
Does anyone know a way to make it?
Thanks
解决方案 Update 3 ( Automation for simple products, WC compatibility )
// Compatibility for WC 3+ and automation enhancements
add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
function custom_attributes_display(){
global $product;
// Just for simple products
if( ! $product->is_type( 'simple' ) ) return;
$loop_count = 0;
echo '<div>';
// Get the attributes taxonomy slugs (Updated and dynamic now)
$attributes_taxonomy = $product->get_attributes();
// OR set an indexed array of taxonomy slug (key) and name (value) to chose which ones, like:
// $attributes_taxonomy = array('pa_nopeus' => 'Nopeus', 'pa_liito' => 'Liito, 'pa_vakaus' => 'Vaukaus' );
foreach( $attributes_taxonomy as $taxonomy => $attribute ) {
// Getting the term names of an attribute (set in a coma separated string if many values)
$attribute_terms = wp_get_post_terms( get_the_id(), $taxonomy, array( 'fields' => 'names' ) );
$terms_string = implode( ',', $attribute_terms );
// Displays only if attribute exist for the product
if( count( $attribute_terms ) > 0 ){ // Updated
echo $terms_string;
// Separating each number by a " | " (Updated and dynamic now)
$attr_count = count( $attributes_taxonomy );
$loop_count++;
if( $loop_count < $attr_count && $attr_count > 1 ) echo ' | ';
}
}
echo '</div>';
}
Update For WooCommerce version 3.0+ only.
// For WooCommerce Version 3.0+ (only)
add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
function custom_attributes_display(){
// Just for product category archives pages
if(is_product_category()){
global $product;
// the array of attributes names
$attribute_names = array('pa_nopeus', 'pa_liito', 'pa_vakaus', 'pa_feidi');
foreach( $attribute_names as $key => $attribute_name ) {
// For WooCommerce version 3.0+
$product_id = $product->get_id(); // WC 3.0+
// Getting the value of an attribute (OK for WC 3.0+)
$wc_term = wc_get_product_terms( $product_id, $attribute_name);
$attribute_value = array_shift($wc_term);
// Displays only if attribute exist for the product
if(!empty($attribute_value) || '0' == $attribute_value ){ // Updated
echo $attribute_value;
// Separating each number by a " / "
if($key < 3) echo ' / ';
}
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
It should work now in WC 3.0+
Related to this Answer code: Display specific product attribute values on archives category pages
这篇关于在 WooCommerce 3.0+ 中为简短描述添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20
php开发问题
168
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23
php开发问题
287
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23
php开发问题
11
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23
php开发问题
4
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23
php开发问题
17
热门文章
1nohup:忽略输入并将输出附加到“nohup.out"
2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET
3如何将 LDAP 时间戳转换为 Unix 时间戳
4不推荐使用常量 FILTER_SANITIZE_STRING
5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动
6PHP通过phpspreadsheet导入Excel日期数据处理方法
7Analytics API 返回:错误请求 - invalid_grant
8“tlsv1 警报内部错误"握手时
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)