获取与WooCommerce产品相关的子类别

时间:2015-12-08 作者:ignaty

我完全被一件似乎很容易的事情所困扰。我希望你能帮我。

每个WooCommerce产品将添加到主母公司“Make”(ID 27)的一个“car brand”子类别和一个“car model”子类别中。

- MAKE
-- CAR BRAND
--- CAR MODEL
--- CAR MODEL
--- CAR MODEL
-- CAR BRAND (selected)
--- CAR MODEL
--- CAR MODEL
--- CAR MODEL (selected)

Category selection screenshot from the admin

如何在产品页面上按层次结构获取这些类别,以便在前端显示为:

MAKE: CAR BRAND
MODEL: CAR MODEL
提前谢谢你救了我的命。

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这可能会适得其反,如果时间失控或完全刺穿轮胎,如果你跑过一层钉子,这将使你的车一事无成。在这里,我们需要考虑一些其他方面,西红柿或自行车可以被标记为1967 Ford Mustang Shelby GT500, 或者品牌将是野马谢尔比GT500和福特,我们不希望这样。

因此,让我们看看这里的计划,以避免上述愚蠢的事情

获取分配给职位的术语(汽车)

确定所有术语的顶级术语是否为Make,就所有目的而言,它将是ID为27的术语

我们需要确定post术语在何处适合层次结构,即术语是子术语还是子术语。为了避免任何其他更深层次的问题,我们将只关注儿童和孙子

一旦我们确定

任何一个学期后的顶级家长都是27岁

而且这个词是指孩子或孙子

然后我们可以根据您的需要输出我们的条款

让我们看看我们将如何做到这一点:(NOTE: 该代码没有经过全面测试,至少需要PHP 5.4。为便于理解,对所有代码进行了注释

您首先需要了解,一篇帖子(post或car)只能有一个孩子和一个孙子学期。如果有更多,将使用最后一个孩子和孙子

function get_car_info( $taxonomy = \'product_cat\', $top_level_parent = 27 )
{
    // Make sure that we have a valid taxonomy before we start. If not, return false
    $taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
    if ( !taxonomy_exists( $taxonomy ) )
        return false;

    // Make sure our $top_level_parent is 27, if not, validate int value
    if ( 27 !== $top_level_parent )
        $top_level_parent = filter_var( $top_level_parent, FILTER_VALIDATE_INT );

    // Now that everything is safe and sanitized/validated, lets get the current post
    global $post;

    // Now we need to get the terms attached to the post (or car)
    $terms = get_the_terms( $post->ID, $taxonomy );
    /**
     * Make sure we actually have terms, if not, return false
     * Just a note, we already made sure the taxonomy is valid, so $terms should never
     * return a WP_Error object
     */
    if ( !$terms )
        return false;

    // Great, we have terms, now the real work starts
    // Set our variables which will store our brand and model
    $brand = \'\';
    $model = \'\';

    // We need to loop through the array of terms ...
    foreach ( $terms as $term ) {
        // ...check if the term is not top level, if so, skip the term
        if ( 0 == $term->parent )
            continue;

        /**
         * Check if the term parent is equal to $top_level_parent, if so, the term is
         * a direct child term, which mean it is the brand, so lets set $brand
         */
        if ( $term->parent == $top_level_parent ) {
            $brand = $term->name; // Adjust as needed, this output the name
            continue; // Lets move on and avoid any unnecessary work
        }

        /**
         * We have reached this point, so our term is not a top level parent or a 
         * direct child from the top level term. This term is a grandchild or even a 
         * lower level child. We now need to know where in the hierarchy this term fits in
         */
        $term_ancestor = get_ancestors( $term->term_id, $taxonomy );
        // Make sure this is a true grandchild, $term_ancestor should only have two values
        if ( 2 != count( $term_ancestor ) )
            continue;

        /**
         * Now that we know the term is a true grandchild, we need to make sure that it
         * is a true grandchild, we actually make sure that it is a grandchild to  $top_level_parent
         */
        if ( $top_level_parent != end( $term_ancestor ) )
            continue;

        /**
         * OK, we have reached the end here, the term is true grandchild of $top_level_parent,
         * so lets set $model
         */
        $model = $term->name;
    } // endforeach $terms

    /**
     * We are nearly done, make sure that we actually have values in $model and $brand
     * before we return any values to avoid silly output
     */
    if (    !$brand
         || !$model
    )
        return false;

    // YEAH!!! We are finally here, lets return our output
    return \'MAKE: \' . $brand . \'</br>MODEL: \' . $model;
} 
在我们了解如何使用它之前,我已经为函数设置了两个参数,

第一个存在$taxonomy 设置为默认woocommerce分类法product_cat

第二个参数调用$top_level_parent 设置为27

这是为了使功能动态化。如果将来分类法或术语父级发生更改,只需将新值传递给函数,而无需修改函数

您还应该添加任何额外的标记等以满足您的确切需要,甚至可以修改现有代码

最后,您可以添加以下内容inside 循环显示汽车的型号和品牌,如果帖子是关于西红柿或自行车的,则不显示任何内容

echo get_car_info();