Advertisement
Guest User

Product

a guest
May 9th, 2016
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Newance\Import\Service;
  4.  
  5. use Magento\Catalog\Api\Data\ProductExtensionFactory;
  6. use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
  7. use Magento\Catalog\Api\ProductRepositoryInterface;
  8. use Magento\CatalogInventory\Api\Data\StockItemInterface;
  9. use Magento\ConfigurableProduct\Api\Data\OptionInterface;
  10. use Newance\Import\Helper\Attribute;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Catalog\Model\ProductFactory;
  13.  
  14. class ProductImporter
  15. {
  16.     /**
  17.      * @var Attribute
  18.      */
  19.     private $attributeHelper;
  20.     /**
  21.      * @var ProductRepositoryInterface
  22.      */
  23.     private $productRepository;
  24.     /**
  25.      * @var StockItemInterface
  26.      */
  27.     private $stockItem;
  28.  
  29.     /**
  30.      * @var ProductAttributeRepositoryInterface
  31.      */
  32.     private $productAttributeRepository;
  33.     /**
  34.      * @var OptionInterface
  35.      */
  36.     private $option;
  37.  
  38.     private $productFactory;
  39.  
  40.     private $productExtensionFactory;
  41.  
  42.     /**
  43.      * ProductImporter constructor.
  44.      * @param Attribute $attributeHelper
  45.      * @param ProductRepositoryInterface $productRepository
  46.      * @param ProductFactory $productFactory
  47.      * @param StockItemInterface $stockItem
  48.      * @param ProductAttributeRepositoryInterface $productAttributeRepository
  49.      * @param OptionInterface $option
  50.      * @param ProductExtensionFactory $productExtensionFactory
  51.      */
  52.     public function __construct(Attribute $attributeHelper,
  53.                                 ProductRepositoryInterface $productRepository,
  54.                                 ProductFactory $productFactory,
  55.                                 StockItemInterface $stockItem,
  56.                                 ProductAttributeRepositoryInterface $productAttributeRepository,
  57.                                 OptionInterface $option,
  58.                                 ProductExtensionFactory $productExtensionFactory
  59. )
  60.     {
  61.         $this->attributeHelper = $attributeHelper;
  62.         $this->productRepository = $productRepository;
  63.         $this->productFactory = $productFactory;
  64.         $this->stockItem = $stockItem;
  65.         $this->productAttributeRepository = $productAttributeRepository;
  66.         $this->option = $option;
  67.         $this->productExtensionFactory = $productExtensionFactory;
  68.     }
  69.  
  70.     /**
  71.      * Import products
  72.      *
  73.      * @param $resultSet
  74.      * @throws \Magento\Framework\Exception\LocalizedException
  75.      */
  76.     public function import($resultSet)
  77.     {
  78.         foreach($resultSet as $result) {
  79.             $product = $this->productFactory->create();
  80.  
  81.             $type = $result[2];
  82.             $article = $result[1];
  83.             $size = $result[3];
  84.             $color = $result[4];
  85.  
  86.             $sku = $article.'-'.$color.'-'.$size;
  87.             $name = $article . ' - ' . $color . ' - ' . $type . ' - ' . $size;
  88.             $price = (float) isset($result[6]) ? $result[6] : 0;
  89.  
  90.             $product->setSku($sku);
  91.             $product->setName($name);
  92.             $product->setPrice($price);
  93.             $product->setTypeId('simple');
  94.             $product->setAttributeSetId(9); // nomo attribute set ID
  95.             $product->setVisibility(1);
  96.             $product->setWeight(0.00);
  97.  
  98.             if ( ! empty($size)) {
  99.                 $product->setCustomAttribute('nomo_maat', $this->attributeHelper->createOrGetId('nomo_maat', $size));
  100.             }
  101.  
  102.             if ( ! empty($color)) {
  103.                 $product->setCustomAttribute('color', $this->attributeHelper->createOrGetId('color', $color));
  104.             }
  105.  
  106.             if ( ! empty($type)) {
  107.                 $product->setCustomAttribute('nomo_type', $this->attributeHelper->createOrGetId('nomo_type', $type));
  108.             }
  109.  
  110.             $productExtension = $this->productExtensionFactory->create();
  111.             $this->stockItem->setItemId(1);
  112.             $this->stockItem->setQty(100);
  113.             $this->stockItem->setUseConfigManageStock(true);
  114.             $this->stockItem->setIsInStock(true);
  115.             $this->stockItem->setIsQtyDecimal(false);
  116.             $this->stockItem->setUseConfigMinQty(true);
  117.             $this->stockItem->setMinQty(0);
  118.             $this->stockItem->setUseConfigMinSaleQty(0);
  119.             $this->stockItem->setMinSaleQty(0);
  120.             $product->setExtensionAttributes($productExtension->setStockItem($this->stockItem));
  121.  
  122.             $product->setStockData(['qty' => 100, 'is_in_stock' => 1]);
  123.             $product->setQuantityAndStockStatus(['qty' => 100, 'is_in_stock' => 1]);
  124.  
  125.             $this->productRepository->save($product);
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement