/** * Test toNestedArray() method with custom attributes and with skipped custom attribute. */ public function testToNestedArrayCustom() { $dataArray = ['attribute_key' => 'attribute_value', AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY => [[AttributeValue::ATTRIBUTE_CODE => 'custom_attribute_code', AttributeValue::VALUE => 'custom_attribute_value'], [AttributeValue::ATTRIBUTE_CODE => 'custom_attribute_code_multi', AttributeValue::VALUE => ['custom_attribute_value_multi_1', 'custom_attribute_value_multi_2']], [AttributeValue::ATTRIBUTE_CODE => 'custom_attribute_code_skip', AttributeValue::VALUE => 'custom_attribute_value_skip']]]; $resultArray = ['attribute_key' => 'attribute_value', 'custom_attribute_code' => 'custom_attribute_value', 'custom_attribute_code_multi' => ['custom_attribute_value_multi_1', 'custom_attribute_value_multi_2']]; $this->processor->expects($this->any())->method('buildOutputDataArray')->with($this->dataObject)->willReturn($dataArray); $this->assertEquals($resultArray, $this->converter->toNestedArray($this->dataObject, ['custom_attribute_code_skip'])); }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { if ($saveOptions) { $productOptions = $product->getProductOptions(); } $groupPrices = $product->getData('group_price'); $tierPrices = $product->getData('tier_price'); $productId = $this->resourceModel->getIdBySku($product->getSku()); $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface'); $product = $this->initializeProductData($productDataArray, empty($productId)); $validationResult = $this->resourceModel->validate($product); if (true !== $validationResult) { throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult))); } try { if ($saveOptions) { $product->setProductOptions($productOptions); $product->setCanSaveCustomOptions(true); } $product->setData('group_price', $groupPrices); $product->setData('tier_price', $tierPrices); $this->resourceModel->save($product); } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) { throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception); } catch (\Exception $e) { throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product')); } unset($this->instances[$product->getSku()]); unset($this->instancesById[$product->getId()]); return $product; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { if ($saveOptions) { $productOptions = $product->getProductOptions(); } $isDeleteOptions = $product->getIsDeleteOptions(); $tierPrices = $product->getData('tier_price'); $productId = $this->resourceModel->getIdBySku($product->getSku()); $ignoreLinksFlag = $product->getData('ignore_links_flag'); $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface'); $productLinks = null; if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) { $productLinks = $product->getProductLinks(); } $productDataArray['store_id'] = (int) $this->storeManager->getStore()->getId(); $product = $this->initializeProductData($productDataArray, empty($productId)); if (isset($productDataArray['options'])) { if (!empty($productDataArray['options']) || $isDeleteOptions) { $this->processOptions($product, $productDataArray['options']); $product->setCanSaveCustomOptions(true); } } $this->processLinks($product, $productLinks); if (isset($productDataArray['media_gallery_entries'])) { $this->processMediaGallery($product, $productDataArray['media_gallery_entries']); } $validationResult = $this->resourceModel->validate($product); if (true !== $validationResult) { throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult))); } try { if ($saveOptions) { $product->setProductOptions($productOptions); $product->setCanSaveCustomOptions(true); } if ($tierPrices !== null) { $product->setData('tier_price', $tierPrices); } $this->resourceModel->save($product); } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) { throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception); } catch (\Exception $e) { throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product')); } unset($this->instances[$product->getSku()]); unset($this->instancesById[$product->getId()]); return $this->get($product->getSku()); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null) { $this->validate($customer); $prevCustomerData = null; if ($customer->getId()) { $prevCustomerData = $this->getById($customer->getId()); } $customer = $this->imageProcessor->save($customer, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $prevCustomerData); $origAddresses = $customer->getAddresses(); $customer->setAddresses([]); $customerData = $this->extensibleDataObjectConverter->toNestedArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setAddresses($origAddresses); $customerModel = $this->customerFactory->create(['data' => $customerData]); $storeId = $customerModel->getStoreId(); if ($storeId === null) { $customerModel->setStoreId($this->storeManager->getStore()->getId()); } $customerModel->setId($customer->getId()); // Need to use attribute set or future updates can cause data loss if (!$customerModel->getAttributeSetId()) { $customerModel->setAttributeSetId(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER); } // Populate model with secure data if ($customer->getId()) { $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId()); $customerModel->setRpToken($customerSecure->getRpToken()); $customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt()); $customerModel->setPasswordHash($customerSecure->getPasswordHash()); } else { if ($passwordHash) { $customerModel->setPasswordHash($passwordHash); } } // If customer email was changed, reset RpToken info if ($prevCustomerData && $prevCustomerData->getEmail() !== $customerModel->getEmail()) { $customerModel->setRpToken(null); $customerModel->setRpTokenCreatedAt(null); } $this->customerResourceModel->save($customerModel); $this->customerRegistry->push($customerModel); $customerId = $customerModel->getId(); if ($customer->getAddresses() !== null) { if ($customer->getId()) { $existingAddresses = $this->getById($customer->getId())->getAddresses(); $getIdFunc = function ($address) { return $address->getId(); }; $existingAddressIds = array_map($getIdFunc, $existingAddresses); } else { $existingAddressIds = []; } $savedAddressIds = []; foreach ($customer->getAddresses() as $address) { $address->setCustomerId($customerId)->setRegion($address->getRegion()); $this->addressRepository->save($address); if ($address->getId()) { $savedAddressIds[] = $address->getId(); } } $addressIdsToDelete = array_diff($existingAddressIds, $savedAddressIds); foreach ($addressIdsToDelete as $addressId) { $this->addressRepository->deleteById($addressId); } } $savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId()); $this->eventManager->dispatch('customer_save_after_data_object', ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer]); return $savedCustomer; }
/** * Convert address data object to a flat array * * @param CustomerInterface $customer * @return array */ public function toFlatArray(CustomerInterface $customer) { $flatArray = $this->extensibleDataObjectConverter->toNestedArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); unset($flatArray["addresses"]); return ConvertArray::toFlatArray($flatArray); }