首页 文章

WooCommerce自动添加Parent属性

提问于
浏览
1

我有一个带有自动导入产品的WooCommerce网站,每周刷新一次 . 这些产品 attributes 名为 COLORSIZE .

但是, COLOR red 具有这些子属性(例如):

  • 深红色

  • 可乐红

  • 彪马红

通常,我必须手动添加并将这些子属性分配给“父RED”属性,这是很多工作 .

我想知道是否有办法自动执行此操作?

例如,如果属性包含单词RED,则将其分配给父属性RED .

谢谢 .

1 回答

  • 2

    这可以看作一个奇怪的答案 . 首先,它应该可以帮助您了解WordPress / WooCommerce数据库中的情况,如果您了解有关数据库SQL或MySQL的信息,它可能是一种很好的选择 .

    首先解释如何在数据库中设置WooCommerce属性和子属性:

    • 创建属性时,它将在DB表 wp_woocommerce_attribute_taxonomies 上运行 .
    attribute_name  attribute_label attribute_id    attribute_type  attribute_orderby   attribute_public
    
    color           color           1               select          menu_order          0
    
    • 每个子属性都在DB表 wp_terms 中(因此它们是 terms ) . 这里我有3个子属性: 'black''blue''green'
    term_id     name        slug        term_group
    
    8           Black       black       0
    9           Blue        blue        0
    10          Green       green       0
    
    • 此主要属性与其子属性之间的关系位于DB表 wp_term_taxonomy 中 .
      现在假设你的主属性的slu is是 'color' .
      然后对于每个子属性 term_id ,你将有一个以 'pa_ (即 p roduct a ttribute)开头的slug字符串主要属性slug color .
      所以你最终会得到: 'pa_color'
      (希望这很清楚) .

    现在,在此表中,您有一个 'count' 列,用于计算与此子类别 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
    
    • 在数据库表 wp_term_relationships 中,您将找到产品之间的关系( object_id 列,例如由变体使用)和子属性( term_taxonomy_id 列) . 正如您在下面的示例中看到的,子属性由2个产品( object_id )使用:
    object_id   term_taxonomy_id        term_order
    
    22          8                       0
    40          8                       0
    40          9                       0
    22          10                      0
    

    SQL解决方案:如果您可以更改数据库表,那么要更改的有用表是wp_term_taxonomy DB表 . 在wp_terms数据库表中搜索所有子属性term_id . 将它们与wp_term_taxonomy DB表中的现有term_id进行比较为wp_term_taxonomy中的所有非现有term_id创建,并为每个term_id创建正确的“taxonomy”slug . 这可以通过PHP进行SQL查询(您需要根据您的特定需求进行微调) .

    我希望这会对你有所帮助 .

    一些有用的线程,与SQL wp_term_taxonomy 相关:

    wordpress woocommerce php sql mysql database tables

相关问题