Flatsome – Thêm post type bộ sưu tập để chọn trong UX Product

shortcod111

Trong theme Flatsome, shortcode ux_products hỗ trợ nhiều tùy chọn lọc sản phẩm (theo category, tag, featured, on-sale…). Tuy nhiên, nếu bạn muốn có thêm taxonomy tùy chỉnh (ví dụ Bộ sưu tập) để chọn khi thêm UX Block hay UX Builder thì cần làm 3 bước:

// này chép vào functions.php trong theme
add_action( 'init', function() {
    register_taxonomy(
        'bo_suu_tap',
        'product',
        array(
            'label'        => 'Bộ sưu tập',
            'rewrite'      => array( 'slug' => 'bo-suu-tap' ),
            'hierarchical' => true,
            'show_admin_column' => true,
        )
    );
});

👉 Sau khi lưu, trong trang Sản phẩm → sẽ có thêm cột quản lý Bộ sưu tập.
sửa file flatsomeincbuildershortcodesux_products.php


$options['post_options']['options']['bo_suu_tap'] = array( 
  'type' => 'select',
  'heading' => 'Bộ sưu tập',
  'conditions' => 'ids == ""',
  'full_width' => true,
  'default' => '',
  'config' => array(
      'multiple' => true,
      'placeholder' => 'Chọn bộ sưu tập...',
      'termSelect' => array(
          'post_type'  => 'product',
          'taxonomies' => 'bo_suu_tap',
      ),
  )
);

👉 Bây giờ trong UX Builder khi chèn UX Products, bạn sẽ thấy thêm dropdown chọn Bộ sưu tập giống như chọn Tag hay Category.

Thêm đoạn này vào phần xử lý: wp-content/themes/flatsome/inc/shortcodes/ux_products.php
và bạn thêm trong extract($atts = shortcode_atts(array( thêm ‘bo_suu_tap’ => ”,

if (empty($ids)) {

		// Get products
		$atts['products'] = $products;
		$atts['offset'] = $offset;
		$atts['page_number'] = $page_number;
		$atts['cat'] = $cat;

		// Xử lý taxonomy bo_suu_tap (chấp nhận slug hoặc ID, nhiều giá trị ngăn cách bởi ',')
		if (!empty($atts['bo_suu_tap'])) {
			$terms = array_map('trim', explode(',', $atts['bo_suu_tap']));
			// Kiểm tra xem phần tử đầu tiên là số => dùng field term_id, ngược lại dùng slug
			$first = reset($terms);
			$field = is_numeric($first) ? 'term_id' : 'slug';

			// Tạo tax_query (nếu $atts đã hỗ trợ tax_query)
			$tax_query = isset($atts['tax_query']) && is_array($atts['tax_query']) ? $atts['tax_query'] : array();
			$tax_query[] = array(
				'taxonomy' => 'bo_suu_tap',
				'field'    => $field,
				'terms'    => $terms,
				'operator' => 'IN'
			);

			$atts['tax_query'] = $tax_query;
		}

		$products = ux_list_products($atts);
	}

Để sản phẩm hiển thị ra ngoài bạn cần sửa tiếp file structure-wc-helpers.php nằm trong wp-content/themes/flatsome/inc/woocommerce/structure-wc-helpers.php

// Nằm dưới phần tags
$collections = array();
		if (isset($options['bo_suu_tap']) && $options['bo_suu_tap'] !== '') {
			$_cols = array_filter(array_map('trim', explode(',', $options['bo_suu_tap'])));
			// Nếu là số (ID) chuyển sang slug để thống nhất (WP Query có thể dùng slug)
			$collections = array_map(function ($term) {
				if (is_numeric($term)) {
					$t = get_term((int) $term);
					if ($t instanceof WP_Term) {
						return $t->slug;
					}
				}
				return $term;
			}, $_cols);
		}
// Nằm dưới phần Query 
if (! empty($collections)) {
		// đảm bảo tax_query là mảng
		if (! isset($query_args['tax_query']) || ! is_array($query_args['tax_query'])) {
			$query_args['tax_query'] = array();
		}

		$query_args['tax_query'][] = array(
			'taxonomy' => 'bo_suu_tap',
			'field'    => 'slug', // chúng ta đã chuyển ID sang slug ở trên nếu có ID
			'terms'    => $collections,
			'operator' => 'IN',
		);
	}