本文整理汇总了PHP中Magento\Customer\Api\AddressRepositoryInterface类的典型用法代码示例。如果您正苦于以下问题:PHP AddressRepositoryInterface类的具体用法?PHP AddressRepositoryInterface怎么用?PHP AddressRepositoryInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AddressRepositoryInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save
/**
* @param CartInterface $quote
* @param AddressInterface $address
* @param bool $useForShipping
* @return void
* @throws NoSuchEntityException
* @throws InputException
*/
public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false)
{
/** @var \Magento\Quote\Model\Quote $quote */
$this->addressValidator->validate($address);
$customerAddressId = $address->getCustomerAddressId();
$shippingAddress = null;
$addressData = [];
if ($useForShipping) {
$shippingAddress = $address;
}
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
if ($customerAddressId) {
try {
$addressData = $this->addressRepository->getById($customerAddressId);
} catch (NoSuchEntityException $e) {
// do nothing if customer is not found by id
}
$address = $quote->getBillingAddress()->importCustomerAddressData($addressData);
if ($useForShipping) {
$shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData);
$shippingAddress->setSaveInAddressBook($saveInAddressBook);
}
} elseif ($quote->getCustomerId()) {
$address->setEmail($quote->getCustomerEmail());
}
$address->setSaveInAddressBook($saveInAddressBook);
$quote->setBillingAddress($address);
if ($useForShipping) {
$shippingAddress->setSameAsBilling(1);
$shippingAddress->setCollectShippingRates(true);
$quote->setShippingAddress($shippingAddress);
}
}
示例2: _prepareLayout
/**
* Prepare the layout of the address edit block.
*
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
// Init address object
if ($addressId = $this->getRequest()->getParam('id')) {
try {
$this->_address = $this->_addressRepository->getById($addressId);
if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) {
$this->_address = null;
}
} catch (NoSuchEntityException $e) {
$this->_address = null;
}
}
if ($this->_address === null || !$this->_address->getId()) {
$this->_address = $this->addressDataFactory->create();
$customer = $this->getCustomer();
$this->_address->setPrefix($customer->getPrefix());
$this->_address->setFirstname($customer->getFirstname());
$this->_address->setMiddlename($customer->getMiddlename());
$this->_address->setLastname($customer->getLastname());
$this->_address->setSuffix($customer->getSuffix());
}
$this->pageConfig->getTitle()->set($this->getTitle());
if ($postedData = $this->_customerSession->getAddressFormData(true)) {
if (!empty($postedData['region_id']) || !empty($postedData['region'])) {
$postedData['region'] = ['region_id' => $postedData['region_id'], 'region' => $postedData['region']];
}
$this->dataObjectHelper->populateWithArray($this->_address, $postedData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
}
return $this;
}
示例3: execute
/**
* Set persistent data to customer session
*
* @param \Magento\Framework\Event\Observer $observer
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) {
return $this;
}
if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) {
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId());
if ($defaultShipping = $customer->getDefaultShipping()) {
/** @var \Magento\Customer\Model\Data\Address $address */
$address = $this->addressRepository->getById($defaultShipping);
if ($address) {
$this->_customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]);
}
}
if ($defaultBilling = $customer->getDefaultBilling()) {
$address = $this->addressRepository->getById($defaultBilling);
if ($address) {
$this->_customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]);
}
}
$this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId());
}
return $this;
}
示例4: validate
/**
* Validates the fields in a specified address data object.
*
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
* @return bool
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
*/
public function validate(\Magento\Quote\Api\Data\AddressInterface $addressData)
{
//validate customer id
if ($addressData->getCustomerId()) {
$customer = $this->customerRepository->getById($addressData->getCustomerId());
if (!$customer->getId()) {
throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid customer id %1', $addressData->getCustomerId()));
}
}
if ($addressData->getCustomerAddressId()) {
try {
$this->addressRepository->getById($addressData->getCustomerAddressId());
} catch (NoSuchEntityException $e) {
throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid address id %1', $addressData->getId()));
}
$applicableAddressIds = array_map(function ($address) {
/** @var \Magento\Customer\Api\Data\AddressInterface $address */
return $address->getId();
}, $this->customerRepository->getById($addressData->getCustomerId())->getAddresses());
if (!in_array($addressData->getCustomerAddressId(), $applicableAddressIds)) {
throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid address id %1', $addressData->getCustomerAddressId()));
}
}
return true;
}
示例5: assign
/**
* {@inheritDoc}
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.'));
}
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
$sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
$customerAddressId = $address->getCustomerAddressId();
$this->addressValidator->validate($address);
$quote->setShippingAddress($address);
$address = $quote->getShippingAddress();
if ($customerAddressId) {
$addressData = $this->addressRepository->getById($customerAddressId);
$address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
} elseif ($quote->getCustomerId()) {
$address->setEmail($quote->getCustomerEmail());
}
$address->setSameAsBilling($sameAsBilling);
$address->setSaveInAddressBook($saveInAddressBook);
$address->setCollectShippingRates(true);
try {
$this->totalsCollector->collectAddressTotals($quote, $address);
$address->save();
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(__('Unable to save address. Please, check input data.'));
}
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId()));
}
return $quote->getShippingAddress()->getId();
}
示例6: testGetAddress
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/Checkout/_files/quote_with_product_and_payment.php
*/
public function testGetAddress()
{
$addressFromFixture = $this->_addressRepository->getById(self::FIXTURE_ADDRESS_ID);
$address = $this->_block->getAddress();
$this->assertEquals($addressFromFixture->getFirstname(), $address->getFirstname());
$this->assertEquals($addressFromFixture->getLastname(), $address->getLastname());
$this->assertEquals($addressFromFixture->getCustomerId(), $address->getCustomerId());
}
示例7: testDeleteAddress
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
*/
public function testDeleteAddress()
{
$fixtureAddressId = 1;
$serviceInfo = ['rest' => ['resourcePath' => "/V1/addresses/{$fixtureAddressId}", 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE], 'soap' => ['service' => self::SOAP_SERVICE_NAME, 'serviceVersion' => self::SOAP_SERVICE_VERSION, 'operation' => self::SOAP_SERVICE_NAME . 'DeleteById']];
$requestData = ['addressId' => $fixtureAddressId];
$response = $this->_webApiCall($serviceInfo, $requestData);
$this->assertTrue($response, 'Expected response should be true.');
$this->setExpectedException('Magento\\Framework\\Exception\\NoSuchEntityException', 'No such entity with addressId = 1');
$this->addressRepository->getById($fixtureAddressId);
}
示例8: testGetAddressCollectionJson
public function testGetAddressCollectionJson()
{
$addressData = $this->_getAddresses();
$searchResult = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false, true, true, ['getItems']);
$searchResult->expects($this->any())->method('getItems')->will($this->returnValue($addressData));
$this->addressRepository->expects($this->any())->method('getList')->will($this->returnValue($searchResult));
$expectedOutput = '[
{
"firstname": false,
"lastname": false,
"company": false,
"street": "",
"city": false,
"country_id": "US",
"region": false,
"region_id": false,
"postcode": false,
"telephone": false,
"fax": false,
"vat_id": false
},
{
"firstname": "FirstName1",
"lastname": "LastName1",
"company": false,
"street": "Street1",
"city": false,
"country_id": false,
"region": false,
"region_id": false,
"postcode": false,
"telephone": false,
"fax": false,
"vat_id": false
},
{
"firstname": "FirstName2",
"lastname": "LastName2",
"company": false,
"street": "Street2",
"city": false,
"country_id": false,
"region": false,
"region_id": false,
"postcode": false,
"telephone": false,
"fax": false,
"vat_id": false
}
]';
$expectedOutput = str_replace([' ', "\n", "\r"], '', $expectedOutput);
$expectedOutput = str_replace(': ', ':', $expectedOutput);
$this->assertEquals($expectedOutput, $this->_addressBlock->getAddressCollectionJson());
}
示例9: execute
/**
* @return void
*/
public function execute()
{
$filter = $this->filterBuilder->setField('parent_id')->setValue($this->_getCheckout()->getCustomer()->getId())->setConditionType('eq')->create();
$addresses = (array) $this->addressRepository->getList($this->searchCriteriaBuilder->addFilters([$filter])->create())->getItems();
/**
* if we create first address we need reset emd init checkout
*/
if (count($addresses) === 1) {
$this->_getCheckout()->reset();
}
$this->_redirect('*/checkout/addresses');
}
示例10: testGetDefaultRateRequest
public function testGetDefaultRateRequest()
{
$customerDataSet = $this->customerRepository->getById(self::FIXTURE_CUSTOMER_ID);
$address = $this->addressRepository->getById(self::FIXTURE_ADDRESS_ID);
$rateRequest = $this->_model->getRateRequest(null, null, null, null, $customerDataSet->getId());
$this->assertNotNull($rateRequest);
$this->assertEquals($address->getCountryId(), $rateRequest->getCountryId());
$this->assertEquals($address->getRegion()->getRegionId(), $rateRequest->getRegionId());
$this->assertEquals($address->getPostcode(), $rateRequest->getPostcode());
$customerTaxClassId = $this->groupRepository->getById($customerDataSet->getGroupId())->getTaxClassId();
$this->assertEquals($customerTaxClassId, $rateRequest->getCustomerClassId());
}
示例11: getAddress
/**
* Get a list of current customer addresses.
*
* @return \Magento\Customer\Api\Data\AddressInterface[]
*/
public function getAddress()
{
$addresses = $this->getData('address_collection');
if ($addresses === null) {
try {
$filter = $this->filterBuilder->setField('parent_id')->setValue($this->_multishipping->getCustomer()->getId())->setConditionType('eq')->create();
$addresses = (array) $this->addressRepository->getList($this->searchCriteriaBuilder->addFilters([$filter])->create())->getItems();
} catch (NoSuchEntityException $e) {
return [];
}
$this->setData('address_collection', $addresses);
}
return $addresses;
}
示例12: testPopulateCustomerInfo
public function testPopulateCustomerInfo()
{
$this->quoteMock->expects($this->once())->method('getCustomer')->willReturn($this->customerMock);
$this->customerMock->expects($this->atLeastOnce())->method('getId')->willReturn(null);
$this->customerMock->expects($this->atLeastOnce())->method('getDefaultBilling')->willReturn(100500);
$this->quoteMock->expects($this->atLeastOnce())->method('getBillingAddress')->willReturn($this->quoteAddressMock);
$this->quoteMock->expects($this->atLeastOnce())->method('getShippingAddress')->willReturn($this->quoteAddressMock);
$this->quoteMock->expects($this->atLeastOnce())->method('setCustomer')->with($this->customerMock)->willReturnSelf();
$this->quoteMock->expects($this->once())->method('getPasswordHash')->willReturn('password hash');
$this->quoteAddressMock->expects($this->atLeastOnce())->method('getId')->willReturn(null);
$this->customerAddressRepositoryMock->expects($this->atLeastOnce())->method('getById')->with(100500)->willReturn($this->customerAddressMock);
$this->quoteAddressMock->expects($this->atLeastOnce())->method('importCustomerAddressData')->willReturnSelf();
$this->accountManagementMock->expects($this->once())->method('createAccountWithPasswordHash')->with($this->customerMock, 'password hash')->willReturn($this->customerMock);
$this->customerManagement->populateCustomerInfo($this->quoteMock);
}
示例13: testSetLayoutWithoutAddress
public function testSetLayoutWithoutAddress()
{
$addressId = 1;
$customerPrefix = 'prefix';
$customerFirstName = 'firstname';
$customerMiddlename = 'middlename';
$customerLastname = 'lastname';
$customerSuffix = 'suffix';
$title = 'title';
$layoutMock = $this->getMockBuilder('Magento\\Framework\\View\\LayoutInterface')->getMock();
$this->requestMock->expects($this->once())->method('getParam')->with('id', null)->willReturn($addressId);
$this->addressRepositoryMock->expects($this->once())->method('getById')->with($addressId)->willThrowException(\Magento\Framework\Exception\NoSuchEntityException::singleField('addressId', $addressId));
$newAddressMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->getMock();
$this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($newAddressMock);
$customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock();
$this->currentCustomerMock->expects($this->once())->method('getCustomer')->willReturn($customerMock);
$customerMock->expects($this->once())->method('getPrefix')->willReturn($customerPrefix);
$customerMock->expects($this->once())->method('getFirstname')->willReturn($customerFirstName);
$customerMock->expects($this->once())->method('getMiddlename')->willReturn($customerMiddlename);
$customerMock->expects($this->once())->method('getLastname')->willReturn($customerLastname);
$customerMock->expects($this->once())->method('getSuffix')->willReturn($customerSuffix);
$newAddressMock->expects($this->once())->method('setPrefix')->with($customerPrefix)->willReturnSelf();
$newAddressMock->expects($this->once())->method('setFirstname')->with($customerFirstName)->willReturnSelf();
$newAddressMock->expects($this->once())->method('setMiddlename')->with($customerMiddlename)->willReturnSelf();
$newAddressMock->expects($this->once())->method('setLastname')->with($customerLastname)->willReturnSelf();
$newAddressMock->expects($this->once())->method('setSuffix')->with($customerSuffix)->willReturnSelf();
$pageTitleMock = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Title')->disableOriginalConstructor()->getMock();
$this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($pageTitleMock);
$this->model->setData('title', $title);
$pageTitleMock->expects($this->once())->method('set')->with($title)->willReturnSelf();
$this->assertEquals($this->model, $this->model->setLayout($layoutMock));
$this->assertEquals($layoutMock, $this->model->getLayout());
}
示例14: testSearchAddresses
/**
* @param \Magento\Framework\Api\Filter[] $filters
* @param \Magento\Framework\Api\Filter[] $filterGroup
* @param \Magento\Framework\Api\SortOrder[] $filterOrders
* @param array $expectedResult array of expected results indexed by ID
*
* @dataProvider searchAddressDataProvider
*
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
* @magentoAppIsolation enabled
*/
public function testSearchAddresses($filters, $filterGroup, $filterOrders, $expectedResult)
{
/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
$searchBuilder = $this->_objectManager->create('Magento\\Framework\\Api\\SearchCriteriaBuilder');
foreach ($filters as $filter) {
$searchBuilder->addFilters([$filter]);
}
if ($filterGroup !== null) {
$searchBuilder->addFilters($filterGroup);
}
if ($filterOrders !== null) {
foreach ($filterOrders as $order) {
$searchBuilder->addSortOrder($order);
}
}
$searchResults = $this->repository->getList($searchBuilder->create());
$this->assertEquals(count($expectedResult), $searchResults->getTotalCount());
$i = 0;
/** @var \Magento\Customer\Api\Data\AddressInterface $item*/
foreach ($searchResults->getItems() as $item) {
$this->assertEquals($expectedResult[$i]['id'], $item->getId());
$this->assertEquals($expectedResult[$i]['city'], $item->getCity());
$this->assertEquals($expectedResult[$i]['postcode'], $item->getPostcode());
$this->assertEquals($expectedResult[$i]['firstname'], $item->getFirstname());
$i++;
}
}
示例15: testExecuteWithException
public function testExecuteWithException()
{
$addressId = 1;
$customerId = 2;
$this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect);
$this->request->expects($this->once())->method('getParam')->with('id', false)->willReturn($addressId);
$this->validatorMock->expects($this->once())->method('validate')->with($this->request)->willReturn(true);
$this->addressRepositoryMock->expects($this->once())->method('getById')->with($addressId)->willReturn($this->address);
$this->sessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
$this->address->expects($this->once())->method('getCustomerId')->willReturn(34);
$exception = new \Exception('Exception');
$this->messageManager->expects($this->once())->method('addError')->with(__('We can\'t delete the address right now.'))->willThrowException($exception);
$this->messageManager->expects($this->once())->method('addException')->with($exception, __('We can\'t delete the address right now.'));
$this->resultRedirect->expects($this->once())->method('setPath')->with('*/*/index')->willReturnSelf();
$this->assertSame($this->resultRedirect, $this->model->execute());
}