You are here

public function CommerceSmartImporerService::generateSku in Commerce Smart Importer 8

Generates new sku.

1 call to CommerceSmartImporerService::generateSku()
CommerceSmartImporerService::useDefaultValuePass in src/Plugin/CommerceSmartImporerService.php
Helper function for createNewProduct.

File

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

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function generateSku() {
  $config = $this
    ->getConfig();
  $method = $config['sku_method'];
  $prefix = $config['sku_prefix'];
  if ($method == 0) {

    // Auto increment.
    if ($this
      ->config('commerce_smart_importer.settings')
      ->get('increment_saver') == NULL) {
      $this->configFactoryService
        ->getEditable('commerce_smart_importer.settings')
        ->set('increment_saver', 0)
        ->save();
    }
    $increment = $this
      ->config('commerce_smart_importer.settings')
      ->get('increment_saver');
    do {
      $increment++;
      $query = $this->entityTypeManager
        ->getStorage('commerce_product_variation')
        ->getQuery();
      $query
        ->condition('sku', $prefix . $increment);
    } while (!empty($query
      ->execute()));
    return $prefix . $increment;
  }
  elseif ($method == 1) {
    $randomDigitsNumber = $config['sku_random_digits'];
    do {
      $sku = '';
      for ($i = 0; $i < $randomDigitsNumber; $i++) {
        $sku .= mt_rand(0, 9);
      }
      $query = $this
        ->entityTypeManager()
        ->getStorage('commerce_product_variation')
        ->getQuery();
      $query
        ->condition('sku', $prefix . $sku);
    } while (!empty($query
      ->execute()));
    return $prefix . $sku;
  }
}