WC_Brands_Admin::parse_brands_field()publicWC 1.0

Parse brands field from a CSV during import.

Based on WC_Product_CSV_Importer::parse_categories_field()

Метод класса: WC_Brands_Admin{}

Хуков нет.

Возвращает

Массив.

Использование

$WC_Brands_Admin = new WC_Brands_Admin();
$WC_Brands_Admin->parse_brands_field( $value );
$value(строка) (обязательный)
Field value.

Код WC_Brands_Admin::parse_brands_field() WC 9.4.2

public function parse_brands_field( $value ) {

	// Based on WC_Product_Importer::explode_values().
	$values    = str_replace( '\\,', '::separator::', explode( ',', $value ) );
	$row_terms = array();
	foreach ( $values as $row_value ) {
		$row_terms[] = trim( str_replace( '::separator::', ',', $row_value ) );
	}

	$brands = array();
	foreach ( $row_terms as $row_term ) {
		$parent = null;

		// WC Core uses '>', but for some reason it's already escaped at this point.
		$_terms = array_map( 'trim', explode( '>', $row_term ) );
		$total  = count( $_terms );

		foreach ( $_terms as $index => $_term ) {
			$term = term_exists( $_term, 'product_brand', $parent );

			if ( is_array( $term ) ) {
				$term_id = $term['term_id'];
			} else {
				$term = wp_insert_term( $_term, 'product_brand', array( 'parent' => intval( $parent ) ) );

				if ( is_wp_error( $term ) ) {
					break; // We cannot continue if the term cannot be inserted.
				}

				$term_id = $term['term_id'];
			}

			// Only requires assign the last category.
			if ( ( 1 + $index ) === $total ) {
				$brands[] = $term_id;
			} else {
				// Store parent to be able to insert or query brands based in parent ID.
				$parent = $term_id;
			}
		}
	}

	return $brands;
}