简体   繁体   English

存储在哪里WooCommerce产品属性数据库中的术语值

[英]Where are stored WooCommerce Product Attributes term values in database

Woocommerce is storing the value of many product attributes as an empty string. Woocommerce将许多产品属性的值存储为空字符串。

Here is an example of what shows up in the database for _product_attributes: 以下是_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;}}

I have properly added product attributes in the product data section in wordpress. 我已在wordpress的产品数据部分正确添加了产品属性。 I have not edited the php code at all. 我根本没有编辑过PHP代码。 How do I get it to actually store the right product attribute values? 如何让它实际存储正确的产品属性值?

The product attribute values are not stored in wp_post_meta table, but in wp_term_relationships . 该产品的属性值不存储在wp_post_meta表,但在wp_term_relationships

Also when looking to your serialized data, there is something weird: All product attributes taxonomy always start with pa_ like in this example: 另外,在查看序列化数据时,还有一些奇怪之处:所有产品属性分类都始于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;}}

To get the values from a dynamic product Id for a given brand taxonomy, you can use: 要从给定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;
}

Or with an SQL query using WPDB class (from a dynamic product ID) : 或者使用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);

So as you can see the SQL query uses wp_terms , wp_term_taxonomy and wp_term_relationships . 因此,您可以看到SQL查询使用wp_termswp_term_taxonomywp_term_relationships The product ID is queried through wp_term_relationships table on object_id column. 通过object_id列上的wp_term_relationships表查询产品ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM