向 WooCommerce 添加品牌类型可以通过添加自定义分类来实现。以下是步骤及代码示例:
function add_product_category() {
wp_insert_term(
'品牌类型', // 分类名称
'product_cat', // 分类类型(产品分类)
array(
'description' => '品牌类型', // 分类描述
'slug' => 'brand', // 分类slug
)
);
}
add_action( 'init', 'add_product_category' );
function add_brand_to_product() {
global $woocommerce_loop;
if(is_tax('product_cat', 'brand') && $woocommerce_loop['name'] == 'loop') {
$loop_columns = get_option('woocommerce_catalog_columns');
$taxonomy = 'product_cat';
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'orderby' => 'meta_value_num',
'meta_key' => 'total_sales',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $term_id,
'operator'=> 'IN'
),
)
);
$products = new WP_Query($args);
woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
wp_reset_postdata();
}
}
add_action('woocommerce_after_shop_loop', 'add_brand_to_product');
add_action( 'woocommerce_product_options_general_product_data', 'add_brand_type_field' );
function add_brand_type_field()