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


PHP Product::getProductLinks方法代码示例

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


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

示例1: testSetProductLinks

 /**
  *  Test for setProductLinks()
  */
 public function testSetProductLinks()
 {
     $link = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $link->setSku("Simple Product 1");
     $link->setLinkType("upsell");
     $link->setLinkedProductSku("Simple Product 2");
     $link->setLinkedProductType("simple");
     $link->setPosition(0);
     $productLinks = [$link];
     $this->model->setProductLinks($productLinks);
     $this->assertEquals($productLinks, $this->model->getProductLinks());
 }
开发者ID:hientruong90,项目名称:magento2_installer,代码行数:15,代码来源:ProductTest.php

示例2: beforeInitializeLinks

 /**
  * Initialize grouped product links
  *
  * @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject
  * @param \Magento\Catalog\Model\Product $product
  * @param array $links
  *
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function beforeInitializeLinks(\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject, \Magento\Catalog\Model\Product $product, array $links)
 {
     if ($product->getTypeId() === TypeGrouped::TYPE_CODE && !$product->getGroupedReadonly()) {
         $links = isset($links[self::TYPE_NAME]) ? $links[self::TYPE_NAME] : $product->getGroupedLinkData();
         if (!is_array($links)) {
             $links = [];
         }
         if ($product->getGroupedLinkData()) {
             $links = array_merge($links, $product->getGroupedLinkData());
         }
         $newLinks = [];
         $existingLinks = $product->getProductLinks();
         foreach ($links as $linkRaw) {
             /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
             $productLink = $this->productLinkFactory->create();
             if (!isset($linkRaw['id'])) {
                 continue;
             }
             $productId = $linkRaw['id'];
             if (!isset($linkRaw['qty'])) {
                 $linkRaw['qty'] = 0;
             }
             $linkedProduct = $this->productRepository->getById($productId);
             $productLink->setSku($product->getSku())->setLinkType(self::TYPE_NAME)->setLinkedProductSku($linkedProduct->getSku())->setLinkedProductType($linkedProduct->getTypeId())->setPosition($linkRaw['position'])->getExtensionAttributes()->setQty($linkRaw['qty']);
             if (isset($linkRaw['custom_attributes'])) {
                 $productLinkExtension = $productLink->getExtensionAttributes();
                 if ($productLinkExtension === null) {
                     $productLinkExtension = $this->productLinkExtensionFactory->create();
                 }
                 foreach ($linkRaw['custom_attributes'] as $option) {
                     $name = $option['attribute_code'];
                     $value = $option['value'];
                     $setterName = 'set' . ucfirst($name);
                     // Check if setter exists
                     if (method_exists($productLinkExtension, $setterName)) {
                         call_user_func([$productLinkExtension, $setterName], $value);
                     }
                 }
                 $productLink->setExtensionAttributes($productLinkExtension);
             }
             $newLinks[] = $productLink;
         }
         $existingLinks = $this->removeUnExistingLinks($existingLinks, $newLinks);
         $product->setProductLinks(array_merge($existingLinks, $newLinks));
     }
 }
开发者ID:BlackIkeEagle,项目名称:magento2-continuousphp,代码行数:58,代码来源:Grouped.php

示例3: convertLinksToGroupedArray

 /**
  * @param \Magento\Catalog\Model\Product $entity
  * @return array
  */
 public function convertLinksToGroupedArray($entity)
 {
     $basicData = $entity->getProductLinks();
     $associatedProducts = $entity->getTypeInstance()->getAssociatedProducts($entity);
     $associatedProducts = $this->indexBySku($associatedProducts);
     $linksAsArray = [];
     /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $link */
     foreach ($basicData as $link) {
         $info = $link->getData();
         if ($link->getLinkType() == 'associated') {
             $info['id'] = $associatedProducts[$link->getLinkedProductSku()]->getId();
         }
         $info = array_merge($info, $link->getExtensionAttributes()->__toArray());
         $linksAsArray[$link->getLinkType()][] = $info;
     }
     return $linksAsArray;
 }
开发者ID:Doability,项目名称:magento2dev,代码行数:21,代码来源:Converter.php

