You are here

public function CommerceSmartImporerService::createNewProduct in Commerce Smart Importer 8

Creates product.

File

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

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function createNewProduct($field_definitions, $product, ImportingParameters $parameters, $external_folders, $override_values) {
  $config = $this
    ->getConfig();
  $each_field_log = [];

  // Products.
  $is_product_creatable = TRUE;
  $redirections = [];
  foreach ($field_definitions['product'] as $key => $field_definition) {
    if ($field_definition['field_types'] == 'currency' || !array_key_exists('index', $field_definition)) {
      continue;
    }
    if ($field_definition['field_types'] === 'redirection') {
      $redirections = explode('|', $product['product'][$field_definition['index']]);
      continue;
    }
    $values = explode('|', $product['product'][$field_definition['index']]);
    if (isset($override_values['product'])) {
      $this
        ->overrideValue($values, $override_values['product'], $field_definition);
    }

    // Formats field and checks for validity.
    $each_field_log[$key] = $this
      ->formatMultipleFieldValues($values, $field_definition, $parameters, $external_folders);
    $this
      ->duplicateValuesPass($each_field_log[$key]);
    $this
      ->cardinalityPass($each_field_log[$key], $field_definition);
    $this
      ->useDefaultValuePass($each_field_log[$key], $field_definition);
    $this
      ->requiredPass($each_field_log[$key], $field_definition);
  }
  if ($is_product_creatable) {
    $is_product_creatable = $parameters
      ->matchParameters($each_field_log);
  }
  $this
    ->changeFieldHasLog($each_field_log);

  // Varitions.
  $variation_each_field_log = [];
  foreach ($product['variations'] as $key => $variation) {
    $variation_each_field_log[$key] = [];
    $variation_each_field_log[$key]['creatable'] = TRUE;
    if (!$this
      ->variationCurrencyValidityPass($variation, $field_definitions)) {
      $variation_each_field_log[$key]['currency'] = FALSE;
    }
    else {
      $variation_each_field_log[$key]['currency'] = TRUE;
    }
    foreach ($field_definitions['variation'] as $field_key => $field_definition) {
      if ($field_definition['field_types'] == 'currency' || !array_key_exists('index', $field_definition)) {
        continue;
      }
      if (is_array($variation[$field_definition['index']])) {
        continue;
      }
      $values = explode('|', $variation[$field_definition['index']]);
      if (isset($override_values['variations'][$key])) {
        $this
          ->overrideValue($values, $override_values['variations'][$key], $field_definition);
      }
      $variation_each_field_log[$key][$field_key] = $this
        ->formatMultipleFieldValues($values, $field_definition, $parameters, $external_folders);
      $this
        ->duplicateValuesPass($variation_each_field_log[$key][$field_key]);
      $this
        ->cardinalityPass($variation_each_field_log[$key][$field_key], $field_definition);
      $this
        ->useDefaultValuePass($variation_each_field_log[$key][$field_key], $field_definition);
      $this
        ->requiredPass($variation_each_field_log[$key][$field_key], $field_definition);
      if ($field_definition['machine_names'] === 'sku') {
        $variation_each_field_log[$key][$field_key]['sku'] = TRUE;
      }
    }
    if ($variation_each_field_log[$key]['creatable']) {
      $variation_each_field_log[$key]['creatable'] = $parameters
        ->matchParameters($variation_each_field_log[$key]);
    }
    $this
      ->changeFieldHasLog($variation_each_field_log[$key]);
  }
  if (!$each_field_log['has_log']) {
    foreach ($variation_each_field_log as $one_field_log) {
      if ($one_field_log['has_log']) {
        $each_field_log['has_log'] = TRUE;
        break;
      }
    }
  }
  $error_log = [
    'product' => $each_field_log,
    'variations' => $variation_each_field_log,
  ];
  $product_data = $this
    ->formatValuesArray($each_field_log, $field_definitions['product']);
  $variation_data = [];
  $valid_variations = TRUE;
  foreach ($variation_each_field_log as $variation_field_log) {
    if ($variation_field_log['creatable']) {
      $variation_data[] = $this
        ->formatValuesArray($variation_field_log, $field_definitions['variation']);
    }
    if ($parameters->notValidVariations === FALSE && !$variation_field_log['creatable']) {
      $valid_variations = FALSE;
    }
  }
  $created = FALSE;
  $valid_import = FALSE;
  if ($valid_variations) {
    if ($is_product_creatable && count($variation_data) > 0) {
      $product_data['uid'] = $this
        ->currentUser()
        ->id();
      $stores = [];
      if (!array_key_exists('stores', $product_data) || !is_array($product_data['stores']) || count($product_data['stores']) === 0) {
        if ($config['store'] != 'all') {
          $stores[] = [
            'target_id' => $config['store'],
          ];
        }
        else {
          $store_ids = $this
            ->getAllStoreIds();
          foreach ($store_ids as $store_id) {
            $stores[] = [
              'target_id' => $store_id,
            ];
          }
        }
        $product_data['stores'] = $stores;
      }
      $product_data['type'] = $config['commerce_product_bundle'];
      $productCreation = Product::create($product_data);
      foreach ($variation_data as $variation_datum) {
        $variation_datum['type'] = $config['commerce_product_variation_bundle'];
        $variationCreationTemp = ProductVariation::create($variation_datum);
        if ($variationCreationTemp
          ->validate()) {
          $variationCreationTemp
            ->save();
          $valid_import = TRUE;
          $productCreation
            ->addVariation($variationCreationTemp);
        }
      }
      if ($valid_import) {
        $productCreation
          ->save();
        if ($this
          ->moduleHandler()
          ->moduleExists('redirect')) {
          $this
            ->createProductRedirection($productCreation, $redirections);
        }
        if ($parameters->createProduct) {
          $created = TRUE;
        }
      }
    }
  }
  return [
    'error_log' => $error_log,
    'created' => $created,
  ];
}