doubinei1457 2016-07-16 14:16
浏览 81
已采纳

WooCommerce自动添加Parent属性

I have a WooCommerce site with auto-imported products, which refresh them every week. These products have attributes called COLOR and SIZE.

However, the COLOR red has these sub-attributes (for example):

  • Crimson Red
  • Cola Red
  • Puma Red

Normally, I have to manually add and assign these sub-attributes to the "parent RED" attribute and that is a lot of work.

I wonder if there is a way to do this automatically?

For example, IF the attribute contains the word RED, assign it to parent attribute RED.

Thanks.

  • 写回答

1条回答 默认 最新

  • duananyu9231 2016-07-16 16:08
    关注

    This could be see as a strange answer. First it should help you to understand how things are going in WordPress/WooCommerce database and may be this way it could be a good alternative if you understand something about Database SQL or MySQL.

    Here is explained first how WooCommerce attributes and sub-attributes are set in database:

    1. When you create an attribute, it goes on DB table wp_woocommerce_attribute_taxonomies.
    attribute_name  attribute_label attribute_id    attribute_type  attribute_orderby   attribute_public
    
    color           color           1               select          menu_order          0
    
    1. Each sub attribute goes in DB table wp_terms (so they are terms). Here I have 3 sub-attributes: 'black', 'blue' and 'green':
    term_id     name        slug        term_group
    
    8           Black       black       0
    9           Blue        blue        0
    10          Green       green       0
    
    1. The relationships between this main attribute and his sub-attributes is located in DB table wp_term_taxonomy.
      Now imagine the slug of your master attribute is 'color'.
      Then for each sub-attribute term_id you will have a slug string beginning with 'pa_ (that mean product attribute) + the main attribute slug color.
      So you will get finally: 'pa_color'
      (hope this is clear).

      Now in this table you have a 'count' column that counts the number of products related to this sub-category term_id.
    term_taxonomy_id    term_id     taxonomy    description parent  count
    
    8                   8           pa_color                0       2
    9                   9           pa_color                0       1
    10                  10          pa_color                0       1
    
    1. In DB table wp_term_relationships you will find the relationships between products (object_id column, used by variations for example) and sub-attributes (term_taxonomy_id column). As you can see on the example below, the sub-attributes are used by 2 products (object_id):
    object_id   term_taxonomy_id        term_order
    
    22          8                       0
    40          8                       0
    40          9                       0
    22          10                      0
    

    A SQL solution:
    For you if you can alter database tables, the useful table to alter is wp_term_taxonomy DB table.

    1. Search all sub-attributes term_id in wp_terms DB table.
    2. Compare them to existing term_id in wp_term_taxonomy DB table
    3. Create for all non existing term_id in wp_term_taxonomy with the correct 'taxonomy' slug for each one.

    This is possible with SQL queries through PHP (that you will need to fine tune to feet your very specific needs).

    I hope this will help you a bit.

    Some useful threads, related to SQL wp_term_taxonomy :

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型