Example #1
0
 /**
  * Check if giftmessages is allowed for specified entity.
  *
  * @param string $type
  * @param \Magento\Framework\DataObject $entity
  * @param \Magento\Store\Model\Store|int|null $store
  * @return bool|string|null
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store = null)
 {
     if ($type == 'items') {
         $items = $entity->getAllItems();
         if (!is_array($items) || empty($items)) {
             return $this->scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
         }
         if ($entity instanceof \Magento\Quote\Model\Quote) {
             $_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
         } else {
             $_type = 'order_item';
         }
         foreach ($items as $item) {
             if ($item->getParentItem()) {
                 continue;
             }
             if ($this->isMessagesAllowed($_type, $item, $store)) {
                 return true;
             }
         }
     } elseif ($type == 'item') {
         return $this->_getDependenceFromStoreConfig($entity->getProduct()->getGiftMessageAvailable(), $store);
     } elseif ($type == 'order_item') {
         return $this->_getDependenceFromStoreConfig($entity->getGiftMessageAvailable(), $store);
     } elseif ($type == 'address_item') {
         $storeId = is_numeric($store) ? $store : $this->_storeManager->getStore($store)->getId();
         if (!$this->isCached('address_item_' . $entity->getProductId())) {
             try {
                 $giftMessageAvailable = $this->productRepository->getById($entity->getProductId(), false, $storeId)->getGiftMessageAvailable();
             } catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) {
                 $giftMessageAvailable = null;
             }
             $this->setCached('address_item_' . $entity->getProductId(), $giftMessageAvailable);
         }
         return $this->_getDependenceFromStoreConfig($this->getCached('address_item_' . $entity->getProductId()), $store);
     } else {
         return $this->scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
     }
     return false;
 }