【问题标题】:Dynamic Dropdowns based on WooCommerce Categories/Sub-Categories基于 WooCommerce 类别/子类别的动态下拉列表
【发布时间】:2018-06-18 19:19:24
【问题描述】:

我正在尝试创建一个包含 WooCommerce 类别和子类别的动态下拉列表。我很难弄清楚如何实现这一目标。我知道 woocommerce 具有可以是下拉列表的小部件,但这不是我想要的。我创建了一个示例here,展示了我想要实现的目标...这个示例是从 JSON 文件中提取猫和子猫。

我还创建了这张图片来展示我所追求的...

我并不是在寻找代码解决方案,只是想了解我可以从 WooCommerce 的 API 中获取什么,以便我有一个起点。我对 WooCommerce 的 API 及其能够实现的功能不太熟悉,所以这对我来说是未知领域。

任何人有任何反馈,我将不胜感激。

谢谢。

谢尔盖

【问题讨论】:

    标签: php wordpress woocommerce dropdown categories


    【解决方案1】:

    这是一个仅使用 Wordpress 函数的显式代码示例,它将在多维数组中分层输出所有产品类别的原始必要数据。正如您将看到的,您不需要非常熟悉 WooCommerce API,因为它是 Wordpress 自定义分类法。

    我已经绑定了这个函数来在档案和产品页面中输出你的产品类别原始数据(仅用于测试目的)

    add_action( 'woocommerce_before_main_content', 'get_product_categories_data' );
    function get_product_categories_data(){
        $result = array();
        $taxonomy = 'product_cat';
        $level1_terms = get_terms( array(
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
            'parent' => 0,
        ) );
        foreach($level1_terms as $term1){
            $level2_terms = get_terms( array(
                'taxonomy' => $taxonomy,
                'hide_empty' => false,
                'parent' => $term1->term_id,
            ) );
            $result[$term1->term_id] = array(
                'id' => $term1->term_id,
                'name' => $term1->name,
                'level' => '1',
                'link' => esc_url( get_term_link( $term1->term_id, $taxonomy ) ),
            );
            if(count($level2_terms) > 0){
                foreach($level2_terms as $term2){
                    $level3_terms = get_terms( array(
                        'taxonomy' => $taxonomy,
                        'hide_empty' => false,
                        'parent' => $term2->term_id,
                    ) );
                    $result[$term1->term_id]['childs'][$term2->term_id] = array(
                        'id' => $term2->term_id,
                        'name' => $term2->name,
                        'level' => '2',
                        'link' => esc_url( get_term_link( $term2->term_id, $taxonomy ) ),
                    );
                    if(count($level3_terms) > 0){
                        foreach($level3_terms as $term3){
                            $level4_terms = get_terms( array(
                                'taxonomy' => $taxonomy,
                                'hide_empty' => false,
                                'parent' => $term3->term_id,
                            ) );
                            $result[$term1->term_id]['childs'][$term2->term_id]['childs'][$term3->term_id] = array(
                                'id' => $term3->term_id,
                                'name' => $term3->name,
                                'level' => '3',
                                'link' => esc_url( get_term_link( $term3->term_id, $taxonomy ) ),
                            );
                            if(count($level4_terms) > 0){
                                foreach($level4_terms as $term4){
                                    $result[$term1->term_id]['childs'][$term2->term_id]['childs'][$term3->term_id]['childs'][$term4->term_id]  = array(
                                        $term4->term_id => array(
                                            'id' => $term4->term_id,
                                            'name' => $term4->name,
                                            'level' => '4',
                                            'link' => esc_url( get_term_link( $term4->term_id, $taxonomy ) ),
                                        )
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
        echo '<pre>'; print_r($result); echo '</pre>'; // Raw data output (for testing)
    
        // return $result;
    }
    

    原始输出 - 您将获得 4 个类别/子类别级别,类似于此摘录中的内容:

    Array
    (
        [11] => Array
            (
                [id] => 11
                [name] => Clothing
                [level] => 1
                [link] => http://www.example.com/clothing/
                [childs] => Array
                    (
                        [12] => Array
                            (
                                [id] => 12
                                [name] => Hoodies
                                [level] => 2
                                [link] => http://www.example.com/clothing/hoodies/
                                [childs] => Array
                                    (
                                        [36] => Array
                                            (
                                                [id] => 36
                                                [name] => Hood 1
                                                [level] => 3
                                                [link] => http://www.example.com/clothing/hoodies/hood1/
                                                [childs] => Array
                                                    (
                                                        [85] => Array
                                                            (
                                                                [id] => 85
                                                                [name] => Sub Hood 1
                                                                [level] => 4
                                                                [link] => http://www.example.com/clothing/hoodies/hood1/sub-hood-1/
                                                            )
                                                    )
                                            )
                                        [37] => Array
                                            (
                                                [id] => 37
                                                [name] => Hood 2
                                                [level] => 3
                                                [link] => http://www.example.com/clothing/hoodies/hood2/
                                            )
                                    )
                            )
                        [16] => Array
                            (
                                [id] => 16
                                [name] => T-shirts
                                [level] => 2
                                [link] => http://www.example.com/clothing/t-shirts/
                            )
                    )
            )
    )
    

    假设您将能够像在链接的实时下拉列表示例中那样在 JSON 格式的数据数组中发送必要的数据。

    【讨论】:

    • 谢谢洛伊克。我需要将您的代码放在函数文件中,还是可以放在任何其他文件中?我尝试将它放在函数文件中,但没有看到任何输出。
    • 编辑:我可以在“商店/产品”页面中看到输出,但在其他页面中看不到...有什么特别的原因吗?
    • @Sergio 我用于函数(woocommerce_before_main_content)的钩子仅在产品档案页面和单个产品页面中触发。这就是为什么。仅用于测试和获取原始输出。
    • @Sergio 我已经更新了第四个 foreach 循环中的代码,添加了缺少的 [$term4-&gt;term_id] ... 试试看。它现在可以工作了。
    • 完美。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    相关资源
    最近更新 更多