Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Newance\Import\Service;
- use Magento\Catalog\Api\Data\ProductExtensionFactory;
- use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\CatalogInventory\Api\Data\StockItemInterface;
- use Magento\ConfigurableProduct\Api\Data\OptionInterface;
- use Newance\Import\Helper\Attribute;
- use Magento\Framework\App\ObjectManager;
- use Magento\Catalog\Model\ProductFactory;
- class ProductImporter
- {
- /**
- * @var Attribute
- */
- private $attributeHelper;
- /**
- * @var ProductRepositoryInterface
- */
- private $productRepository;
- /**
- * @var StockItemInterface
- */
- private $stockItem;
- /**
- * @var ProductAttributeRepositoryInterface
- */
- private $productAttributeRepository;
- /**
- * @var OptionInterface
- */
- private $option;
- private $productFactory;
- private $productExtensionFactory;
- /**
- * ProductImporter constructor.
- * @param Attribute $attributeHelper
- * @param ProductRepositoryInterface $productRepository
- * @param ProductFactory $productFactory
- * @param StockItemInterface $stockItem
- * @param ProductAttributeRepositoryInterface $productAttributeRepository
- * @param OptionInterface $option
- * @param ProductExtensionFactory $productExtensionFactory
- */
- public function __construct(Attribute $attributeHelper,
- ProductRepositoryInterface $productRepository,
- ProductFactory $productFactory,
- StockItemInterface $stockItem,
- ProductAttributeRepositoryInterface $productAttributeRepository,
- OptionInterface $option,
- ProductExtensionFactory $productExtensionFactory
- )
- {
- $this->attributeHelper = $attributeHelper;
- $this->productRepository = $productRepository;
- $this->productFactory = $productFactory;
- $this->stockItem = $stockItem;
- $this->productAttributeRepository = $productAttributeRepository;
- $this->option = $option;
- $this->productExtensionFactory = $productExtensionFactory;
- }
- /**
- * Import products
- *
- * @param $resultSet
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function import($resultSet)
- {
- foreach($resultSet as $result) {
- $product = $this->productFactory->create();
- $type = $result[2];
- $article = $result[1];
- $size = $result[3];
- $color = $result[4];
- $sku = $article.'-'.$color.'-'.$size;
- $name = $article . ' - ' . $color . ' - ' . $type . ' - ' . $size;
- $price = (float) isset($result[6]) ? $result[6] : 0;
- $product->setSku($sku);
- $product->setName($name);
- $product->setPrice($price);
- $product->setTypeId('simple');
- $product->setAttributeSetId(9); // nomo attribute set ID
- $product->setVisibility(1);
- $product->setWeight(0.00);
- if ( ! empty($size)) {
- $product->setCustomAttribute('nomo_maat', $this->attributeHelper->createOrGetId('nomo_maat', $size));
- }
- if ( ! empty($color)) {
- $product->setCustomAttribute('color', $this->attributeHelper->createOrGetId('color', $color));
- }
- if ( ! empty($type)) {
- $product->setCustomAttribute('nomo_type', $this->attributeHelper->createOrGetId('nomo_type', $type));
- }
- $productExtension = $this->productExtensionFactory->create();
- $this->stockItem->setItemId(1);
- $this->stockItem->setQty(100);
- $this->stockItem->setUseConfigManageStock(true);
- $this->stockItem->setIsInStock(true);
- $this->stockItem->setIsQtyDecimal(false);
- $this->stockItem->setUseConfigMinQty(true);
- $this->stockItem->setMinQty(0);
- $this->stockItem->setUseConfigMinSaleQty(0);
- $this->stockItem->setMinSaleQty(0);
- $product->setExtensionAttributes($productExtension->setStockItem($this->stockItem));
- $product->setStockData(['qty' => 100, 'is_in_stock' => 1]);
- $product->setQuantityAndStockStatus(['qty' => 100, 'is_in_stock' => 1]);
- $this->productRepository->save($product);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement