public function process($item)
 {
     $sku = (string) $item['sku'];
     $attribute = $this->productManager->getIdentifierAttribute();
     $product = $this->productManager->findByIdentifier($sku);
     // if (!$product) {
     //     $product = $this->productManager->createProduct();
     //     $value   = $this->productManager->createProductValue();
     //     $value->setAttribute($attribute);
     //     $value->setData($sku);
     //     $product->addValue($value);
     //     $this->stepExecution->incrementSummaryInfo('create');
     //     return $product;
     // } else {
     if (!$product) {
         $data = current((array) $item);
         $this->stepExecution->incrementSummaryInfo('skip');
         throw new InvalidItemException(sprintf('Skip the existing %s product', $sku), $data);
     } else {
         $product_tab = array();
         $product_tab[] = $product;
         $this->productUpdater->setValue($product_tab, 'price', [['data' => (string) $item['price'], 'currency' => (string) $item['currency']]]);
         return $product;
     }
     // }
 }
 /**
  * Don't allow creating an identifier attribute if one already exists
  *
  * @param AbstractAttribute $attribute
  * @param Constraint        $constraint
  */
 public function validate($attribute, Constraint $constraint)
 {
     if ($attribute->getAttributeType() === 'pim_catalog_identifier') {
         $identifier = $this->manager->getIdentifierAttribute();
         if ($identifier && $identifier->getId() !== $attribute->getId()) {
             $this->context->addViolationAt('attributeType', $constraint->message);
         }
     }
 }
 /**
  * Create and configure a family intance
  *
  * @return Family
  */
 public function createFamily()
 {
     $family = new Family();
     $identifier = $this->productManager->getIdentifierAttribute();
     $family->addAttribute($identifier);
     $family->setAttributeAsLabel($identifier);
     foreach ($this->getChannels() as $channel) {
         $family->addAttributeRequirement($this->factory->createAttributeRequirement($identifier, $channel, true));
     }
     return $family;
 }
Exemple #4
0
    public function process($item)
    {
        $sku       = $item['sku'];
        $attribute = $this->productManager->getIdentifierAttribute();
        $product   = $this->productManager->findByIdentifier($sku);

        if (!$product) {
            $product = $this->productManager->createProduct();
            $value   = $this->productManager->createProductValue();
            $value->setAttribute($attribute);
            $value->setData($sku);
            $product->addValue($value);
            $this->stepExecution->incrementSummaryInfo('create');

            return $product;

        } else {

            $data = current(((array) $item));
            $this->stepExecution->incrementSummaryInfo('skip');

            throw new InvalidItemException(sprintf('Skip the existing %s product', $sku), $data);
        }
    }
 /**
  * @param  object                                                $product
  * @param  null                                                  $format
  * @param  array                                                 $context
  * @return array|\Symfony\Component\Serializer\Normalizer\scalar
  */
 public function normalize($product, $format = null, array $context = [])
 {
     $normalizedAssociations = [];
     $identifierAttributeCode = $this->productManager->getIdentifierAttribute()->getCode();
     $sku = $product->getValue($this->productManager->getIdentifierAttribute()->getCode())->getData();
     $normalizedAssociations['sku'] = $sku;
     $normalizedAssociations['family'] = $product->getFamily()->getCode();
     $normalizedAssociations['associations'] = [];
     foreach ($product->getAssociations() as $association) {
         $associationCode = $association->getAssociationType()->getCode();
         if ($association->getGroups()->count() > 0 || $association->getProducts()->count() > 0) {
             /**@var Product $product * */
             $normalizedAssociations['associations'][$associationCode] = ['type' => null, 'groups' => [], 'products' => []];
             $normalizedAssociations['associations'][$associationCode]['type'] = $associationCode;
             foreach ($association->getGroups() as $group) {
                 $normalizedAssociations['associations'][$associationCode]['groups'][] = $group->getCode();
             }
             foreach ($association->getProducts() as $product) {
                 $normalizedAssociations['associations'][$associationCode]['products'][] = $product->getValue($identifierAttributeCode)->getData();
             }
         }
     }
     return $normalizedAssociations;
 }
 /**
  * @param ProductInterface $product
  * @param array            $drupalProduct
  */
 protected function computeProductAssociation(ProductInterface $product, array &$drupalProduct)
 {
     $identifierAttributeCode = $this->productManager->getIdentifierAttribute()->getCode();
     /** @var Group $group */
     foreach ($product->getAssociations() as $association) {
         $associationCode = $association->getAssociationType()->getCode();
         if ($association->getGroups()->count() > 0 || $association->getProducts()->count() > 0) {
             /**@var Product $product * */
             $drupalProduct['associations'][$associationCode] = ['type' => null, 'groups' => [], 'products' => []];
             $drupalProduct['associations'][$associationCode]['type'] = $associationCode;
             foreach ($association->getGroups() as $group) {
                 $drupalProduct['associations'][$associationCode]['groups'][] = $group->getCode();
             }
             foreach ($association->getProducts() as $product) {
                 $drupalProduct['associations'][$associationCode]['products'][] = $product->getValue($identifierAttributeCode)->getData();
             }
         }
     }
 }