modules/acm_sku/src/ProductManager.php
private function updateFields($parent, SKU $sku, array $values) {
    $additionalFields = $this->skuFieldsManager->getFieldAdditions();

    // Filter fields for the parent requested.
    $additionalFields = array_filter($additionalFields, function ($field) use ($parent) {
      return ($field['parent'] == $parent);
    });

    // Loop through all the fields we want to read from product data.
    foreach ($additionalFields as $key => $field) {
      $source = isset($field['source']) ? $field['source'] : $key;

      // Field key.
      $field_key = 'attr_' . $key;

      switch ($field['type']) {
        case 'attribute':
          // If attribute is not coming in response, then unset it.
          if (!isset($values[$source])) {
            $sku->{$field_key}->set(0, NULL);
          }
          else {
            $value = $values[$source];
            $value = $field['cardinality'] != 1 ? explode(',', $value) : [$value];
            foreach ($value as $index => $val) {
              if ($term = $this->productOptionsManager->loadProductOptionByOptionId($source, $val, $sku->language()->getId())) {
                $sku->{$field_key}->set($index, $term->getName());
              }
              else {
                $sku->{$field_key}->set($index, $val);
              }
            }
          }
          break;

        case 'string':
          // If attribute is not coming in response, then unset it.
          if (!isset($values[$source])) {
            $sku->{$field_key}->setValue(NULL);
          }
          else {
            $value = $values[$source];
            $value = $field['cardinality'] != 1 ? explode(',', $value) : $value;
            $sku->{$field_key}->setValue($value);
          }
          break;

        case 'text_long':
          // If attribute is not coming in response, then unset it.
          if (!isset($values[$source])) {
            $sku->{$field_key}->setValue(NULL);
          }
          else {
            $value = $values[$source];
            $value = !empty($field['serialize']) ? serialize($value) : $value;
            $sku->{$field_key}->setValue($value);
          }
          break;
      }
    }
  }
private function updateFields($parent, SKU $sku, array $values) {
    $additionalFields = $this->skuFieldsManager->getFieldAdditions();

    // Filter fields for the parent requested.
    $additionalFields = array_filter($additionalFields, function ($field) use ($parent) {
      return ($field['parent'] == $parent);
    });

    // Loop through all the fields we want to read from product data.
    foreach ($additionalFields as $key => $field) {
      $source = isset($field['source']) ? $field['source'] : $key;

      // Field key.
      $field_key = 'attr_' . $key;

      switch ($field['type']) {
        case 'attribute':
          // If attribute is not coming in response, then unset it.
          if (!isset($values[$source])) {
            $sku->{$field_key}->set(0, NULL);
          }
          else {
            $value = $values[$source];
            $value = $field['cardinality'] != 1 ? explode(',', $value) : [$value];
            foreach ($value as $index => $val) {
              if ($term = $this->productOptionsManager->loadProductOptionByOptionId($source, $val, $sku->language()->getId())) {
                $sku->{$field_key}->set($index, $term->getName());
              }
              else {
                $sku->{$field_key}->set($index, $val);
              }
            }
          }
          break;

        case 'string':
          // If attribute is not coming in response, then unset it.
          if (!isset($values[$source])) {
            $sku->{$field_key}->setValue(NULL);
          }
          else {
            $value = $values[$source];
            $value = $field['cardinality'] != 1 ? explode(',', $value) : $value;
            $sku->{$field_key}->setValue($value);
          }
          break;

        case 'text_long':
          // If attribute is not coming in response, then unset it.
          if (!isset($values[$source])) {
            $sku->{$field_key}->setValue(NULL);
          }
          else {
            $value = $values[$source];
            $value = !empty($field['serialize']) ? serialize($value) : $value;
            $sku->{$field_key}->setValue($value);
          }
          break;
      }
    }
  }
private function updateFields ( $ parent , SKU $ sku , array $ values ) { $ additionalFields = $ this -> skuFieldsManager -> getFieldAdditions ( ) ; // Filter fields for the parent requested. $ additionalFields = array_filter ( $ additionalFields , function ( $ field ) use ( $ parent ) { return ( $ field [ 'parent' ] == $ parent ) ; } ) ; // Loop through all the fields we want to read from product data. foreach ( $ additionalFields as $ key => $ field ) { $ source = isset ( $ field [ 'source' ] ) ? $ field [ 'source' ] : $ key ; // Field key. $ field_key = 'attr_' . $ key ; switch ( $ field [ 'type' ] ) { case 'attribute' : // If attribute is not coming in response, then unset it. if ( ! isset ( $ values [ $ source ] ) ) { $ sku -> { $ field_key } -> set ( 0 , NULL ) ; } else { $ value = $ values [ $ source ] ; $ value = $ field [ 'cardinality' ] != 1 ? explode ( ',' , $ value ) : [ $ value ] ; foreach ( $ value as $ index => $ val ) { if ( $ term = $ this -> productOptionsManager -> loadProductOptionByOptionId ( $ source , $ val , $ sku -> language ( ) -> getId ( ) ) ) { $ sku -> { $ field_key } -> set ( $ index , $ term -> getName ( ) ) ; } else { $ sku -> { $ field_key } -> set ( $ index , $ val ) ; } } } break ; case 'string' : // If attribute is not coming in response, then unset it. if ( ! isset ( $ values [ $ source ] ) ) { $ sku -> { $ field_key } -> setValue ( NULL ) ; } else { $ value = $ values [ $ source ] ; $ value = $ field [ 'cardinality' ] != 1 ? explode ( ',' , $ value ) : $ value ; $ sku -> { $ field_key } -> setValue ( $ value ) ; } break ; case 'text_long' : // If attribute is not coming in response, then unset it. if ( ! isset ( $ values [ $ source ] ) ) { $ sku -> { $ field_key } -> setValue ( NULL ) ; } else { $ value = $ values [ $ source ] ; $ value = ! empty ( $ field [ 'serialize' ] ) ? serialize ( $ value ) : $ value ; $ sku -> { $ field_key } -> setValue ( $ value ) ; } break ; } } }
Update attribute fields.

Update the fields based on the values from attributes.

@param string $parent
Fields to get from this parent will be processed.
@param \Drupal\acm_sku\Entity\SKU $sku
The root SKU.
@param array $values
The product attributes/extensions to get value from.

@throws \Drupal\Core\TypedData\Exception\ReadOnlyException
If the data is read-only.
@throws \Drupal\Core\TypedData\Exception\MissingDataException
If the complex data structure is unset and no item can be set.
Update attribute fields .