Woocommerce将许多产品属性的值存储为空字符串。
以下是_product_attributes数据库中显示的内容示例:
a:1:{s:5:"brand";a:6:{s:4:"name";s:5:"brand";s:5:"value";s:0:"";s:11:"is_taxonomy";i:1;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}}
我已在wordpress的产品数据部分正确添加了产品属性。我根本没有编辑过PHP代码。如何让它实际存储正确的产品属性值?
发布于 2019-05-17 14:25:10
产品属性值不存储在wp_post_meta
表中,而是存储在表中wp_term_relationships
。
另外,在查看序列化数据时,有一些奇怪的事情:所有产品属性分类都始于pa_
以下示例:
a:1:{s:8:"pa_color";a:6:{s:4:"name";s:8:"pa_color";s:5:"value";s:0:"";s:8:"position";i:0;s:10:"is_visible";i:0;s:12:"is_variation";i:1;s:11:"is_taxonomy";i:1;}}
要从给定brand
分类的动态产品ID获取值,您可以使用:
$taxonomy = 'brand';
$terms = wp_get_post_terms( $product_id, $taxonomy );
// Loop through WP_Term objects
foreach ( $terms as $term ) {
$term_id = $term->term_id;
$term_name = $term->name;
$term_slug = $term->slug;
}
或者使用WPDB类的SQL查询(来自动态产品ID):
global $wpdb;
$taxonomy = 'brand'; // The taxonomy
$product_id = 37; // The product ID
$terms = $wpdb->get_results( $wpdb->prepare("
SELECT t.term_id, t.name, t.slug
FROM {$wpdb->prefix}terms t
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON t.term_id = tt.term_id
INNER JOIN {$wpdb->prefix}term_relationships tr
ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tt.taxonomy = '%s'
AND tr.object_id = %d
", $taxonomy, $product_id ) );
// Raw output
var_dump($terms);
因此,您可以看到SQL查询使用wp_terms
,wp_term_taxonomy
和wp_term_relationships
。通过wp_term_relationships
列上的表查询产品ID object_id
。
https://stackoverflow.com/questions/-100006743
复制相似问题