本文整理汇总了PHP中Magento\Catalog\Model\Product::setProductLinks方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::setProductLinks方法的具体用法?PHP Product::setProductLinks怎么用?PHP Product::setProductLinks使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\Product
的用法示例。
在下文中一共展示了Product::setProductLinks方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: setProductLinks
/**
* Setting product links
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function setProductLinks(\Magento\Catalog\Model\Product $product)
{
$links = $this->getLinkResolver()->getLinks();
$product->setProductLinks([]);
$product = $this->productLinks->initializeLinks($product, $links);
$productLinks = $product->getProductLinks();
$linkTypes = ['related' => $product->getRelatedReadonly(), 'upsell' => $product->getUpsellReadonly(), 'crosssell' => $product->getCrosssellReadonly()];
foreach ($linkTypes as $linkType => $readonly) {
if (isset($links[$linkType]) && !$readonly) {
foreach ((array) $links[$linkType] as $linkData) {
if (empty($linkData['id'])) {
continue;
}
$linkProduct = $this->getProductRepository()->getById($linkData['id']);
$link = $this->getProductLinkFactory()->create();
$link->setSku($product->getSku())->setLinkedProductSku($linkProduct->getSku())->setLinkType($linkType)->setPosition(isset($linkData['position']) ? (int) $linkData['position'] : 0);
$productLinks[] = $link;
}
}
}
return $product->setProductLinks($productLinks);
}
示例3: 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)
*/
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 ($product->getGroupedLinkData()) {
$links = array_merge($links, $product->getGroupedLinkData());
}
$newLinks = [];
$existingLinks = $product->getProductLinks();
if ($links) {
foreach ($links as $linkId => $linkRaw) {
/** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
$productLink = $this->productLinkFactory->create();
if (isset($linkRaw['id'])) {
$productId = $linkRaw['id'];
} else {
$productId = $linkId;
}
$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));
}
}
示例4: setProductLinks
/**
* {@inheritdoc}
*/
public function setProductLinks(array $links = null)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'setProductLinks');
if (!$pluginInfo) {
return parent::setProductLinks($links);
} else {
return $this->___callPlugins('setProductLinks', func_get_args(), $pluginInfo);
}
}