/** * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { $response = new \Magento\Framework\DataObject(); $response->setError(false); $attributeCode = $this->getRequest()->getParam('attribute_code'); $frontendLabel = $this->getRequest()->getParam('frontend_label'); $attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]); $attributeId = $this->getRequest()->getParam('attribute_id'); $attribute = $this->_objectManager->create('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute')->loadByCode($this->_entityTypeId, $attributeCode); if ($attribute->getId() && !$attributeId) { if (strlen($this->getRequest()->getParam('attribute_code'))) { $response->setMessage(__('An attribute with this code already exists.')); } else { $response->setMessage(__('An attribute with the same code (%1) already exists.', $attributeCode)); } $response->setError(true); } if ($this->getRequest()->has('new_attribute_set_name')) { $setName = $this->getRequest()->getParam('new_attribute_set_name'); /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */ $attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set'); $attributeSet->setEntityTypeId($this->_entityTypeId)->load($setName, 'attribute_set_name'); if ($attributeSet->getId()) { $setName = $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($setName); $this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $setName)); $layout = $this->layoutFactory->create(); $layout->initMessages(); $response->setError(true); $response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml()); } } return $this->resultJsonFactory->create()->setJsonData($response->toJson()); }
/** * Add attribute to attribute set * * @return \Magento\Framework\Controller\Result\Json */ public function execute() { $request = $this->getRequest(); $resultJson = $this->resultJsonFactory->create(); try { /** @var \Magento\Eav\Model\Entity\Attribute $attribute */ $attribute = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute')->load($request->getParam('attribute_id')); $attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->load($request->getParam('template_id')); /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection $attributeGroupCollection */ $attributeGroupCollection = $this->_objectManager->get('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Group\\Collection'); $attributeGroupCollection->setAttributeSetFilter($attributeSet->getId()); $attributeGroupCollection->addFilter('attribute_group_code', $request->getParam('group')); $attributeGroupCollection->setPageSize(1); $attributeGroup = $attributeGroupCollection->getFirstItem(); $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet(); $attribute->setAttributeSetId($request->getParam('template_id'))->setAttributeGroupId($attributeGroup->getId())->setSortOrder('0')->save(); $resultJson->setJsonData($attribute->toJson()); } catch (\Exception $e) { $response = new \Magento\Framework\DataObject(); $response->setError(false); $response->setMessage($e->getMessage()); $resultJson->setJsonData($response->toJson()); } return $resultJson; }
/** * Ajax handler to response configuration fieldset of composite product in quote items * * @return \Magento\Framework\View\Result\Layout */ public function execute() { // Prepare data $configureResult = new \Magento\Framework\DataObject(); try { $quoteItemId = (int) $this->getRequest()->getParam('id'); if (!$quoteItemId) { throw new \Magento\Framework\Exception\LocalizedException(__('Quote item id is not received.')); } $quoteItem = $this->_objectManager->create('Magento\\Quote\\Model\\Quote\\Item')->load($quoteItemId); if (!$quoteItem->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Quote item is not loaded.')); } $configureResult->setOk(true); $optionCollection = $this->_objectManager->create('Magento\\Quote\\Model\\Quote\\Item\\Option')->getCollection()->addItemFilter([$quoteItemId]); $quoteItem->setOptions($optionCollection->getOptionsByItem($quoteItem)); $configureResult->setBuyRequest($quoteItem->getBuyRequest()); $configureResult->setCurrentStoreId($quoteItem->getStoreId()); $configureResult->setProductId($quoteItem->getProductId()); $sessionQuote = $this->_objectManager->get('Magento\\Backend\\Model\\Session\\Quote'); $configureResult->setCurrentCustomerId($sessionQuote->getCustomerId()); } catch (\Exception $e) { $configureResult->setError(true); $configureResult->setMessage($e->getMessage()); } // Render page /** @var \Magento\Catalog\Helper\Product\Composite $helper */ $helper = $this->_objectManager->get('Magento\\Catalog\\Helper\\Product\\Composite'); return $helper->renderConfigureResult($configureResult); }
/** * Adds configured product to quote * * @return \Magento\Backend\Model\View\Result\Redirect */ public function executeInternal() { $errorMessage = null; try { $this->_initSession()->_processData(); } catch (\Exception $e) { $this->_reloadQuote(); $errorMessage = $e->getMessage(); } // Form result for client javascript $updateResult = new \Magento\Framework\DataObject(); if ($errorMessage) { $updateResult->setError(true); $updateResult->setMessage($errorMessage); } else { $updateResult->setOk(true); } $updateResult->setJsVarName($this->getRequest()->getParam('as_js_varname')); $this->_objectManager->get('Magento\Backend\Model\Session')->setCompositeProductResult($updateResult); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); return $resultRedirect->setPath('catalog/product/showUpdateResult'); }
/** * Create shipping label action for specific shipment * * @return void */ public function execute() { $response = new \Magento\Framework\DataObject(); try { $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment')); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); $this->labelGenerator->create($shipment, $this->_request); $shipment->save(); $this->messageManager->addSuccess(__('You created the shipping label.')); $response->setOk(true); } catch (\Magento\Framework\Exception\LocalizedException $e) { $response->setError(true); $response->setMessage($e->getMessage()); } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $response->setError(true); $response->setMessage(__('An error occurred while creating shipping label.')); } $this->getResponse()->representJson($response->toJson()); }
/** * Ajax handler to response configuration fieldset of composite product in customer's wishlist. * * @return \Magento\Framework\View\Result\Layout */ public function execute() { $configureResult = new \Magento\Framework\DataObject(); try { $this->_initData(); $configureResult->setProductId($this->_wishlistItem->getProductId()); $configureResult->setBuyRequest($this->_wishlistItem->getBuyRequest()); $configureResult->setCurrentStoreId($this->_wishlistItem->getStoreId()); $configureResult->setCurrentCustomerId($this->_wishlist->getCustomerId()); $configureResult->setOk(true); } catch (Exception $e) { $configureResult->setError(true); $configureResult->setMessage($e->getMessage()); } return $this->_objectManager->get('Magento\\Catalog\\Helper\\Product\\Composite')->renderConfigureResult($configureResult); }
/** * IFrame handler for submitted configuration for wishlist item. * * @return \Magento\Backend\Model\View\Result\Redirect */ public function executeInternal() { // Update wishlist item $updateResult = new \Magento\Framework\DataObject(); try { $this->_initData(); $buyRequest = new \Magento\Framework\DataObject($this->getRequest()->getParams()); $this->_wishlist->updateItem($this->_wishlistItem->getId(), $buyRequest)->save(); $updateResult->setOk(true); } catch (Exception $e) { $updateResult->setError(true); $updateResult->setMessage($e->getMessage()); } $updateResult->setJsVarName($this->getRequest()->getParam('as_js_varname')); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCompositeProductResult($updateResult); return $this->resultRedirectFactory->create()->setPath('catalog/product/showUpdateResult'); }
/** * Ajax handler to response configuration fieldset of composite product in customer's cart * * @return \Magento\Framework\View\Result\Layout */ public function executeInternal() { $configureResult = new \Magento\Framework\DataObject(); try { $this->_initData(); $quoteItem = $this->_quoteItem; $optionCollection = $this->_objectManager->create('Magento\\Quote\\Model\\Quote\\Item\\Option')->getCollection()->addItemFilter($quoteItem); $quoteItem->setOptions($optionCollection->getOptionsByItem($quoteItem)); $configureResult->setOk(true); $configureResult->setProductId($quoteItem->getProductId()); $configureResult->setBuyRequest($quoteItem->getBuyRequest()); $configureResult->setCurrentStoreId($quoteItem->getStoreId()); $configureResult->setCurrentCustomerId($this->_customerId); } catch (\Exception $e) { $configureResult->setError(true); $configureResult->setMessage($e->getMessage()); } return $this->_objectManager->get('Magento\\Catalog\\Helper\\Product\\Composite')->renderConfigureResult($configureResult); }
/** * Add attribute to attribute set * * @return \Magento\Framework\Controller\Result\Json */ public function execute() { $request = $this->getRequest(); $response = new \Magento\Framework\DataObject(); $response->setError(false); try { $attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->load($request->getParam('templateId')); /** @var \Magento\Eav\Model\ResourceModel\Attribute\Collection $collection */ $attributesCollection = $this->attributeCollectionFactory->create(); $attributesIds = $request->getParam('attributesIds'); if ($attributesIds['excludeMode'] === 'false' && !empty($attributesIds['selected'])) { $attributesCollection->addFieldToFilter('main_table.attribute_id', ['in' => $attributesIds['selected']]); } elseif ($attributesIds['excludeMode'] === 'true') { $attributesCollection->setExcludeSetFilter($attributeSet->getId()); } else { throw new \Magento\Framework\Exception\LocalizedException(__('Please, specify attributes')); } $groupCode = $request->getParam('groupCode'); /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection $attributeGroupCollection */ $attributeGroupCollection = $this->_objectManager->get('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Group\\Collection'); $attributeGroupCollection->setAttributeSetFilter($attributeSet->getId()); $attributeGroupCollection->addFilter('attribute_group_code', $groupCode); $attributeGroupCollection->setPageSize(1); $attributeGroup = $attributeGroupCollection->getFirstItem(); if (!$attributeGroup->getId()) { $attributeGroup->addData(['attribute_group_code' => $groupCode, 'attribute_set_id' => $attributeSet->getId(), 'attribute_group_name' => $request->getParam('groupName'), 'sort_order' => $request->getParam('groupSortOrder')]); $attributeGroup->save(); } foreach ($attributesCollection as $attribute) { $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet(); $attribute->setAttributeGroupId($attributeGroup->getId())->setSortOrder('0')->save(); } } catch (\Exception $e) { $response->setError(true); $response->setMessage($e->getMessage()); } return $this->resultJsonFactory->create()->setJsonData($response->toJson()); }
/** * Save shipment * We can save only new shipment. Existing shipments are not editable * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest()); $isPost = $this->getRequest()->isPost(); if (!$formKeyIsValid || !$isPost) { $this->messageManager->addError(__('We can\'t save the shipment right now.')); return $resultRedirect->setPath('sales/order/index'); } $data = $this->getRequest()->getParam('shipment'); if (!empty($data['comment_text'])) { $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']); } try { $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($data); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); if (!$shipment) { $this->_forward('noroute'); return; } if (!empty($data['comment_text'])) { $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\DataObject(); $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel) { $this->labelGenerator->create($shipment, $this->_request); $responseAjax->setOk(true); } $this->_saveShipment($shipment); if (!empty($data['send_email'])) { $this->shipmentSender->send($shipment); } $shipmentCreatedMessage = __('The shipment has been created.'); $labelCreatedMessage = __('You created the shipping label.'); $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage($e->getMessage()); } else { $this->messageManager->addError($e->getMessage()); $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); } } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage(__('An error occurred while creating shipping label.')); } else { $this->messageManager->addError(__('Cannot save shipment.')); $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); } } if ($isNeedCreateLabel) { $this->getResponse()->representJson($responseAjax->toJson()); } else { $this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]); } }
/** * @param StockItemInterface $stockItem * @param float|int $qty * @return \Magento\Framework\DataObject */ public function checkQtyIncrements(StockItemInterface $stockItem, $qty) { $result = new \Magento\Framework\DataObject(); if ($stockItem->getSuppressCheckQtyIncrements()) { return $result; } $qtyIncrements = $stockItem->getQtyIncrements() * 1; if ($qtyIncrements && $this->mathDivision->getExactDivision($qty, $qtyIncrements) != 0) { $result->setHasError(true)->setQuoteMessage(__('Please correct the quantity for some products.'))->setErrorCode('qty_increments')->setQuoteMessageIndex('qty'); if ($stockItem->getIsChildItem()) { $result->setMessage(__('You can buy %1 only in quantities of %2 at a time.', $stockItem->getProductName(), $qtyIncrements)); } else { $result->setMessage(__('You can buy this product only in quantities of %1 at a time.', $qtyIncrements)); } } return $result; }
/** * Add attribute to attribute set * * @return \Magento\Framework\Controller\Result\Json */ public function execute() { $request = $this->getRequest(); $response = new \Magento\Framework\DataObject(); $response->setError(false); try { /** @var AttributeSetInterface $attributeSet */ $attributeSet = $this->getAttributeSetRepository()->get($request->getParam('templateId')); $groupCode = $request->getParam('groupCode'); $groupName = $request->getParam('groupName'); $groupSortOrder = $request->getParam('groupSortOrder'); $attributeSearchCriteria = $this->getBasicAttributeSearchCriteriaBuilder()->create(); $attributeGroupSearchCriteria = $this->getSearchCriteriaBuilder()->addFilter('attribute_set_id', $attributeSet->getAttributeSetId())->addFilter('attribute_group_code', $groupCode)->addSortOrder($this->getSortOrderBuilder()->setAscendingDirection()->create())->setPageSize(1)->create(); try { /** @var AttributeGroupInterface[] $attributeGroupItems */ $attributeGroupItems = $this->getAttributeGroupRepository()->getList($attributeGroupSearchCriteria)->getItems(); if (!$attributeGroupItems) { throw new \Magento\Framework\Exception\NoSuchEntityException(); } /** @var AttributeGroupInterface $attributeGroup */ $attributeGroup = reset($attributeGroupItems); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** @var AttributeGroupInterface $attributeGroup */ $attributeGroup = $this->getAttributeGroupFactory()->create(); } $extensionAttributes = $attributeGroup->getExtensionAttributes() ?: $this->getExtensionAttributesFactory()->create(AttributeGroupInterface::class); $extensionAttributes->setAttributeGroupCode($groupCode); $extensionAttributes->setSortOrder($groupSortOrder); $attributeGroup->setAttributeGroupName($groupName); $attributeGroup->setAttributeSetId($attributeSet->getAttributeSetId()); $attributeGroup->setExtensionAttributes($extensionAttributes); $this->getAttributeGroupRepository()->save($attributeGroup); /** @var AttributeInterface[] $attributesItems */ $attributesItems = $this->getAttributeRepository()->getList(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeSearchCriteria)->getItems(); array_walk($attributesItems, function (AttributeInterface $attribute) use($attributeSet, $attributeGroup) { $this->getAttributeManagement()->assign(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeSet->getAttributeSetId(), $attributeGroup->getAttributeGroupId(), $attribute->getAttributeCode(), '0'); }); } catch (LocalizedException $e) { $response->setError(true); $response->setMessage($e->getMessage()); } catch (\Exception $e) { $this->getLogger()->critical($e); $response->setError(true); $response->setMessage(__('Unable to add attribute')); } return $this->resultJsonFactory->create()->setJsonData($response->toJson()); }