Skip to content

Instantly share code, notes, and snippets.

@davidalexander
Created January 4, 2021 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidalexander/c06c9d3b0ebef491b8ecd06668c178f9 to your computer and use it in GitHub Desktop.
Save davidalexander/c06c9d3b0ebef491b8ecd06668c178f9 to your computer and use it in GitHub Desktop.
Skywire / m2-module-linked-products

m2-module-linked-products

Link products to each other with user configurable types.

Installation

Install via composer

composer require skywire/linkedproducts

Configure link types

Link types can be managed from the admin

Admin -> Products -> Type Management

  • Code is the type identifier and will be used to retrieve products by type.
  • Label can be displayed on the frontend, and is used in the admin product view form
  • SKU pattern is a regex used by the auto link cron, this will find all products with a matching pattern, and link them to each other ie: /sku-m/ notice no use of * and the addition of the slashes.
  • Product types are used by the auto link cron, only products of the selected types will be linked.

Creating and updating product links

Link Import

Admin -> Products -> Import Product Links

Provide a CSV containing these fields

  • sku
  • type_code
  • linked_skus

Example CSV

"sku","type_code","linked_skus"
"24-MB01 ","Type_1","24-MB04,24-MB03,24-MB05"
"24-MB04","Type_1","24-MB01,24-MB03,24-MB05"

Note that the linked_skus are comma separated within a single field

Running the import will delete any existing links for any products found in the sku field before creating new links

Link Export

Admin -> Products -> Export Product Links

Exports all existing links, it is advised to import your links, then if you need to make a change you can export, modify and re-import.

Auto Link

Admin -> Stores -> Configuration -> Catalog -> Skywire Linked Products

A cron can be configured to automatically create links based on the SKU pattern, this method is advised if you are trying to link products with consistent SKU patterns eg.

  • prod1-32
  • prod1-33
  • prod1-34

So you could use a regex to match the first part of the SKU, and the auto link will find all product SKUs starting in prod1 and link them to each other

Viewing existing links

Links can be viewed (but not edited) from the admin product edit form, under the Linked Products tab.

Accessing product links in blocks and templates

A helper has been provided for this purpose.

Skywire\LinkedProducts\Helper\ProductHelper

Call getLinkedProductCollection with the link type code and the product ID, and it will return either a product collection or false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment