Php 显示同时为空的属性(字段名)
我正在尝试列出WooCommerce中的所有属性(填写或不填写) 使用此代码:Php 显示同时为空的属性(字段名),php,wordpress,woocommerce,attributes,product,Php,Wordpress,Woocommerce,Attributes,Product,我正在尝试列出WooCommerce中的所有属性(填写或不填写) 使用此代码: //在产品单一视图中汇总后显示属性 添加操作('woocommerce\u single\u product\u summary',函数(){ //此模板位于storefront child/woocommerce/single product/product-attributes.php中 全球$产品; echo$product->list_attributes(); }, 25); 但这只显示填充属性,我想全部
//在产品单一视图中汇总后显示属性
添加操作('woocommerce\u single\u product\u summary',函数(){
//此模板位于storefront child/woocommerce/single product/product-attributes.php中
全球$产品;
echo$product->list_attributes();
}, 25);
但这只显示填充属性,我想全部显示
请帮忙
感谢WooCommerce 3+WC\u产品列表属性()中的1)方法已被弃用。相反,您应该使用函数
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_id; // ID
$attribute_obj->attribute_name; // SLUG
$attribute_obj->attribute_label; // LABEL (displayed name)