本文整理汇总了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());
}
示例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));
}
}
示例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;
}
示例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);
}
示例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);
}
}