You are here

public function CommerceSmartImporerService::createAttribute in Commerce Smart Importer 8

Creates attribute for product.

1 call to CommerceSmartImporerService::createAttribute()
CommerceSmartImporerService::formatField in src/Plugin/CommerceSmartImporerService.php
Formats one field value based on field settings.

File

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

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function createAttribute($name, $reference, $create = TRUE) {
  $attributeId = $this
    ->entityTypeManager()
    ->getStorage('commerce_product_attribute_value')
    ->getQuery()
    ->condition('attribute', $reference)
    ->condition('name', $name)
    ->execute();
  if (!empty($attributeId)) {
    $attributeId = array_keys($attributeId);
    $attributeId = array_shift($attributeId);
    $attributeId = ProductAttributeValue::load($attributeId);
    return $attributeId;
  }
  else {
    $attributeId = ProductAttributeValue::create([
      'attribute' => $reference,
      'name' => $name,
    ]);
    if (!$create) {
      $attributeId
        ->save();
    }
    return $attributeId;
  }
}