Below is the JSON output of a product I am having the issue with:
{
"StkGrupKodu1" : "10090",
"StokAdi" : "7 Parça Kombine Anahtar Takımı",
"StkGrupKodu2" : "",
"SiparisVerilebilir" : true,
"StokMiktari" : 0,
"StkGrupKodu4" : "",
"StokTipiAdi" : "Ticari Mallar",
"KDVOrani" : 18,
"StokTipi" : "00000",
"DovizCinsi" : "EUR",
"Fiyat" : "34.00000",
"StkGrupKodu3" : "",
"StkGrupKodu5" : "",
"PasifStok" : false,
"EskiBilgileri" : {
"EskiStokKodu" : "",
"DegistirmeTarihi" : "1920-04-23T00:00:00"
},
"StokKodu" : "JBM50563"
},
In this e-commerce system, I am consuming a web service and creating corresponding products. With a few products such as the above and a few others, I am having this dreadful issue. As you can see, StkGrupKodu2 and StkGrupKodu3 are empty but when I go to the edit page of the product, it's not empty. You can see from the screenshot below:
This is the code I am using to associate terms with products
foreach ($excel_products as $e) {
$req = \Drupal::httpClient()->get("http://mitapp.mitacan.com/b2b/api/vStkLib/GetStkKodlist?UserCode=B2BUSER&StokKodu=".$e->stokkodu);
$res = $req->getBody();
$res = json_decode($res);
$data = $res->Data[0];
$sid = \Drupal::database()->select("commerce_store_field_data", "csfd")->fields("csfd", ["store_id"])->condition("default_currency", ($data->DovizCinsi == "TRL")?"TRY":$data->DovizCinsi)->execute()->fetchCol();
$product = Product::create(["type" => "default"]);
<snip>
if (!empty($data->StkGrupKodu1)) {
$tid_1 = \Drupal::database()->select("taxonomy_term__field_stok_grup_kodu", "ttfsgk")->fields("ttfsgk", ["entity_id"])->condition("field_stok_grup_kodu_value", $data->StkGrupKodu1)->execute()->fetchCol();
$tid_1 = (!empty($tid_1[0]))?$tid_1[0]:"";
}
if (!empty($data->StkGrupKodu2)) {
$tid_2 = \Drupal::database()->select("taxonomy_term__field_stok_grup_kodu", "ttfsgk")->fields("ttfsgk", ["entity_id"])->condition("field_stok_grup_kodu_value", $data->StkGrupKodu2)->execute()->fetchCol();
$tid_2 = (!empty($tid_2[0]))?$tid_2[0]:"";
}
if (!empty($data->StkGrupKodu3)) {
$tid_3 = \Drupal::database()->select("taxonomy_term__field_stok_grup_kodu", "ttfsgk")->fields("ttfsgk", ["entity_id"])->condition("field_stok_grup_kodu_value", $data->StkGrupKodu3)->execute()->fetchCol();
$tid_3 = (!empty($tid_3[0]))?$tid_3[0]:"";
}
$tids = [$tid_1, $tid_2, $tid_3];
$tids = array_filter($tids);
<snip>
foreach ($tids as $tid) {
$product->field_stok_grup_adi[] = [
"target_id" => $tid,
];
}
<snip>
}
What am I doing wrong or missing?