显示空的 WooCommerce 属性(字段名称)
Display WooCommerce Attributes (field names) that are empty as well
我正在尝试列出 WooCommerce 中的所有属性(填写或不填写)。
使用此代码:
//show attributes after summary in product single view
add_action('woocommerce_single_product_summary', function() {
//template for this is in storefront-child/woocommerce/single-product/product-attributes.php
global $product;
echo $product->list_attributes();
}, 25);
但这只显示填充的属性,我想显示它们。
请帮忙。
谢谢
1) 在 WooCommerce 3+ 中 WC_Product list_attributes()
方法已弃用。相反,您应该使用函数 wc_display_product_attributes($product)
…
2) 您不能在产品中保存具有空值的属性...
因此,您可能正在查看的是获取所有现有产品属性。如果是这种情况,请按照以下方法进行操作:
function wc_get_all_attributes(){
global $wpdb;
$table_name = $wpdb->prefix . "woocommerce_attribute_taxonomies";
$wc_attributes = $wpdb->get_results("
SELECT attribute_id, attribute_name, attribute_label
FROM $table_name
");
return $wc_attributes;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
用法:
然后您可以在活动主题的任何 php 文件中以多种方式使用它:
例如列出属性名称(标签):
$all_attributes = wc_get_all_attributes();
foreach( $all_attributes as $attribute_obj ){
echo '<p>'.$attribute_obj->attribute_label.'</p>';
}
这将输出所有现有的产品属性。
在每个$attribute_obj
对象中,您将能够得到:
$attribute_obj->attribute_id; // ID
$attribute_obj->attribute_name; // SLUG
$attribute_obj->attribute_label; // LABEL (displayed name)
我正在尝试列出 WooCommerce 中的所有属性(填写或不填写)。
使用此代码:
//show attributes after summary in product single view
add_action('woocommerce_single_product_summary', function() {
//template for this is in storefront-child/woocommerce/single-product/product-attributes.php
global $product;
echo $product->list_attributes();
}, 25);
但这只显示填充的属性,我想显示它们。
请帮忙。
谢谢
1) 在 WooCommerce 3+ 中 WC_Product list_attributes()
方法已弃用。相反,您应该使用函数 wc_display_product_attributes($product)
…
2) 您不能在产品中保存具有空值的属性...
因此,您可能正在查看的是获取所有现有产品属性。如果是这种情况,请按照以下方法进行操作:
function wc_get_all_attributes(){
global $wpdb;
$table_name = $wpdb->prefix . "woocommerce_attribute_taxonomies";
$wc_attributes = $wpdb->get_results("
SELECT attribute_id, attribute_name, attribute_label
FROM $table_name
");
return $wc_attributes;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
用法:
然后您可以在活动主题的任何 php 文件中以多种方式使用它:
例如列出属性名称(标签):
$all_attributes = wc_get_all_attributes();
foreach( $all_attributes as $attribute_obj ){
echo '<p>'.$attribute_obj->attribute_label.'</p>';
}
这将输出所有现有的产品属性。
在每个$attribute_obj
对象中,您将能够得到:
$attribute_obj->attribute_id; // ID
$attribute_obj->attribute_name; // SLUG
$attribute_obj->attribute_label; // LABEL (displayed name)