当前位置: 首页 > 知识库问答 >
问题:

提取与WooCommerce中的类别相关的品牌列表

孟鸿朗
2023-03-14

在WooCommerce中,我的产品被归类为一个(或多个)类别(product_cat),并且具有品牌属性(pa_marque),并且只在一个品牌中。

我正试图提取与某个类别相关的品牌列表。。。

通过SQLJOIN(这可能不是最好的解决方案)term_relationships,term_taxonomy,条款和帖子表我得到这样的东西:

{"post_id":"23391","term_taxonomy_id":"1217","taxonomy":"product_cat","taxonomy_parent":"0"},
{"post_id":"23391","term_taxonomy_id":"1219","taxonomy":"product_cat","taxonomy_parent":"1217"},
{"post_id":"23391","term_taxonomy_id":"1943","taxonomy":"pa_marque","taxonomy_parent":"0"}

(即产品23391有两个产品类别和一个品牌…但我真的不知道如何继续…)

你有什么线索吗?

共有1个答案

洪经义
2023-03-14

我发现Get WooCommerce产品类别包含一个特定的产品品牌答案代码,我对代码做了一些小修改,如下所示:

// Inspired by https://stackoverflow.com/a/61624358/1256770
function get_taxovalues_in_another_taxo_from_a_product_categories($taxonomy_tofind, $taxonomy_known, $cat_term_slug)
{
    global $wpdb;
    $results = $wpdb->get_results("
    SELECT DISTINCT
        t1.*
     FROM    {$wpdb->prefix}terms t1
        INNER JOIN {$wpdb->prefix}term_taxonomy tt1
            ON  t1.term_id = tt1.term_id 
        INNER JOIN {$wpdb->prefix}term_relationships tr1
            ON  tt1.term_taxonomy_id = tr1.term_taxonomy_id
        INNER JOIN {$wpdb->prefix}term_relationships tr2
            ON  tr1.object_id = tr2.object_id
        INNER JOIN {$wpdb->prefix}term_taxonomy tt2
            ON  tr2.term_taxonomy_id = tt2.term_taxonomy_id         
        INNER JOIN {$wpdb->prefix}terms t2
            ON  tt2.term_id = t2.term_id
    WHERE
        tt1.taxonomy = '$taxonomy_tofind'
        AND tt2.taxonomy = '$taxonomy_known'
        AND  t2.slug = '$cat_term_slug'
    ORDER BY t1.name
    ");

    $return = [];
    if (!empty($results)) {
        $term_names = [];
        foreach ($results as $result) {
            $term_link = get_term_link(get_term($result->term_id, $taxonomy_tofind), $taxonomy_tofind);
            $term_names[] = '<a class="' . $result->slug . '" href="' . $term_link . '">'
                . $result->name . '</a>';
        }
        $return = $term_names;
    }
    return $return;
}
// searching 'pa_marque' associated to product_cat "aiguilles-et-crochets"
$brands = get_taxovalues_in_another_taxo_from_a_product_categories('pa_marque', 'product_cat', 'aiguilles-et-crochets');
print(implode(" - ", $brands));

// searching 'pa_epaisseur-laine' associated to product_cat "laines"
$brands = get_taxovalues_in_another_taxo_from_a_product_categories('pa_epaisseur-laine', 'product_cat', 'laines');
print(implode(" - ", $brands));
 类似资料:
  • 我有一个名为“产品类型”的WooCommerce产品类别,我试图列出该类别下的所有类别,但不是这些类别的子类别。例如,我有: 产品类型 碳化物米尔斯 钓鱼工具 儿童类别 我希望它列出“电石磨坊”和“打捞工具”,但不是“儿童类别”。 这是我的代码: 但它仍然返回“儿童类别”。我不知道为什么将深度限制为“1”并将“include_children”设置为“false”不能解决这个问题。

  • 在woocommerce中,类别和产品是否可能有相同的基本url?目前,它给我的分类页面一个404,但产品运行良好。 我想做的是,两者都有基本的“商店”类别:demo。com/商店/类别/产品:演示。com/商店/类别/产品名称

  • 对于我的WC产品页面,我需要向body标记添加一个类,以便执行一些自定义样式。这是我为这个创建的函数。。。 …但是我怎么才能拿到猫的身份证呢?

  • 我试图在Woocommerce中显示当前类别下的子类别(而不是子类别等),如以下Web:http://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en 例如,建筑类和密封剂类 密封胶 已经有一个存档产品。php在我的孩子主题下的woocommerce文件夹中。 已经尝试了一些代码,它适用,但这不是我想要的。

  • 我正在使用SQLServer 2008,我迫切需要sql查询或存储过程来显示任何类别或子类别中的顶级品牌。例如,如果我通过Id分类=2,结果集应该通过执行id分类=2及其子类别和子类别的产品计数来显示电子产品中的顶级品牌。如果我通过Id分类=38,结果应该显示手机中的顶级品牌 以下是我数据库中的表格。 类别 品牌 下表打破了类别和品牌表之间的多对多关系 分类目录品牌 产品 关于乘积表的假设 产品只

  • 我所有的WooCommerce产品都被分配了两个类别。第一类称为“所有产品”,所有产品都得到分配。第二类是依赖于产品,可以是电影、书籍、工具等。 由于每个产品都被分配为“所有产品”类别,因此在产品页面上的related products小部件中,您可以看到每个产品,而不仅仅是第二个(更具体的)产品类别。 我试图在functions.php中添加一些代码,这些代码将忽略“All products”作