【问题标题】:Get WooCommerce product categories that contain a specific product brand获取包含特定产品品牌的 WooCommerce 产品类别
【发布时间】:2020-05-05 20:02:38
【问题描述】:

我正在开发一个网上商店,我想要一个显示包含特定品牌产品的产品类别的功能。

我在网上商店卖衣服,然后如果您在标题菜单中选择品牌“Levis”,那么在商店页面的侧边栏中,您应该会看到包含“Levis”品牌产品的所有产品类别.希望有道理。

我一直在寻找解决方案,但我还没有成功。网上某处,它说没有解决方案,所以我必须从头开始开发。

如果需要从头开始开发,您如何最轻松地进行开发?我自己为 Wordpress 开发了一些主题,所以我知道 Wordpress 是如何工作的。只是无法确切地看到如何解决这个问题。

有什么想法吗?

【问题讨论】:

  • 请贴出你试过的代码
  • 我还没有尝试开发任何代码,因为我不知道如何处理它。我也不要求问题的完整解决方案,而只是提供一个线索,如果有人已经找到解决方案或知道在哪里阅读它,其他人将如何解决它。
  • @JørgenCasper 请仍在等待您对以下答案的反馈……

标签: php sql wordpress woocommerce taxonomy-terms


【解决方案1】:

这只能通过使用 WordPress WPDB 类的自定义 SQL 查询来完成。

现在有多个产品品牌插件可用于 WooCommerce,根据您使用的插件,您必须调整正确的分类:

  • product_brand WooCommerce Brands 插件默认在此处在下面的函数中)
  • yith_product_brand 用于 YITH WooCommerce 品牌插件
  • pa_brand 用于自定义产品属性(自制)

您将在下面找到一个自定义函数,该函数根据产品品牌术语slug返回一系列 WooCommerce 产品类别对象:

// The defined taxonomy here in the function $taxonomy argument is for WooCommerce Product brand plugin
function get_product_categories_from_a_product_brand( $brand_term_slug, $taxonomy = 'product_brand' ) {
    global $wpdb;

    return $wpdb->get_results( "
        SELECT 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 = 'product_cat'
        AND tt2.taxonomy = '$taxonomy'
        AND  t2.slug = '$brand_term_slug'
    " );
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


在任何 php 文件中使用示例代码 - 显示产品品牌标签的所有相关链接产品类别名称:

$brand_term_slug = 'levis'; // A term slug is required (not the term id or term name)

$results = get_product_categories_from_a_product_brand( $brand_term_slug );

if( ! empty($results) ) {

    $term_names = []; // Initializing an empty array variable

    // Loop through each product category terms:
    foreach ( $results as $result ) {
        $term_id   = $result->term_id; // Term id
        $term_name = $result->slug; // Term slug
        $term_slug = $result->name; // Term name
        $taxonomy  = 'product_cat'; 

        $term_link = get_term_link( get_term( $result->term_id, $taxonomy ), $taxonomy );

        // Example: Set the linked formatted term name in an array
        $term_names[] = '<a class="'.$result->slug.'" href="'.$term_link.'">'.$result->name.'</a>';
    }   

    // Display the linked formatted terms names
    echo '<div class="brand-categories '.$brand_term_slug.'">'.implode(' ', $term_names).'</div>';
}

【讨论】:

  • 它的工作!很好的解决方案!
猜你喜欢
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
相关资源
最近更新 更多