コード例 #1
0
ファイル: Plugin.php プロジェクト: Doability/magento2dev
 /**
  * @param \Magento\Catalog\Controller\Adminhtml\Product\Builder $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  *
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function aroundBuild(\Magento\Catalog\Controller\Adminhtml\Product\Builder $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     $product = $proceed($request);
     if ($request->has('attributes')) {
         $attributes = $request->getParam('attributes');
         if (!empty($attributes)) {
             $product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE);
             $this->configurableType->setUsedProductAttributes($product, $attributes);
         } else {
             $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
         }
     }
     // Required attributes of simple product for configurable creation
     if ($request->getParam('popup') && ($requiredAttributes = $request->getParam('required'))) {
         $requiredAttributes = explode(",", $requiredAttributes);
         foreach ($product->getAttributes() as $attribute) {
             if (in_array($attribute->getId(), $requiredAttributes)) {
                 $attribute->setIsRequired(1);
             }
         }
     }
     if ($request->getParam('popup') && $request->getParam('product') && !is_array($request->getParam('product')) && $request->getParam('id', false) === false) {
         $configProduct = $this->productFactory->create();
         $configProduct->setStoreId(0)->load($request->getParam('product'))->setTypeId($request->getParam('type'));
         $data = [];
         foreach ($configProduct->getTypeInstance()->getSetAttributes($configProduct) as $attribute) {
             /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
             if (!$attribute->getIsUnique() && $attribute->getFrontend()->getInputType() != 'gallery' && $attribute->getAttributeCode() != 'required_options' && $attribute->getAttributeCode() != 'has_options' && $attribute->getAttributeCode() != $configProduct->getIdFieldName()) {
                 $data[$attribute->getAttributeCode()] = $configProduct->getData($attribute->getAttributeCode());
             }
         }
         $product->addData($data);
         $product->setWebsiteIds($configProduct->getWebsiteIds());
     }
     return $product;
 }