示例#1
0
 /**
  * Creates a collection item that represents a customer for the customer Grid.
  *
  * @param CustomerDetails $customerDetail Input data for creating the item.
  * @return \Magento\Framework\Object Collection item that represents a customer
  */
 protected function createCustomerDetailItem(CustomerDetails $customerDetail)
 {
     $customer = $customerDetail->getCustomer();
     $customerNameParts = array($customer->getPrefix(), $customer->getFirstname(), $customer->getMiddlename(), $customer->getLastname(), $customer->getSuffix());
     $customerItem = new \Magento\Framework\Object();
     $customerItem->setId($customer->getId());
     $customerItem->setEntityId($customer->getId());
     // All parts of the customer name must be displayed in the name column of the grid
     $customerItem->setName(implode(' ', array_filter($customerNameParts)));
     $customerItem->setEmail($customer->getEmail());
     $customerItem->setWebsiteId($customer->getWebsiteId());
     $customerItem->setCreatedAt($customer->getCreatedAt());
     $customerItem->setGroupId($customer->getGroupId());
     $billingAddress = null;
     foreach ($customerDetail->getAddresses() as $address) {
         if ($address->isDefaultBilling()) {
             $billingAddress = $address;
             break;
         }
     }
     if ($billingAddress !== null) {
         $customerItem->setBillingTelephone($billingAddress->getTelephone());
         $customerItem->setBillingPostcode($billingAddress->getPostcode());
         $customerItem->setBillingCountryId($billingAddress->getCountryId());
         $region = is_null($billingAddress->getRegion()) ? '' : $billingAddress->getRegion()->getRegion();
         $customerItem->setBillingRegion($region);
     }
     return $customerItem;
 }
示例#2
0
文件: Action.php 项目: nja78/magento2
 /**
  * Update attribute values for entity list per store
  *
  * @param array $entityIds
  * @param array $attrData
  * @param int $storeId
  * @return $this
  * @throws \Exception
  */
 public function updateAttributes($entityIds, $attrData, $storeId)
 {
     $object = new \Magento\Framework\Object();
     $object->setStoreId($storeId);
     $this->_getWriteAdapter()->beginTransaction();
     try {
         foreach ($attrData as $attrCode => $value) {
             $attribute = $this->getAttribute($attrCode);
             if (!$attribute->getAttributeId()) {
                 continue;
             }
             $i = 0;
             foreach ($entityIds as $entityId) {
                 $i++;
                 $object->setId($entityId);
                 $object->setEntityId($entityId);
                 // collect data for save
                 $this->_saveAttributeValue($object, $attribute, $value);
                 // save collected data every 1000 rows
                 if ($i % 1000 == 0) {
                     $this->_processAttributeValues();
                 }
             }
             $this->_processAttributeValues();
         }
         $this->_getWriteAdapter()->commit();
     } catch (\Exception $e) {
         $this->_getWriteAdapter()->rollBack();
         throw $e;
     }
     return $this;
 }
