コード例 #1
0
ファイル: AddressRepository.php プロジェクト: mage2pro/core
 /**
  * 2016-07-27
  * Цель плагина — добавление возможности отключения необходимости платёжного адреса.
  * Это будет использоваться моими платёжными модулями.
  * Помимо этого плагина для данной функциональности нужны ещё 2:
  * @see \Df\Customer\Plugin\Model\Address\AbstractAddress
  * @see \Df\Sales\Plugin\Model\Order\Address\Validator
  *
  * @see \Magento\Customer\Model\ResourceModel\AddressRepository::save()
  * @param Sb $sb
  * @param \Closure $proceed
  * @param AI|CDA $address
  * @return AI
  * @throws InputException
  */
 public function aroundSave(Sb $sb, \Closure $proceed, AI $address)
 {
     /** @var AI $result */
     /**
      * 2016-07-27
      * Адрес приобретает тип, только когда используется при оформлении заказа.
      * Пока же адрес просто принадлежит покупателю
      * @see \Magento\Customer\Model\Data\Address
      * @see \Magento\Customer\Api\Data\AddressInterface
      * а не используется в контексте оформления заказа, то такой адрес ещё типа не имеет,
      * и в будущем, в зависимости от контекста,
      * может использоваться и как адрес доставки, и как платёжный адрес.
      *
      * По этой причине мы не вызываем здесь @see df_address_is_billing()
      * В то же время, мы попадаем сюда при оформлении заказа,
      * поэтому мы не можем проводить валидацию адреса,
      * если необходимость платёжного адреса отключена администратором.
      * Поэтому ветка S::disabled() нужна.
      */
     if (!S::disabled()) {
         $result = $proceed($address);
     } else {
         /** @var Customer $customer */
         $customer = df_customer_get($address->getCustomerId());
         /** @var Address $addressM */
         $addressM = null;
         if ($address->getId()) {
             $addressM = df_address_registry()->retrieve($address->getId());
         }
         if ($addressM) {
             $addressM->updateData($address);
         } else {
             $addressM = df_om()->create(Address::class);
             $addressM->updateData($address);
             $addressM->setCustomer($customer);
         }
         // 2016-07-27
         // Вот здесь в ядре валидация, а мы её пропускаем.
         $addressM->save();
         // Clean up the customer registry since the Address save has side effect on customer:
         // \Magento\Customer\Model\ResourceModel\Address::_afterSave
         df_customer_registry()->remove($address->getCustomerId());
         df_address_registry()->push($addressM);
         $customer->getAddressesCollection()->clear();
         $result = $addressM->getDataModel();
     }
     return $result;
 }
コード例 #2
0
ファイル: customer.php プロジェクト: mage2pro/core
/**
 * 2016-04-05
 * How to get a customer by his ID? https://mage2.pro/t/1136
 * How to get a customer by his ID with the @uses \Magento\Customer\Model\CustomerRegistry::retrieve()?
 * https://mage2.pro/t/1137
 * How to get a customer by his ID with the @see \Magento\Customer\Api\CustomerRepositoryInterface::getById()?
 * https://mage2.pro/t/1138
 * @param string $id
 * @return C
 * @throws NoSuchEntityException
 */
