基于 WooCommerce 的动态下拉菜单 Categories/Sub-Categories
Dynamic Dropdowns based on WooCommerce Categories/Sub-Categories
我正在尝试创建一个包含 WooCommerce 类别和子类别的动态下拉列表组。我很难思考如何实现这一目标。我知道 woocommerce 有可以是下拉菜单的小部件,但这不是我要找的。我已经创建了一个示例 here 来展示我想要实现的目标...这个示例是从 JSON 文件中提取猫和子猫。
我还创建了这张图片来展示我想要的...
我不是在寻找代码解决方案,只是想了解我可以从 WooCommerce 的 API 中获取什么,这样我就可以有一个起点。我不太熟悉 WooCommerce 的 API 以及它能够实现的目标,所以这对我来说是未知领域。
任何人有任何反馈,我将不胜感激。
谢谢。
塞尔吉
这是一个仅使用 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 个 category/subcategories 级别,类似于此摘录中的内容:
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/
)
)
)
)
Assuming that you will be able to dispatch the necessary data in a JSON formated data array like in your linked live dropdowns example.
我正在尝试创建一个包含 WooCommerce 类别和子类别的动态下拉列表组。我很难思考如何实现这一目标。我知道 woocommerce 有可以是下拉菜单的小部件,但这不是我要找的。我已经创建了一个示例 here 来展示我想要实现的目标...这个示例是从 JSON 文件中提取猫和子猫。
我还创建了这张图片来展示我想要的...
我不是在寻找代码解决方案,只是想了解我可以从 WooCommerce 的 API 中获取什么,这样我就可以有一个起点。我不太熟悉 WooCommerce 的 API 以及它能够实现的目标,所以这对我来说是未知领域。
任何人有任何反馈,我将不胜感激。
谢谢。
塞尔吉
这是一个仅使用 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 个 category/subcategories 级别,类似于此摘录中的内容:
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/
)
)
)
)
Assuming that you will be able to dispatch the necessary data in a JSON formated data array like in your linked live dropdowns example.