示例#3
0
 /**
  * After Save Attribute manipulation
  *
  * @param \Magento\Catalog\Model\Product $object
  * @return $this
  */
 public function afterSave($object)
 {
     $websiteId = $this->_storeManager->getStore($object->getStoreId())->getWebsiteId();
     $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0;
     $priceRows = $object->getData($this->getAttribute()->getName());
     if (empty($priceRows)) {
         if ($isGlobal) {
             $this->_getResource()->deletePriceData($object->getId());
         } else {
             $this->_getResource()->deletePriceData($object->getId(), $websiteId);
         }
         return $this;
     }
     $old = array();
     $new = array();
     // prepare original data for compare
     $origGroupPrices = $object->getOrigData($this->getAttribute()->getName());
     if (!is_array($origGroupPrices)) {
         $origGroupPrices = array();
     }
     foreach ($origGroupPrices as $data) {
         if ($data['website_id'] > 0 || $data['website_id'] == '0' && $isGlobal) {
             $key = join('-', array_merge(array($data['website_id'], $data['cust_group']), $this->_getAdditionalUniqueFields($data)));
             $old[$key] = $data;
         }
     }
     // prepare data for save
     foreach ($priceRows as $data) {
         $hasEmptyData = false;
         foreach ($this->_getAdditionalUniqueFields($data) as $field) {
             if (empty($field)) {
                 $hasEmptyData = true;
                 break;
             }
         }
         if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
             continue;
         }
         if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) {
             continue;
         }
         if (!$isGlobal && (int) $data['website_id'] == 0) {
             continue;
         }
         $key = join('-', array_merge(array($data['website_id'], $data['cust_group']), $this->_getAdditionalUniqueFields($data)));
         $useForAllGroups = $data['cust_group'] == CustomerGroupServiceInterface::CUST_GROUP_ALL;
         $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0;
         $new[$key] = array_merge(array('website_id' => $data['website_id'], 'all_groups' => $useForAllGroups ? 1 : 0, 'customer_group_id' => $customerGroupId, 'value' => $data['price']), $this->_getAdditionalUniqueFields($data));
     }
     $delete = array_diff_key($old, $new);
     $insert = array_diff_key($new, $old);
     $update = array_intersect_key($new, $old);
     $isChanged = false;
     $productId = $object->getId();
     if (!empty($delete)) {
         foreach ($delete as $data) {
             $this->_getResource()->deletePriceData($productId, null, $data['price_id']);
             $isChanged = true;
         }
     }
     if (!empty($insert)) {
         foreach ($insert as $data) {
             $price = new \Magento\Framework\Object($data);
             $price->setEntityId($productId);
             $this->_getResource()->savePriceData($price);
             $isChanged = true;
         }
     }
     if (!empty($update)) {
         foreach ($update as $k => $v) {
             if ($old[$k]['price'] != $v['value']) {
                 $price = new \Magento\Framework\Object(array('value_id' => $old[$k]['price_id'], 'value' => $v['value']));
                 $this->_getResource()->savePriceData($price);
                 $isChanged = true;
             }
         }
     }
     if ($isChanged) {
         $valueChangedKey = $this->getAttribute()->getName() . '_changed';
         $object->setData($valueChangedKey, 1);
     }
     return $this;
 }
示例#4
0
文件: Url.php 项目: nja78/magento2
 /**
  * Retrieve Product data objects
  *
  * @param int|array $productIds
  * @param int $storeId
  * @param int $entityId
  * @param int &$lastEntityId
  * @return array
  */
 protected function _getProducts($productIds, $storeId, $entityId, &$lastEntityId)
 {
     $products = [];
     $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId();
     $adapter = $this->_getReadAdapter();
     if ($productIds !== null) {
         if (!is_array($productIds)) {
             $productIds = [$productIds];
         }
     }
     $bind = ['website_id' => (int) $websiteId, 'entity_id' => (int) $entityId];
     $select = $adapter->select()->useStraightJoin(true)->from(['e' => $this->getTable('catalog_product_entity')], ['entity_id'])->join(['w' => $this->getTable('catalog_product_website')], 'e.entity_id = w.product_id AND w.website_id = :website_id', [])->where('e.entity_id > :entity_id')->order('e.entity_id')->limit($this->_productLimit);
     if ($productIds !== null) {
         $select->where('e.entity_id IN(?)', $productIds);
     }
     $rowSet = $adapter->fetchAll($select, $bind);
     foreach ($rowSet as $row) {
         $product = new \Magento\Framework\Object($row);
         $product->setId($row['entity_id']);
         $product->setEntityId($row['entity_id']);
         $product->setCategoryIds([]);
         $product->setStoreId($storeId);
         $products[$product->getId()] = $product;
         $lastEntityId = $product->getId();
     }
     unset($rowSet);
     if ($products) {
         $select = $adapter->select()->from($this->getTable('catalog_category_product'), ['product_id', 'category_id'])->where('product_id IN(?)', array_keys($products));
         $categories = $adapter->fetchAll($select);
         foreach ($categories as $category) {
             $productId = $category['product_id'];
             $categoryIds = $products[$productId]->getCategoryIds();
             $categoryIds[] = $category['category_id'];
             $products[$productId]->setCategoryIds($categoryIds);
         }
         foreach (['name', 'url_key', 'url_path'] as $attributeCode) {
             $attributes = $this->_getProductAttribute($attributeCode, array_keys($products), $storeId);
             foreach ($attributes as $productId => $attributeValue) {
                 $products[$productId]->setData($attributeCode, $attributeValue);
             }
         }
     }
     return $products;
 }