function df_customer_get($id)
{
    return df_customer_registry()->retrieve($id);
}
コード例 #3
0
ファイル: CustomerReturn.php プロジェクト: mage2pro/core
 /**         
  * @used-by execute()
  * @return MC|null
  * 2016-12-01
  * Отныне метод может (и будет) возвращать null в том случае,
  * когда учётная запись покупателя отсутствует в Magento,
  * а метод @see canRegister() вернул false (случай Blackbaud NetCommunity).
  */
 private function mc()
 {
     return dfc($this, function () {
         /** @var MCR $resource */
         $resource = df_customer_resource();
         /** @var \Magento\Framework\DB\Select $select */
         $select = df_db_from($resource, $resource->getEntityIdField());
         /**
          * 2015-10-10
          * 1) Полученный нами от браузера идентификатор пользователя Facebook
          * не является глобальным: он разный для разных приложений.
          * 2) Я так понял, что нельзя использовать одно и то же приложение Facebook
          * сразу на нескольких доменах.
          * 3) Из пунктов 1 и 2 следует, что нам нельзя идентифицировать пользователя Facebook
          * по его идентификатору: ведь Magento — многодоменная система.
          *
          * Есть выход: token_for_business
          * https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids
          * https://developers.facebook.com/docs/apps/for-business
          * https://business.facebook.com/
          */
         // 2016-11-21
         // Добавил возможность идентификации покупателей по email.
         // Вроде бы Discourse поступает аналогично.
         $select->where(df_db_or(df_db_quote_into("? = {$this->fId()}", $this->c()->id()), !$this->c()->email() ? null : ['? = email', $this->c()->email()]));
         /**
          * @see \Magento\Customer\Model\ResourceModel\Customer::loadByEmail()
          * https://github.com/magento/magento2/blob/2e2785cc6a78dc073a4d5bb5a88bd23161d3835c/app/code/Magento/Customer/Model/Resource/Customer.php#L215
          */
         if (!df_are_customers_global()) {
             /**
              * @see \Magento\Customer\Model\CustomerRegistry::retrieveByEmail()
              * https://github.com/magento/magento2/blob/2e2785cc6a78dc073a4d5bb5a88bd23161d3835c/app/code/Magento/Customer/Model/CustomerRegistry.php#L104
              * @see \Magento\Customer\Model\ResourceModel\Customer::loadByEmail()
              * https://github.com/magento/magento2/blob/2e2785cc6a78dc073a4d5bb5a88bd23161d3835c/app/code/Magento/Customer/Model/Resource/Customer.php#L222
              */
             $select->where('? = website_id', df_store_m()->getStore()->getWebsiteId());
         }
         /**
          * 2016-03-01
          * @uses \Zend_Db_Adapter_Abstract::fetchOne() возвращает false при пустом результате запроса.
          * https://mage2.pro/t/853
          * @var int|false $customerId
          */
         $customerId = df_conn()->fetchOne($select);
         /** @var MC|null $result */
         if ($result = !$customerId && !$this->canRegister() ? null : df_om()->create(MC::class)) {
             if (!$customerId) {
                 $this->register($result);
             } else {
                 $resource->load($result, $customerId);
                 // Обновляем в нашей БД полученую от сервиса авторизации информацию о покупателе.
                 $result->addData(dfa_select($this->customerData(), $this->customerFieldsToSync()));
                 $result->save();
             }
             /**
              * 2015-10-08
              * Ядро здесь делает так:
              * $customerModel = $this->customerFactory->create()->updateData($customer);
              * @see \Magento\Customer\Model\AccountManagement::authenticate()
              * https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Customer/Model/AccountManagement.php#L381
              * Я так понимаю, ядро так делает потому, что выше там код:
              * $customer = $this->customerRepository->get($username);
              * и этот код необязательно возвращает объект класса @see \Magento\Customer\Model\Customer
              * а может вернуть что-то другое, поддерживающее интерфейс
              * @see \Magento\Customer\Api\Data\CustomerInterface
              * @see \Magento\Customer\Api\CustomerRepositoryInterface::get()
              */
             /**
              * По аналогии с @see \Magento\Customer\Model\AccountManagement::authenticate()
              * https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Customer/Model/AccountManagement.php#L382-L385
              */
             df_dispatch('customer_customer_authenticated', ['model' => $result, 'password' => '']);
             /**
              * 2015-10-08
              * Не знаю, нужно ли это на самом деле.
              * Сделал по аналогии с @see \Magento\Customer\Model\CustomerRegistry::retrieveByEmail()
              * https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Customer/Model/CustomerRegistry.php#L133-L134
              *
              * 2016-12-01
              * Однозначно нужно.
              */
             df_customer_registry()->push($result);
             // 2015-12-10
             // Иначе новый покупатель не попадает в таблицу «customer_grid_flat».
             $result->reindex();
         }
         return $result;
     });
 }