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 :

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

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题