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


PHP ProductInterface::getSku方法代码示例

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


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

示例1: create

 /**
  * Populate product with variation of attributes
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param array $attributes
  * @return \Magento\Catalog\Api\Data\ProductInterface[]
  */
 public function create(\Magento\Catalog\Api\Data\ProductInterface $product, $attributes)
 {
     $variations = $this->variationMatrix->getVariations($attributes);
     $products = [];
     foreach ($variations as $variation) {
         $price = $product->getPrice();
         /** @var \Magento\Catalog\Model\Product $item */
         $item = $this->productFactory->create();
         $item->setData($product->getData());
         $suffix = '';
         foreach ($variation as $attributeId => $valueInfo) {
             $suffix .= '-' . $valueInfo['value'];
             $customAttribute = $this->customAttributeFactory->create()->setAttributeCode($attributes[$attributeId]['attribute_code'])->setValue($valueInfo['value']);
             $customAttributes = array_merge($item->getCustomAttributes(), [$attributes[$attributeId]['attribute_code'] => $customAttribute]);
             $item->setData('custom_attributes', $customAttributes);
             $priceInfo = $valueInfo['price'];
             $price += (!empty($priceInfo['is_percent']) ? $product->getPrice() / 100.0 : 1.0) * $priceInfo['pricing_value'];
         }
         $item->setPrice($price);
         $item->setName($product->getName() . $suffix);
         $item->setSku($product->getSku() . $suffix);
         $item->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE);
         $products[] = $item;
     }
     return $products;
 }
开发者ID:shabbirvividads,项目名称:magento2,代码行数:33,代码来源:ProductVariationsBuilder.php

示例2: addChild

 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 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->setProductIdFilter($product->getId())->joinValues($this->storeManager->getStore()->getId());
     $isNewOption = true;
     /** @var \Magento\Bundle\Model\Option $option */
     foreach ($options as $option) {
         if ($option->getOptionId() == $optionId) {
             $isNewOption = false;
             break;
         }
     }
     if ($isNewOption) {
         throw new InputException(__('Product with specified sku: "%1" does not contain option: "%2"', [$product->getSku(), $optionId]));
     }
     /* @var $resource \Magento\Bundle\Model\Resource\Bundle */
     $resource = $this->bundleFactory->create();
     $selections = $resource->getSelectionsData($product->getId());
     /** @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->getId()) {
                 throw new CouldNotSaveException(__('Child with specified sku: "%1" already assigned to product: "%2"', [$linkedProduct->getSku(), $product->getSku()]));
             }
         }
     }
     $selectionModel = $this->bundleSelection->create();
     $selectionModel->setOptionId($optionId)->setPosition($linkedProduct->getPosition())->setSelectionQty($linkedProduct->getQty())->setSelectionPriceType($linkedProduct->getPriceType())->setSelectionPriceValue($linkedProduct->getPrice())->setSelectionCanChangeQty($linkedProduct->getCanChangeQuantity())->setProductId($linkProductModel->getId())->setParentProductId($product->getId())->setIsDefault($linkedProduct->getIsDefault())->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
     try {
         $selectionModel->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
     }
     return $selectionModel->getId();
 }
开发者ID:shabbirvividads,项目名称:magento2,代码行数:47,代码来源:LinkManagement.php

示例3: delete

 /**
  * {@inheritdoc}
  */
 public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
 {
     $sku = $product->getSku();
     $productId = $product->getId();
     try {
         $this->resourceModel->delete($product);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\StateException(__('Unable to remove product %1', $sku));
     }
     unset($this->instances[$sku]);
     unset($this->instancesById[$productId]);
     return true;
 }
开发者ID:nblair,项目名称:magescotch,代码行数:16,代码来源:ProductRepository.php

