Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from richdynamix/release/0.2.0rc
Browse files Browse the repository at this point in the history
release/0.2.0
  • Loading branch information
Steven Richardson committed Dec 22, 2015
2 parents f7dfad7 + eaf42d0 commit 006b1a2
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 81 deletions.
31 changes: 3 additions & 28 deletions Block/Checkout/Cart/Crosssell.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class Crosssell extends \Magento\Checkout\Block\Cart\Crosssell
*/
private $_crosssell;

/**
* @var
*/
private $_itemCollection;

/**
* @var Manager
*/
Expand Down Expand Up @@ -95,19 +90,14 @@ public function getItems()
return parent::getItems();
}

$ninProductIds = $this->_getCartProductIds();
$personalisedIds = $this->_crosssell->getProductCollection($ninProductIds);

$personalisedIds = $this->_crosssell->getProductCollection();
if (!$personalisedIds) {
return parent::getItems();
}

$collection = $this->_getPersonalisedProductCollection($personalisedIds);

$this->_itemCollection = $collection;

$collection = $this->_crosssell->getPersonalisedProductCollection($personalisedIds);
if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
$this->_addProductAttributesAndPrices($collection);
}

$items = [];
Expand All @@ -117,19 +107,4 @@ public function getItems()

return $items;
}

/**
* We only want to show visible and enabled products.
*
* @param $personalisedIds
* @return $this
*/
private function _getPersonalisedProductCollection($personalisedIds)
{
$collection = $this->_productFactory->create()->getCollection()
->addAttributeToFilter('entity_id', ['in', $personalisedIds])
->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH)
->addAttributeToFilter('status', array('eq' => 1));
return $collection;
}
}
135 changes: 135 additions & 0 deletions Block/Checkout/Cart/TargetCrosssell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Richdynamix\PersonalisedProducts\Block\Checkout\Cart;

use \Magento\Catalog\Block\Product\Context;
use \Magento\TargetRule\Model\ResourceModel\Index;
use \Magento\TargetRule\Helper\Data;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use \Magento\Catalog\Model\Product\Visibility;
use \Magento\CatalogInventory\Helper\Stock;
use \Magento\Checkout\Model\Session;
use \Magento\Catalog\Model\Product\LinkFactory;
use \Magento\TargetRule\Model\IndexFactory;
use \Magento\Catalog\Model\ProductTypes\ConfigInterface;
use \Magento\Catalog\Api\ProductRepositoryInterface;
use \Richdynamix\PersonalisedProducts\Helper\Config;
use \Magento\TargetRule\Block\Checkout\Cart\Crosssell;
use \Richdynamix\PersonalisedProducts\Model\Frontend\Checkout\Cart\Crosssell as CrosssellModel;
use \Magento\Catalog\Model\ProductFactory as ProductFactory;
use \Magento\Framework\Module\Manager as Manager;

/**
* Class TargetCrosssell
*
* @category Richdynamix
* @package PersonalisedProducts
* @author Steven Richardson ([email protected]) @mage_gizmo
*/
class TargetCrosssell extends Crosssell
{
/**
* @var Config
*/
private $_config;
/**
* @var CrosssellModel
*/
private $_crosssell;
/**
* @var ProductFactory
*/
private $_productFactory;
/**
* @var Manager
*/
private $_moduleManager;

/**
* TargetCrosssell constructor.
* @param Context $context
* @param Index $index
* @param Data $targetRuleData
* @param CollectionFactory $productCollectionFactory
* @param Visibility $visibility
* @param Stock $stockHelper
* @param Session $session
* @param LinkFactory $productLinkFactory
* @param IndexFactory $indexFactory
* @param ConfigInterface $productTypeConfig
* @param ProductRepositoryInterface $productRepository
* @param Config $config
* @param CrosssellModel $crosssell
* @param ProductFactory $productFactory
* @param Manager $moduleManager
* @param array $data
*/
public function __construct(
Context $context,
Index $index,
Data $targetRuleData,
CollectionFactory $productCollectionFactory,
Visibility $visibility,
Stock $stockHelper,
Session $session,
LinkFactory $productLinkFactory,
IndexFactory $indexFactory,
ConfigInterface $productTypeConfig,
ProductRepositoryInterface $productRepository,
Config $config,
CrosssellModel $crosssell,
ProductFactory $productFactory,
Manager $moduleManager,
array $data = []
) {
$this->_config = $config;
$this->_crosssell = $crosssell;
$this->_productFactory = $productFactory;
$this->_moduleManager = $moduleManager;
parent::__construct(
$context,
$index,
$targetRuleData,
$productCollectionFactory,
$visibility,
$stockHelper,
$session,
$productLinkFactory,
$indexFactory,
$productTypeConfig,
$productRepository,
$data
);
}

/**
* Get the crossell items for the basket page
* Rewrites the target rules for Enterprise edition
*
* @return array
*/
public function getItemCollection()
{
if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::COMPLEMENTARY_REPLACE_RULES)) {
return parent::getItemCollection();
}

$personalisedIds = $this->_crosssell->getProductCollection();
if (!$personalisedIds) {
return parent::getItemCollection();
}

$collection = $this->_crosssell->getPersonalisedProductCollection($personalisedIds);

