Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Address.php
Go to the documentation of this file.
1 <?php
7 
16 class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
17 {
24  const COLUMN_EMAIL = '_email';
25 
26  const COLUMN_WEBSITE = '_website';
27 
28  const COLUMN_ADDRESS_ID = '_entity_id';
29 
35  const COLUMN_COUNTRY_ID = 'country_id';
36 
40  const COLUMN_REGION_ID = 'region_id';
41 
45  const COLUMN_NAME_DEFAULT_BILLING = '_address_default_billing_';
46 
47  const COLUMN_NAME_DEFAULT_SHIPPING = '_address_default_shipping_';
48 
54  const ATTRIBUTE_COLLECTION_NAME = \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection::class;
55 
61  const XML_PATH_PAGE_SIZE = 'export/customer_page_size/address';
62 
67 
75 
81  protected static $_defaultAddressAttributeMapping = [
82  self::COLUMN_NAME_DEFAULT_BILLING => 'default_billing',
83  self::COLUMN_NAME_DEFAULT_SHIPPING => 'default_shipping',
84  ];
85 
92 
99 
105  protected $_customerEntity;
106 
121  protected $_customers = [];
122 
136  public function __construct(
137  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
138  \Magento\Store\Model\StoreManagerInterface $storeManager,
139  \Magento\ImportExport\Model\Export\Factory $collectionFactory,
140  \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory,
141  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
142  \Magento\Eav\Model\Config $eavConfig,
143  \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory,
144  \Magento\CustomerImportExport\Model\Export\CustomerFactory $eavCustomerFactory,
145  \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory,
146  array $data = []
147  ) {
148  parent::__construct(
149  $scopeConfig,
151  $collectionFactory,
152  $resourceColFactory,
153  $localeDate,
154  $eavConfig,
155  $data
156  );
157 
158  $this->_customerCollection = isset(
159  $data['customer_collection']
160  ) ? $data['customer_collection'] : $customerColFactory->create();
161 
162  $this->_customerEntity = isset(
163  $data['customer_entity']
164  ) ? $data['customer_entity'] : $eavCustomerFactory->create();
165 
166  $this->_addressCollection = isset(
167  $data['address_collection']
168  ) ? $data['address_collection'] : $addressColFactory->create();
169 
170  $this->_initAttributeValues()->_initAttributeTypes()->_initWebsites(true);
171  $this->setFileName($this->getEntityTypeCode());
172  }
173 
179  protected function _initCustomers()
180  {
181  if (empty($this->_customers)) {
182  // add customer default addresses column name to customer attribute mapping array
183  $this->_customerCollection->addAttributeToSelect(self::$_defaultAddressAttributeMapping);
184  // filter customer collection
185  $this->_customerCollection = $this->_customerEntity->filterEntityCollection($this->_customerCollection);
186 
187  $customers = [];
188  $addCustomer = function (\Magento\Customer\Model\Customer $customer) use (&$customers) {
189  $customers[$customer->getId()] = $customer->getData();
190  };
191 
192  $this->_byPagesIterator->iterate($this->_customerCollection, $this->_pageSize, [$addCustomer]);
193  $this->_customers = $customers;
194  }
195 
196  return $this;
197  }
198 
202  protected function _getHeaderColumns()
203  {
204  return array_merge(
205  $this->_permanentAttributes,
206  $this->_getExportAttributeCodes(),
207  array_keys(self::$_defaultAddressAttributeMapping)
208  );
209  }
210 
216  protected function _getEntityCollection()
217  {
219  }
220 
226  public function export()
227  {
228  // skip and filter by customer address attributes
230  $this->_getEntityCollection()->setCustomerFilter(array_keys($this->_customers));
231 
232  // prepare headers
233  $this->getWriter()->setHeaderCols($this->_getHeaderColumns());
234 
236 
237  return $this->getWriter()->getContents();
238  }
239 
246  public function exportItem($item)
247  {
249 
251  $customer = $this->_customers[$item->getParentId()];
252 
253  // Fill row with default address attributes values
254  foreach (self::$_defaultAddressAttributeMapping as $columnName => $attributeCode) {
255  if (!empty($customer[$attributeCode]) && $customer[$attributeCode] == $item->getId()) {
256  $row[$columnName] = 1;
257  }
258  }
259 
260  // Unique key
261  $row[self::COLUMN_ADDRESS_ID] = $item['entity_id'];
262  $row[self::COLUMN_EMAIL] = $customer['email'];
263  $row[self::COLUMN_WEBSITE] = $this->_websiteIdToCode[$customer['website_id']];
264  $row[self::COLUMN_REGION_ID] = $item->getRegionId();
265 
266  $this->getWriter()->writeRow($row);
267  }
268 
275  public function setParameters(array $parameters)
276  {
277  // push filters from post into export customer model
278  $this->_customerEntity->setParameters($parameters);
279  $this->_initCustomers();
280 
281  return parent::setParameters($parameters);
282  }
283 
289  public function getEntityTypeCode()
290  {
291  return $this->getAttributeCollection()->getEntityTypeCode();
292  }
293 }
$customer
Definition: customers.php:11
$storeManager
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Eav\Model\Config $eavConfig, \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory, \Magento\CustomerImportExport\Model\Export\CustomerFactory $eavCustomerFactory, \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory, array $data=[])
Definition: Address.php:136
_prepareEntityCollection(AbstractCollection $collection)
$attributeCode
Definition: extend.phtml:12
_addAttributeValuesToRow(\Magento\Framework\Model\AbstractModel $item, array $row=[])
_exportCollectionByPages(\Magento\Framework\Data\Collection\AbstractDb $collection)