Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Link.php
Go to the documentation of this file.
1 <?php
7 
10 
27 {
28  const LINK_TYPE_RELATED = 1;
29 
30  const LINK_TYPE_UPSELL = 4;
31 
33 
37  protected $_attributes = null;
38 
45 
52 
57  protected $saveProductLinks;
58 
63  protected $stockHelper;
64 
75  public function __construct(
76  \Magento\Framework\Model\Context $context,
77  \Magento\Framework\Registry $registry,
78  \Magento\Catalog\Model\ResourceModel\Product\Link\CollectionFactory $linkCollectionFactory,
79  \Magento\Catalog\Model\ResourceModel\Product\Link\Product\CollectionFactory $productCollectionFactory,
80  \Magento\CatalogInventory\Helper\Stock $stockHelper,
81  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
82  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
83  array $data = []
84  ) {
85  $this->_linkCollectionFactory = $linkCollectionFactory;
86  $this->_productCollectionFactory = $productCollectionFactory;
87  $this->stockHelper = $stockHelper;
88  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
89  }
90 
96  protected function _construct()
97  {
98  $this->_init(\Magento\Catalog\Model\ResourceModel\Product\Link::class);
99  }
100 
104  public function useRelatedLinks()
105  {
106  $this->setLinkTypeId(self::LINK_TYPE_RELATED);
107  return $this;
108  }
109 
113  public function useUpSellLinks()
114  {
115  $this->setLinkTypeId(self::LINK_TYPE_UPSELL);
116  return $this;
117  }
118 
122  public function useCrossSellLinks()
123  {
124  $this->setLinkTypeId(self::LINK_TYPE_CROSSSELL);
125  return $this;
126  }
127 
134  public function getAttributeTypeTable($type)
135  {
136  return $this->_getResource()->getAttributeTypeTable($type);
137  }
138 
144  public function getProductCollection()
145  {
146  $collection = $this->_productCollectionFactory->create()->setLinkModel($this);
147  return $collection;
148  }
149 
155  public function getLinkCollection()
156  {
157  $collection = $this->_linkCollectionFactory->create()->setLinkModel($this);
158  return $collection;
159  }
160 
165  public function getAttributes($type = null)
166  {
167  if ($type === null) {
168  $type = $this->getLinkTypeId();
169  }
170  if (!isset($this->_attributes[$type])) {
171  $this->_attributes[$type] = $this->_getResource()->getAttributesByType($type);
172  }
173 
174  return $this->_attributes[$type];
175  }
176 
184  {
185  $this->getProductLinkSaveHandler()->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $product);
186  return $this;
187  }
188 
192  private function getProductLinkSaveHandler()
193  {
194  if (null === $this->saveProductLinks) {
195  $this->saveProductLinks = \Magento\Framework\App\ObjectManager::getInstance()
196  ->get(\Magento\Catalog\Model\Product\Link\SaveHandler::class);
197  }
199  }
200 }
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13