if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($collection);
}

$this->_items = [];
foreach ($collection as $item) {
$this->_items[] = $item;
}

return $this->_items;
}
}
117 changes: 117 additions & 0 deletions Block/Product/ProductList/TargetUpsell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Richdynamix\PersonalisedProducts\Block\Product\ProductList;

use \Magento\TargetRule\Block\Catalog\Product\ProductList\Upsell;
use \Magento\Catalog\Block\Product\Context;
use \Magento\TargetRule\Model\ResourceModel\Index;
use \Magento\TargetRule\Helper\Data;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use \Magento\Catalog\Model\Product\Visibility;
use \Magento\TargetRule\Model\IndexFactory;
use \Magento\Framework\Module\Manager as Manager;
use \Magento\Checkout\Model\Cart;
use \Richdynamix\PersonalisedProducts\Helper\Config;
use \Richdynamix\PersonalisedProducts\Model\Frontend\Catalog\Product\ProductList\Upsell as UpsellModel;

/**
* Rewrite target rule product upsell block in enterprise edition
* to switch out product collection for one returned from PredictionIO
*
* @category Richdynamix
* @package PersonalisedProducts
* @author Steven Richardson ([email protected]) @mage_gizmo
*/
class TargetUpsell extends Upsell
{
/**
* @var Cart
*/
protected $_cart;
/**
* @var Config
*/
protected $_config;
/**
* @var UpsellModel
*/
protected $_upsell;
/**
* @var Manager
*/
protected $_moduleManager;

/**
* @param Context $context
* @param Index $index
* @param Data $targetRuleData
* @param CollectionFactory $productCollectionFactory
* @param Visibility $visibility
* @param IndexFactory $indexFactory
* @param Cart $cart
* @param Config $config
* @param UpsellModel $upsell
* @param Manager $moduleManager
*/
public function __construct(
Context $context,
Index $index,
Data $targetRuleData,
CollectionFactory $productCollectionFactory,
Visibility $visibility,
IndexFactory $indexFactory,
Cart $cart,
Config $config,
UpsellModel $upsell,
Manager $moduleManager
) {
$this->_cart = $cart;
$this->_config = $config;
$this->_upsell = $upsell;
$this->_moduleManager = $moduleManager;
parent::__construct(
$context,
$index,
$targetRuleData,
$productCollectionFactory,
$visibility,
$indexFactory,
$cart
);
}

/**
* Rewrite parent getAllItems method to use PredictionIO results when available
* Rewrites the target rules for Enterprise edition
*
* @return array
*/
public function getAllItems()
{
if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::SIMILARITY_REPLACE_RULES)) {
return parent::getAllItems();
}

$product = $this->_coreRegistry->registry('product');
$categoryIds = $this->_upsell->getCategoryIds($product);
$personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds);

if (!$personalisedIds) {
return parent::getAllItems();
}

$collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds);

if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($collection);
}

$items = [];
foreach ($collection as $product) {
$product->setDoNotUseCategoryId(true);
$items[] = $product;
}

return $items;
}
}
35 changes: 2 additions & 33 deletions Block/Product/ProductList/Upsell.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ protected function _prepareData()
}

$product = $this->_coreRegistry->registry('product');
$categoryIds = $this->_getCategoryIds($product);
$categoryIds = $this->_upsell->getCategoryIds($product);
$personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds);

if (!$personalisedIds) {
return parent::_prepareData();
}

$collection = $this->_getPersonalisedProductCollection($personalisedIds);
$collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds);

$this->_itemCollection = $collection;

Expand All @@ -116,35 +116,4 @@ protected function _prepareData()

return $this;
}

/**
* Get array of category ID's the product belongs to
*
* @param $product
* @return array
*/
private function _getCategoryIds($product)
{
if (!$this->_config->getItem(Config::SIMILARITY_USE_CATEGORY_FILTER)) {
return [];
}

return $product->getCategoryIds();

}

/**
* We only want to show visible and enabled products.
*
* @param $personalisedIds
* @return $this
*/
private function _getPersonalisedProductCollection($personalisedIds)
{
$collection = $this->_productFactory->create()->getCollection()
->addAttributeToFilter('entity_id', ['in', $personalisedIds])
->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH)
->addAttributeToFilter('status', array('eq' => 1));
return $collection;
}
}
10 changes: 10 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
*/
const SIMILARITY_USE_CATEGORY_FILTER = 'personalised_products/similarity_engine/use_categories';

/**
* Switch to replace Rule Based Upsells (enterprise edition only)
*/
const SIMILARITY_REPLACE_RULES = 'personalised_products/similarity_engine/replace_rules';

/**
* Complementary engine URL (Crosssell engine)
*/
Expand All @@ -80,6 +85,11 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
*/
const COMPLEMENTARY_PRODUCT_COUNT = 'personalised_products/complementary_engine/product_count';

/**
* Switch to replace Rule Based Cross sells (enterprise edition only)
*/
const COMPLEMENTARY_REPLACE_RULES = 'personalised_products/complementary_engine/replace_rules';

/**
* Config constructor.
* @param ScopeConfigInterface $scopeConfig
Expand Down
Loading

0 comments on commit 006b1a2

Please sign in to comment.