示例4: saveSamples

 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\Downloadable\Api\Data\SampleInterface[] $samples
  * @return $this
  */
 protected function saveSamples(\Magento\Catalog\Api\Data\ProductInterface $product, array $samples)
 {
     $existingSampleIds = [];
     $extensionAttributes = $product->getExtensionAttributes();
     if ($extensionAttributes !== null) {
         $existingSamples = $extensionAttributes->getDownloadableProductSamples();
         if ($existingSamples !== null) {
             foreach ($existingSamples as $existingSample) {
                 $existingSampleIds[] = $existingSample->getId();
             }
         }
     }
     $updatedSampleIds = [];
     foreach ($samples as $sample) {
         $sampleId = $sample->getId();
         if ($sampleId) {
             $updatedSampleIds[] = $sampleId;
         }
         $this->sampleRepository->save($product->getSku(), $sample);
     }
     $sampleIdsToDelete = array_diff($existingSampleIds, $updatedSampleIds);
     foreach ($sampleIdsToDelete as $sampleId) {
         $this->sampleRepository->delete($sampleId);
     }
     return $this;
 }
开发者ID:kidaa30,项目名称:magento2-platformsh,代码行数:31,代码来源:AroundProductRepositorySave.php

示例5: 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

示例6: deleteConfigurableProductAttributes

 /**
  * Remove product attributes
  *
  * @param ProductInterface $product
  * @return void
  */
 private function deleteConfigurableProductAttributes(ProductInterface $product)
 {
     $list = $this->optionRepository->getList($product->getSku());
     foreach ($list as $item) {
         $this->optionRepository->deleteById($product->getSku(), $item->getId());
     }
 }
开发者ID:Doability,项目名称:magento2dev,代码行数:13,代码来源:SaveHandler.php

示例7: updateOptionSelection

 /**
  * Update option selections
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\Bundle\Api\Data\OptionInterface $option
  * @return $this
  */
 protected function updateOptionSelection(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option)
 {
     $optionId = $option->getOptionId();
     $existingLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
     $linksToAdd = [];
     $linksToUpdate = [];
     $linksToDelete = [];
     if (is_array($option->getProductLinks())) {
         $productLinks = $option->getProductLinks();
         foreach ($productLinks as $productLink) {
             if (!$productLink->getId()) {
                 $linksToAdd[] = $productLink;
             } else {
                 $linksToUpdate[] = $productLink;
             }
         }
         /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
         $linksToDelete = $this->compareLinks($linksToUpdate, $existingLinks);
     }
     foreach ($linksToUpdate as $linkedProduct) {
         $this->linkManagement->saveChild($product->getSku(), $linkedProduct);
     }
     foreach ($linksToDelete as $linkedProduct) {
         $this->linkManagement->removeChild($product->getSku(), $option->getOptionId(), $linkedProduct->getSku());
     }
     foreach ($linksToAdd as $linkedProduct) {
         $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
     }
     return $this;
 }
开发者ID:whoople,项目名称:magento2-testing,代码行数:37,代码来源:OptionRepository.php

示例8: saveConfigurableProductOptions

 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\ConfigurableProduct\Api\Data\OptionInterface[] $options
  * @return $this
  */
 protected function saveConfigurableProductOptions(\Magento\Catalog\Api\Data\ProductInterface $product, array $options)
 {
     $existingOptionIds = [];
     if ($product->getExtensionAttributes() !== null) {
         $extensionAttributes = $product->getExtensionAttributes();
         if ($extensionAttributes->getConfigurableProductOptions() !== null) {
             $existingOptions = $extensionAttributes->getConfigurableProductOptions();
             foreach ($existingOptions as $option) {
                 $existingOptionIds[] = $option->getId();
             }
         }
     }
     $updatedOptionIds = [];
     foreach ($options as $option) {
         if ($option->getId()) {
             $updatedOptionIds[] = $option->getId();
         }
         $this->optionRepository->save($product->getSku(), $option);
     }
     $optionIdsToDelete = array_diff($existingOptionIds, $updatedOptionIds);
     foreach ($optionIdsToDelete as $optionId) {
         $this->optionRepository->deleteById($product->getSku(), $optionId);
     }
     return $this;
 }
开发者ID:tingyeeh,项目名称:magento2,代码行数:30,代码来源:AroundProductRepositorySave.php


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