/** * BC layer to find existing collection items by old identity filed values * * {@inheritdoc} */ protected function findExistingEntity($entity, array $searchContext = []) { $existingEntity = parent::findExistingEntity($entity, $searchContext); if (!$existingEntity && $entity instanceof OrderAddress) { /** @var OrderAddress $existingEntity */ $existingEntity = $this->existingEntity->getAddresses()->filter(function (OrderAddress $address) use($entity) { $isMatched = true; $fieldsToMatch = ['street', 'city', 'postalCode', 'country', 'region']; foreach ($fieldsToMatch as $fieldToMatch) { $addressValue = $this->getPropertyAccessor()->getValue($address, $fieldToMatch); $entityValue = $this->getPropertyAccessor()->getValue($entity, $fieldToMatch); $isMatched = $isMatched && $addressValue === $entityValue; } return $isMatched; })->first(); if ($existingEntity && $entity->getOriginId()) { $existingEntity->setOriginId($entity->getOriginId()); } } if ($entity instanceof OrderItem && is_null($entity->getName())) { //name can't be null, so to avoid import job failing empty string is used $entity->setName(''); } return $existingEntity; }
/** * BC layer to find existing collection items by old identity filed values * * {@inheritdoc} */ protected function findExistingEntity($entity, array $searchContext = []) { $existingEntity = parent::findExistingEntity($entity, $searchContext); if (!$existingEntity && $entity instanceof OrderAddress) { $propertyAccessor = PropertyAccess::createPropertyAccessor(); /** @var OrderAddress $existingEntity */ $existingEntity = $this->existingEntity->getAddresses()->filter(function (OrderAddress $address) use($entity, $propertyAccessor) { $isMatched = true; $fieldsToMatch = ['street', 'city', 'postalCode', 'country', 'region']; foreach ($fieldsToMatch as $fieldToMatch) { $addressValue = $propertyAccessor->getValue($address, $fieldToMatch); $entityValue = $propertyAccessor->getValue($entity, $fieldToMatch); $isMatched = $isMatched && $addressValue === $entityValue; } return $isMatched; })->first(); if ($existingEntity && $entity->getOriginId()) { $existingEntity->setOriginId($entity->getOriginId()); } } return $existingEntity; }
/** * "Success" form handler * * @param Order $entity */ protected function onSuccess(Order $entity) { if (null === $entity->getOrganization()) { $entity->setOrganization($this->organization); } /** @var OrderAddress $address */ foreach ($entity->getAddresses() as $address) { if (null === $address->getOwner()) { $address->setOwner($entity); } if (null === $address->getOrganization()) { $address->setOrganization($this->organization); } } /** @var OrderItem $item */ foreach ($entity->getItems() as $item) { if (null === $item->getOrder()) { $item->setOrder($entity); } } $this->manager->persist($entity); $this->manager->flush(); }
/** * @param Order $entityToUpdate * @param Order $entityToImport */ protected function processAddresses(Order $entityToUpdate, Order $entityToImport) { /** @var OrderAddress $address */ foreach ($entityToImport->getAddresses() as $k => $address) { if (!$address->getCountry()) { // skip addresses without country, we cant save it $entityToUpdate->getAddresses()->offsetUnset($k); continue; } // at this point imported address region have code equal to region_id in magento db field $mageRegionId = $address->getRegion() ? $address->getRegion()->getCode() : null; $existingAddress = $entityToUpdate->getAddresses()->get($k); if ($existingAddress) { $this->strategyHelper->importEntity($existingAddress, $address, ['id', 'region', 'country']); $address = $existingAddress; } $this->updateAddressCountryRegion($address, $mageRegionId); if (!$address->getCountry()) { $entityToUpdate->getAddresses()->offsetUnset($k); continue; } $this->updateAddressTypes($address); $address->setOwner($entityToUpdate); $entityToUpdate->getAddresses()->set($k, $address); } }