Skip to content

Commit

Permalink
Merge pull request #38 from ctidigital/product-links
Browse files Browse the repository at this point in the history
Product links
  • Loading branch information
chevli authored Jul 14, 2017
2 parents a151784 + 9a294b8 commit 25a44a4
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 8 deletions.
148 changes: 148 additions & 0 deletions Model/Component/ProductLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace CtiDigital\Configurator\Model\Component;

use CtiDigital\Configurator\Model\LoggingInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
use Magento\Framework\ObjectManagerInterface;
use CtiDigital\Configurator\Model\Exception\ComponentException;

class ProductLinks extends YamlComponentAbstract
{

protected $alias = 'product_links';
protected $name = 'Product Links';
protected $description = 'Component to create and maintain product links (related/up-sells/cross-sells)';


// @var Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory
protected $productLinkFactory;

protected $productRepository;

protected $allowedLinks = ['relation', 'up_sell', 'cross_sell'];
protected $linkTypeMap = ['relation' => 'related', 'up_sell' => 'upsell', 'cross_sell' => 'crosssell'];

public function __construct(
LoggingInterface $log,
ObjectManagerInterface $objectManager,
ProductRepositoryInterface $productRepository,
ProductLinkInterfaceFactory $productLinkFactory
) {
parent::__construct($log, $objectManager);
$this->productRepository = $productRepository;
$this->productLinkFactory = $productLinkFactory;
}

/**
* Process the data by splitting up the different link types.
*
* @param $data
*/
public function processData($data = null)
{
try {

// Loop through all the product link types - if there are multiple link types in the yaml file
foreach ($data as $linkType => $skus) {

// Validate the link type to see if it is allowed
if (!in_array($linkType, $this->allowedLinks)) {
throw new ComponentException(sprintf('Link type %s is not supported', $linkType));
}

// Process creating the links
$this->processSkus($skus, $linkType);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
} catch (\Exception $e) {
$this->log->logError($e->getMessage());
}
}

/**
* Process an array of products that require products linking to them
*
* @param array $data
* @param $linkType
*/
private function processSkus(array $data, $linkType)
{
try {

// Loop through the SKUs in the link type
foreach ($data as $sku => $linkSkus) {

// Check if the product exists
if (!$this->doesProductExist($sku)) {
throw new ComponentException(sprintf('SKU (%s) for products to link to is not found', $sku));
}
$this->log->logInfo(sprintf('Creating product links for %s', $sku));

// Process the links for that product
$this->processLinks($sku, $linkSkus, $linkType);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
} catch (\Exception $e) {
$this->log->logError($e->getMessage());
}
}

/**
* Process all the SKUs that need to be linked to a particular product (SKU)
*
* @param $sku
* @param $linkSkus
* @param $linkType
*/
private function processLinks($sku, $linkSkus, $linkType)
{
try {

$productLinks = array();

// Loop through all the products that require linking to a product
foreach ($linkSkus as $position => $linkSku) {

// Check if the product exists
if (!$this->doesProductExist($linkSku)) {
throw new ComponentException(sprintf('SKU (%s) to link does not exist', $linkSku));
}

// Create an array of product link objects
$productLinks[] = $this->productLinkFactory->create()->setSku($sku)
->setLinkedProductSku($linkSku)
->setLinkType($this->linkTypeMap[$linkType])
->setPosition($position * 10);
$this->log->logInfo($linkSku, 1);
}

// Save product links onto the main product
$this->productRepository->get($sku)->setProductLinks($productLinks)->save();
$this->log->logComment(sprintf('Saved product links for %s', $sku), 1);

} catch (ComponentException $e) {
$this->log->logError($e->getMessage(), 1);
} catch (\Exception $e) {
$this->log->logError($e->getMessage(), 1);
}
}

/**
* Check if the product exists function
*
* @param string $sku
* @return bool
* @todo find an efficient way to check if the product exists.
*/
private function doesProductExist($sku)
{
if ($this->productRepository->get($sku)->getId()) {
return true;
}
return false;
}
}
2 changes: 2 additions & 0 deletions Model/Component/YamlComponentAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* Abstract Class for Components driven by YAML configuration
* @package CtiDigital\Configurator\Model\Component
*
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class YamlComponentAbstract extends ComponentAbstract
{
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ These test include PHP Code Sniffer, PHP Mess Detector, PHP Copy and Paste Detec
php vendor/bin/phpcs --standard=PSR2 vendor/ctidigital/magento2-configurator/Model/ vendor/ctidigital/magento2-configurator/Console/ vendor/ctidigital/magento2-configurator/Test/ vendor/ctidigital/magento2-configurator/Helper/
php vendor/bin/phpmd vendor/ctidigital/magento2-configurator/Model/,vendor/ctidigital/magento2-configurator/Console/,vendor/ctidigital/magento2-configurator/Test/,vendor/ctidigital/magento2-configurator/Helper/ text cleancode,codesize,controversial,design,naming,unusedcode
php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctidigital/magento2-configurator/Console vendor/ctidigital/magento2-configurator/Test/ vendor/ctidigital/magento2-configurator/Helper/
php vendor/bin/phpunit --coverage-clover build/logs/clover.xml vendor/ctidigital/magento2-configurator/Test/Unit/
php vendor/bin/phpunit vendor/ctidigital/magento2-configurator/Test/Unit/
```

## Integration tests
Expand Down Expand Up @@ -92,6 +92,9 @@ It tests the following:
| Tax Rates | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Rewrites | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| Review Ratings | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| Related Products | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Up Sell Products | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Cross Sell Products | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Customers | :x: | :x: | :x: |
| Related Products | :x: | :x: | :x: |
| SQL | :x: | :x: | :x: |
Expand Down
4 changes: 4 additions & 0 deletions Samples/Components/ProductLinks/cross-sells.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cross_sell:
configurable_product_2:
- simple-product
- configurable_product_1
4 changes: 4 additions & 0 deletions Samples/Components/ProductLinks/related.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
relation:
simple-product:
- configurable_product_1
- configurable_product_2
4 changes: 4 additions & 0 deletions Samples/Components/ProductLinks/up-sells.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
up_sell:
configurable_product_1:
- simple-product
- configurable_product_2
9 changes: 8 additions & 1 deletion Samples/master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,11 @@ review_rating:
enabled: 1
method: code
sources:
- ../configurator/ReviewRating/reviewrating.yaml
- ../configurator/ReviewRating/reviewrating.yaml
product_links:
enabled: 1
method: code
sources:
- ../configurator/ProductLinks/related.yaml
- ../configurator/ProductLinks/cross-sells.yaml
- ../configurator/ProductLinks/up-sells.yaml
15 changes: 15 additions & 0 deletions Test/Unit/Component/ProductLinksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace CtiDigital\Configurator\Test\Unit\Component;

use CtiDigital\Configurator\Model\Component\ProductLinks;

class ProductLinksTest extends ComponentAbstractTestCase
{

protected function componentSetUp()
{
$this->component = $this->testObjectManager->getObject('CtiDigital\Configurator\Model\Component\ProductLinks');
$this->className = ProductLinks::class;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"satooshi/php-coveralls": "^1.0",
"phpunit/phpunit": "4.1.0"
},
"version": "0.18.0-dev",
"version": "0.19.0-dev",
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,15 @@ <h2>Supported Components</h2>
<span class="icon fa-users"></span>
<strong>Customer</strong> Groups
</li>
<li class="style4">
<span class="icon fa-link"></span>
<strong>Product Links</strong> Related, Up-sells &amp; Cross-sells
</li>
<li class="style3">
<span class="icon fa-file-image-o"></span>
<strong>Media</strong>
</li>
<li class="style4">
<li class="style2">
<span class="icon fa-gbp"></span>
<strong>Tax</strong> Rules &amp; Rates
</li>
Expand All @@ -199,10 +203,6 @@ <h2>Work In Progress Components</h2>
<p>Below are the list of components, not supported and been worked upon.</p>
</header>
<ul class="statistics">
<li class="style1">
<span class="icon fa-lightbulb-o"></span>
<strong>Related</strong> Products
</li>
<li class="style3">
<span class="icon fa-lightbulb-o"></span>
<strong>Customers</strong>
Expand Down
1 change: 1 addition & 0 deletions etc/configurator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<component name="media" class="CtiDigital\Configurator\Model\Component\Media" />
<component name="rewrites" class="CtiDigital\Configurator\Model\Component\Rewrites" />
<component name="review_rating" class="CtiDigital\Configurator\Model\Component\ReviewRating" />
<component name="product_links" class="CtiDigital\Configurator\Model\Component\ProductLinks" />
</config>

0 comments on commit 25a44a4

Please sign in to comment.