Commit df037cb3 authored by Aleksey.Alekseev's avatar Aleksey.Alekseev
Browse files

products import part 1

parent 49c53498
......@@ -41,6 +41,22 @@ class DrushContentImport extends DrushCommands {
$this->output()->writeln("!NOTE Done.");
}
/**
* Synhelper Products import.
*
* @param string $directory
* Content directory.
*
* @command synhelper:product_import
* @usage synhelper:scont $directory
* Products import from $directory/products.
*/
public function importProducts($directory) {
$this->output()->writeln("Import Products from: $directory/products");
\Drupal::service('synhelper.content_import')->importProducts($directory);
$this->output()->writeln("!NOTE Done.");
}
/**
* Synhelper Menu import.
*
......
......@@ -7,6 +7,9 @@ use Drupal\media\Entity\Media;
use Drupal\pathauto\PathautoState;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\File\FileSystemInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_price\Price;
/**
* Defines Content Import.
......@@ -29,6 +32,7 @@ class SynhelperContentImport {
$this->importTaxonomy($directory);
$this->importNode($directory);
$this->importMenu($directory);
$this->importProducts($directory);
$this->setSynLandingConfig($directory);
}
......@@ -49,6 +53,20 @@ class SynhelperContentImport {
}
}
/**
* Import Products.
*/
public function importProducts(string $directory) {
$sourceProducts = new FileStorage($directory . '/products');
foreach ($sourceProducts->listAll() as $file) {
$product = $sourceProducts->read($file);
if (!empty($product['variations'])) {
$variations = $this->importVariations($product);
}
$this->createProduct($product, $variations);
}
}
/**
* Import Menu.
*/
......@@ -63,6 +81,7 @@ class SynhelperContentImport {
}
else {
\Drupal::entityTypeManager()->getStorage('menu')->create([
'uid' => 1,
'id' => $data['menu']['id'],
'label' => $data['menu']['label'],
'description' => $data['menu']['description'],
......@@ -132,7 +151,7 @@ class SynhelperContentImport {
break;
}
}
$n['uid'] = 1;
$node = $storage->create($n);
if (!empty($data['field_image_big'])) {
$node->set('field_image_big', $this->importMedia($data['field_image_big']));
......@@ -182,6 +201,46 @@ class SynhelperContentImport {
*
*/
/**
* Imports product variations.
*/
private function importVariations(array $product) : array {
foreach ($product['variations'] as $vid => $variation_content) {
$variation = ProductVariation::create([
'uid' => 1,
'id' => $vid,
'title' => $product['title'],
'type' => 'variation',
'sku' => $vid,
'price' => [
'number' => (int) filter_var($variation_content['price'], FILTER_SANITIZE_NUMBER_INT),
'currency_code' => $variation_content['currency'],
],
'field_oldprice' => $variation_content['field_oldprice'],
]);
$variations[] = $variation;
$variation->save();
}
return $variations;
}
/**
* Creates product.
*/
private function createProduct(array $data, array $variations = NULL) {
$product = Product::create([
'uid' => 1,
'type' => $data['type'],
'title' => $data['title'],
'field_image' => $this->importImages($data['field_image']),
'field_gallery' => $this->importImages($data['field_gallery']),
'field_short' => $data['field_short'],
'body' => $data['body'],
'variations' => $variations,
]);
$product->save();
}
/**
* Checks vocabulary for existence.
*/
......@@ -205,6 +264,7 @@ class SynhelperContentImport {
$vocabulary_name = $data['vid'];
}
$t = [
'uid' => 1,
'vid' => $data['vid'],
'name' => $vocabulary_name,
];
......@@ -217,6 +277,7 @@ class SynhelperContentImport {
*/
private function createTaxonomyTerm(array $data) {
$t = [
'uid' => 1,
'vid' => $data['vid'],
'name' => $data['name'],
'field_catalog_image' => $this->importImages($data['field_catalog_image']),
......@@ -233,6 +294,7 @@ class SynhelperContentImport {
$child_terms = [];
foreach ($data['children_terms'] as $child_term) {
$t = [
'uid' => 1,
'vid' => $vid,
'name' => $child_term['name'],
'field_catalog_image' => $this->importImages($child_term['field_catalog_image']),
......@@ -271,6 +333,7 @@ class SynhelperContentImport {
$link = \Drupal::entityTypeManager()->getStorage('menu_link_content');
foreach ($links as $menu_link) {
$t = [
'uid' => 1,
'title' => $menu_link['title'],
'link' => ['uri' => $menu_link['uri']],
'menu_name' => $menu_id,
......@@ -309,6 +372,7 @@ class SynhelperContentImport {
$item = $storage->create($about_item);
$item->save();
$items[] = [
'uid' => 1,
'type' => $about_item['type'],
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
......@@ -336,6 +400,7 @@ class SynhelperContentImport {
$item = $storage->create($image_blocks_item);
$item->save();
$items[] = [
'uid' => 1,
'type' => $image_blocks_item['type'],
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment