You are here

public function CommerceSmartImporerService::updateProduct in Commerce Smart Importer 8

Updates entity with given values.

File

src/Plugin/CommerceSmartImporerService.php, line 1780
Main Commerce Smart Importer Service.

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function updateProduct($entity, $fields, $value, $external_folders, ImportingParameters $parameters) {
  $change = FALSE;
  $error_log = [];
  $redirections = [];
  foreach ($fields as $key => $field) {
    if ($field['machine_names'] == 'currency' || $field['field_settings']['read-only']) {
      continue;
    }
    if (!is_numeric($value[$field['index']]) && empty($value[$field['index']])) {
      continue;
    }
    if ($field['field_types'] == 'redirection') {
      $redirections = explode('|', $value[$field['index']]);
      $change = TRUE;
      continue;
    }
    $values = explode('|', $value[$field['index']]);
    $field_log = $this
      ->formatMultipleFieldValues($values, $field, new ImportingParameters(), $external_folders);
    if ($parameters->appendImages && $field['field_types'] == 'image') {
      $field_log['values'] = array_merge($entity
        ->get($field['machine_names'])
        ->getValue(), $field_log['values']);
    }
    $this
      ->duplicateValuesPass($field_log);
    $this
      ->cardinalityPass($field_log, $field);
    $this
      ->useDefaultValuePass($field_log, $field);
    $this
      ->requiredPass($field_log, $field);
    $field_value = count($field_log['values']) == 1 ? current($field_log['values']) : $field_log['values'];
    $accepted = $parameters
      ->matchOneFieldLog($field_log);
    if (!$accepted) {
      $error_log[$key] = $field_log;
    }
    else {
      $entity
        ->set($field['machine_names'], $field_value);
      $change = TRUE;
    }
  }
  if ($change && $parameters->createProduct) {
    $entity
      ->save();
    if ($entity
      ->getEntityTypeId() == 'commerce_product') {
      if ($this
        ->moduleHandler()
        ->moduleExists('redirect')) {
        $this
          ->createProductRedirection($entity, $redirections);
      }
    }
  }
  if (!empty($error_log)) {
    $this
      ->changeFieldHasLog($error_log);
    return $error_log;
  }
  else {
    return TRUE;
  }
}