0

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:

terms issue

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?

1 Answer 1

0

Looks to be a PHP issue and not drupal. You're not clearing your $tid_1,$tid_2,$tid_3 values within your for loop

So when $data->StkGrupKodu1 isn't set for example the value of $tid_1 is still that of the last time $data->StkGrupKodu1 did exist.

You could declare your $tid_1,$tid_2,$tid_3 variables at the top of your foreach loop so they get set similar to what you're doing if the database results null

3
  • Shouldn't $tid_1 = (!empty($tid_1[0]))?$tid_1[0]:""; be doing exactly that?
    – Kartagis
    Commented May 7, 2019 at 9:00
  • But that would only happen if $data->StkGrupKodu1 exists. Think through the steps and what would $tid_1 be if that $data->StkGrupKodu1 didn't exist which would mean you'd never get into that if statement for that loop
    – Leigh
    Commented May 7, 2019 at 9:04
  • Gotcha. Trying that now. Thank you :)
    – Kartagis
    Commented May 7, 2019 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.