使用Collection类获取客户的数据

1.25K 浏览开发笔记

使用Collection类获取客户的数据

customer 集合类 在 MagentoCustomerModelResourceModelCustomerCollection.php
order集合类 在 MagentoSalesModelResourceModelOrderCollection.php
product集合类 在MagentoCatalogModelResourceModelProductCollection.php

获取数据

<code><?php namespace VeriteworksExportDataControllerIndex; class Collection extends MagentoFrameworkAppActionAction{ public function execute(){ $customerCollection = $this->_objectManager->create('MagentoCustomerModelResourceModelCustomerCollection'); } }</code>

过滤要采集的数据

使用AttributeToFillter()按元素值过滤。
只检索邮件地址为“[email&#160;protected]”的客户

<code> $customerCollection = $this->_objectManager->create('MagentoCustomerModelResourceModelCustomerCollection')->addAttributeToFilter('email', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__">[email&#160;protected]</a>');</code>

仅检索产品名称包括“pen”的产品

<code>$productCollection = $this->_objectManager->create('MagentoCatalogModelResourceModelProductCollection')->addAttributeToFilter('name', array('like' => '%pen%'));</code>

订单排序

要按升序降序重新排列数据,使用setOrder()
按Entitiy_id以降序重新排列

<code> $customerCollection = $this->_objectManager->create('MagentoCustomerModelResourceModelCustomerCollection')->setOrder('entity_id', 'DESC');</code>

按updated_at以降序重新排列

<code> $customerCollection = $this->_objectManager->create('MagentoCustomerModelResourceModelCustomerCollection')->setOrder('updated_at', 'ASC');</code>

检索元素

为了得到我们使用每个getter方法的元素
firstName

<code><?php namespace VeriteworksExportDataControllerIndex; class Collection extends MagentoFrameworkAppActionAction{ public function execute(){ $customerCollection = $this->_objectManager->create('MagentoCustomerModelResourceModelCustomerCollection'); foreach ($customerCollection as $customer) { echo $customer->getFirstname() ."<br />"; } } }</code>
0