示例4: testGetProductLinks

 /**
  *  Test for getProductLinks() with associated product links
  */
 public function testGetProductLinks()
 {
     $this->markTestIncomplete('Skipped due to https://jira.corp.x.com/browse/MAGETWO-36926');
     $linkTypes = ['related' => 1, 'upsell' => 4, 'crosssell' => 5, 'associated' => 3];
     $this->linkTypeProviderMock->expects($this->once())->method('getLinkTypes')->willReturn($linkTypes);
     $inputRelatedLink = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $inputRelatedLink->setProductSku("Simple Product 1");
     $inputRelatedLink->setLinkType("related");
     $inputRelatedLink->setData("sku", "Simple Product 2");
     $inputRelatedLink->setData("type", "simple");
     $inputRelatedLink->setPosition(0);
     $customData = ["attribute_code" => "qty", "value" => 1];
     $inputGroupLink = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $inputGroupLink->setProductSku("Simple Product 1");
     $inputGroupLink->setLinkType("associated");
     $inputGroupLink->setData("sku", "Simple Product 2");
     $inputGroupLink->setData("type", "simple");
     $inputGroupLink->setPosition(0);
     $inputGroupLink["custom_attributes"] = [$customData];
     $outputRelatedLink = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $outputRelatedLink->setProductSku("Simple Product 1");
     $outputRelatedLink->setLinkType("related");
     $outputRelatedLink->setLinkedProductSku("Simple Product 2");
     $outputRelatedLink->setLinkedProductType("simple");
     $outputRelatedLink->setPosition(0);
     $groupExtension = $this->objectManagerHelper->getObject('Magento\\Catalog\\Api\\Data\\ProductLinkExtension');
     $reflectionOfExtension = new \ReflectionClass('Magento\\Catalog\\Api\\Data\\ProductLinkExtension');
     $method = $reflectionOfExtension->getMethod('setData');
     $method->setAccessible(true);
     $method->invokeArgs($groupExtension, array('qty', 1));
     $outputGroupLink = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $outputGroupLink->setProductSku("Simple Product 1");
     $outputGroupLink->setLinkType("associated");
     $outputGroupLink->setLinkedProductSku("Simple Product 2");
     $outputGroupLink->setLinkedProductType("simple");
     $outputGroupLink->setPosition(0);
     $outputGroupLink->setExtensionAttributes($groupExtension);
     $this->entityCollectionProviderMock->expects($this->at(0))->method('getCollection')->with($this->model, 'related')->willReturn([$inputRelatedLink]);
     $this->entityCollectionProviderMock->expects($this->at(1))->method('getCollection')->with($this->model, 'upsell')->willReturn([]);
     $this->entityCollectionProviderMock->expects($this->at(2))->method('getCollection')->with($this->model, 'crosssell')->willReturn([]);
     $this->entityCollectionProviderMock->expects($this->at(3))->method('getCollection')->with($this->model, 'associated')->willReturn([$inputGroupLink]);
     $expectedOutput = [$outputRelatedLink, $outputGroupLink];
     $typeInstanceMock = $this->getMock('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Simple', ["getSku"], [], '', false);
     $typeInstanceMock->expects($this->atLeastOnce())->method('getSku')->willReturn("Simple Product 1");
     $this->model->setTypeInstance($typeInstanceMock);
     $productLink1 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $productLink2 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\ProductLink\\Link');
     $this->productLinkFactory->expects($this->at(0))->method('create')->willReturn($productLink1);
     $this->productLinkFactory->expects($this->at(1))->method('create')->willReturn($productLink2);
     $extension = $this->objectManagerHelper->getObject('Magento\\Catalog\\Api\\Data\\ProductLinkExtension');
     $productLink2->setExtensionAttributes($extension);
     $links = $this->model->getProductLinks();
     // Match the links
     $matches = 0;
     foreach ($links as $link) {
         foreach ($expectedOutput as $expected) {
             if ($expected->getData() == $link->getData()) {
                 $matches++;
             }
         }
     }
     $this->assertEquals($matches, 2);
 }
开发者ID:kidaa30,项目名称:magento2-platformsh,代码行数:66,代码来源:ProductTest.php

示例5: getProductLinks

 /**
  * {@inheritdoc}
  */
 public function getProductLinks()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getProductLinks');
     if (!$pluginInfo) {
         return parent::getProductLinks();
     } else {
         return $this->___callPlugins('getProductLinks', func_get_args(), $pluginInfo);
     }
 }
开发者ID:dragonsword007008,项目名称:magento2,代码行数:12,代码来源:Interceptor.php


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