<?php

error_reporting(E_ALL);



define( 'WP_USE_THEMES', false );

require( '../wp-load.php' );



$productId = wc_get_product_id_by_sku($_GET['sku']);



if ($productId) {

	$product = wc_get_product($productId);



	switch ($_GET["action"]) {

		case 'brand':



			$term_name = $_GET['brand'];

			$term_id = 0;



	        if ($term = get_term_by( 'name', $term_name, "product_brand" )) {

	        	$term_id = $term->term_id;

	        } else {

	        	$term = wp_insert_term($term_name, "product_brand");

	        	if (! is_wp_error($term)) {

	        		$term_id = $term["term_id"];

	        	}

	        }







	        wp_set_post_terms($productId, [$term_id], "product_brand");



	        echo json_encode(['result'=> 1, "productId" => $productId, "termId" => $term_id, "brand" => $term_name]);

			break;

		

		default:

			// code...

			break;

	}

} else {

	echo json_encode(['result'=> 0]);

}