当前位置: 首页>>代码示例>>PHP>>正文


PHP ProductInterface::getCopyFromView方法代码示例

本文整理汇总了PHP中Magento\Catalog\Api\Data\ProductInterface::getCopyFromView方法的典型用法代码示例。如果您正苦于以下问题:PHP ProductInterface::getCopyFromView方法的具体用法?PHP ProductInterface::getCopyFromView怎么用?PHP ProductInterface::getCopyFromView使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Magento\Catalog\Api\Data\ProductInterface的用法示例。


在下文中一共展示了ProductInterface::getCopyFromView方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: addChild

 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function addChild(\Magento\Catalog\Api\Data\ProductInterface $product, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct)
 {
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new InputException(__('Product with specified sku: "%1" is not a bundle product', $product->getSku()));
     }
     $options = $this->optionCollection->create();
     $options->setIdFilter($optionId);
     $existingOption = $options->getFirstItem();
     if (!$existingOption->getId()) {
         throw new InputException(__('Product with specified sku: "%1" does not contain option: "%2"', [$product->getSku(), $optionId]));
     }
     $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
     /* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
     $resource = $this->bundleFactory->create();
     $selections = $resource->getSelectionsData($product->getData($linkField));
     /** @var \Magento\Catalog\Model\Product $linkProductModel */
     $linkProductModel = $this->productRepository->get($linkedProduct->getSku());
     if ($linkProductModel->isComposite()) {
         throw new InputException(__('Bundle product could not contain another composite product'));
     }
     if ($selections) {
         foreach ($selections as $selection) {
             if ($selection['option_id'] == $optionId && $selection['product_id'] == $linkProductModel->getEntityId()) {
                 if (!$product->getCopyFromView()) {
                     throw new CouldNotSaveException(__('Child with specified sku: "%1" already assigned to product: "%2"', [$linkedProduct->getSku(), $product->getSku()]));
                 } else {
                     return $this->bundleSelection->create()->load($linkProductModel->getEntityId());
                 }
             }
         }
     }
     $selectionModel = $this->bundleSelection->create();
     $selectionModel = $this->mapProductLinkToSelectionModel($selectionModel, $linkedProduct, $linkProductModel->getEntityId(), $product->getData($linkField));
     $selectionModel->setOptionId($optionId);
     try {
         $selectionModel->save();
         $resource->addProductRelation($product->getData($linkField), $linkProductModel->getEntityId());
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
     }
     return $selectionModel->getId();
 }
开发者ID:Doability,项目名称:magento2dev,代码行数:46,代码来源:LinkManagement.php


注:本文中的Magento\Catalog\Api\Data\ProductInterface::getCopyFromView方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。