Create a Module named Jeff_CustomLinked
registration.php:
etc/module.xml:
Set up a InstallData script
Setup/InstallData.php:
Model/Product.php:\Jeff\CustomLinked\Model\Product\Link::LINK_TYPE_CUSTOMLINKED, 'code' => 'customlinked'], ]; foreach($data as $bind) { $setup->getConnection()->insertForce($setup->getTable('catalog_product_link_type'), $bind); } $data = [ [ 'link_type_id' => \Jeff\CustomLinked\Model\Product\Link::LINK_TYPE_CUSTOMLINKED, 'product_link_attribute_code' => 'position', 'data_type' => 'int', ] ]; $setup->getConnection()->insertMultiple($setup->getTable('catalog_product_link_attribute'), $data); } }
Model/Product/Link.php:hasCustomlinkedProducts()) { $products = []; $collection = $this->getCustomlinkedProductCollection(); foreach ($collection as $product) { $products[] = $product; } $this->setCustomlinkedProducts($products); } return $this->getData('customlinked_products'); } /** * Retrieve related products identifiers * * @return array */ public function getCustomlinkedProductIds() { if (!$this->hasCustomlinkedProductIds()) { $ids = []; foreach ($this->getCustomlinkedProducts() as $product) { $ids[] = $product->getId(); } $this->setCustomlinkedProductIds($ids); } return [$this->getData('customlinked_product_ids')]; } /** * Retrieve collection related product * * @return \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */ public function getCustomlinkedProductCollection() { $collection = $this->getLinkInstance()->setLinkTypeId(static::LINK_TYPE_CUSTOMLINKED)->getProductCollection()->setIsStrongMode(); $collection->setProduct($this); return $collection; } }
Model/ProductLink/CollectionProvider/CustomLinked.php:
etc/di.xml:getCustomlinkedProducts(); } }
- Jeff\CustomLinked\Model\Product\Link::LINK_TYPE_CUSTOMLINKED
- Jeff\CustomLinked\Model\ProductLink\CollectionProvider\Customlinked
etc/adminhtml/di.xml
Ui Component Data Provider
Ui/DataProvider/Product/Related/CustomLinkedDataProvider.php:
Plugin for product related grid
Plugin/Related.php:scopePrefix . static::DATA_SCOPE_CUSTOMLINKED] = $this->getCustomlinkedFieldset($modify); } return $result; } /** * Get price modifier * * @return \Magento\Catalog\Ui\Component\Listing\Columns\Price * @deprecated 101.0.0 */ private function getPriceModifier($modify) { if (!$this->priceModifier) { $this->priceModifier = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Catalog\Ui\Component\Listing\Columns\Price::class ); } return $this->priceModifier; } /** * Prepares config for the Related products fieldset * * @return array * @since 101.0.0 */ protected function getCustomlinkedFieldset($modify) { $content = __( 'My Custom Linked Product.' ); return [ 'children' => [ 'button_set' => $modify->getButtonSet( $content, __('Add Custom Linked'), $modify->scopePrefix . static::DATA_SCOPE_CUSTOMLINKED ), 'modal' => $this->getGenericModal( __('Add Custom Linked'), $modify->scopePrefix . static::DATA_SCOPE_CUSTOMLINKED ), static::DATA_SCOPE_CUSTOMLINKED => $this->getGrid($modify->scopePrefix . static::DATA_SCOPE_CUSTOMLINKED), ], 'arguments' => [ 'data' => [ 'config' => [ 'additionalClasses' => 'admin__fieldset-section', 'label' => __('Custom Linked'), 'collapsible' => false, 'componentType' => Fieldset::NAME, 'dataScope' => '', 'sortOrder' => 11, ], ], ] ]; } public function afterModifyData($modify , $data) { $product = $modify->locator->getProduct(); $productId = $product->getId(); if (!$productId) { return $data; } $priceModifier = $this->getPriceModifier($modify); /** * Set field name for modifier */ $priceModifier->setData('name', 'price'); $dataScopes = $this->getDataScopes(); $dataScopes[] = static::DATA_SCOPE_CUSTOMLINKED; foreach ($dataScopes as $dataScope) { if($dataScope == static::DATA_SCOPE_CUSTOMLINKED){ $data[$productId]['links'][$dataScope] = []; foreach ($modify->productLinkRepository->getList($product) as $linkItem) { if ($linkItem->getLinkType() !== $dataScope) { continue; } /** @var \Magento\Catalog\Model\Product $linkedProduct */ $linkedProduct = $modify->productRepository->get( $linkItem->getLinkedProductSku(), false, $modify->locator->getStore()->getId() ); $data[$productId]['links'][$dataScope][] = $this->fillData($linkedProduct, $linkItem); } if (!empty($data[$productId]['links'][$dataScope])) { $dataMap = $priceModifier->prepareDataSource([ 'data' => [ 'items' => $data[$productId]['links'][$dataScope] ] ]); $data[$productId]['links'][$dataScope] = $dataMap['data']['items']; } } } return $data; } public function beforeGetLinkedProducts($provider, $product) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->product = $objectManager->create('Jeff\CustomLinked\Model\Product'); $currentProduct = $this->product->load($product->getId()); return [$currentProduct]; } }
ui component xml configuration
view/adminhtml/ui_component/customlinked_product_listing.xml:- customlinked_product_listing.customlinked_product_listing_data_source
- customlinked_product_listing.customlinked_product_listing_data_source
- product_columns
- Magento_Catalog::products
Jeff\CustomLinked\Ui\DataProvider\Product\Related\CustomlinkedDataProvider customlinked_product_listing_data_source entity_id id - Magento_Ui/js/grid/provider
- false
- false
- Magento\Catalog\Ui\Component\Listing\Filters
- customlinkedProductGrid
- selectProduct
- ${ $.$data.rowIndex }
- entity_id
- 0
- true
- textRange
- asc
- ID
- 10
- Magento_Ui/js/grid/columns/thumbnail
- true
- false
- name
- 1
- left
- Thumbnail
- 20
- text
- true
- Name
- 30
- Magento\Catalog\Model\Product\AttributeSet\Options
- select
- Magento_Ui/js/grid/columns/select
- select
- Attribute Set
- 40
- 41
- AttributeSetText
- false
- Magento\Catalog\Model\Product\Attribute\Source\Status
- select
- Magento_Ui/js/grid/columns/select
- select
- Status
- 50
- 51
- StatusText
- false
- Magento\Catalog\Model\Product\Type
- select
- Magento_Ui/js/grid/columns/select
- select
- Type
- 60
- text
- SKU
- 70
- textRange
- true
- Price
- 80
After following all these steps as above, you can add your custom linked products to any product shown as following image:
You can download the source code at My GitHub