Skip to content
This repository was archived by the owner on Dec 1, 2020. It is now read-only.
Open

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 81 additions & 53 deletions crossselling.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
exit;
}

require_once dirname(__FILE__)."/fpgrowth/PrestashopWrapper.php";

class CrossSelling extends Module
{
protected $html;
Expand Down Expand Up @@ -56,15 +58,21 @@ public function install()
!$this->registerHook('shoppingCart') ||
!$this->registerHook('actionOrderStatusPostUpdate') ||
!Configuration::updateValue('CROSSSELLING_DISPLAY_PRICE', 0) ||
!Configuration::updateValue('CROSSSELLING_NBR', 10)
) {
!Configuration::updateValue('CROSSSELLING_NBR', 10) ||
!\fpgrowth\PrestashopWrapper::createTable() ) {
return false;
}
$this->_clearCache('crossselling.tpl');

return true;
}

public function enable()
{
return parent::enable();

}

public function uninstall()
{
$this->_clearCache('crossselling.tpl');
Expand Down Expand Up @@ -95,6 +103,9 @@ public function getContent()
$this->_clearCache('crossselling.tpl');
$this->html .= $this->displayConfirmation($this->l('Settings updated successfully'));
}
}elseif (Tools::isSubmit('submitRefresh')) {
$this->processTransactionsDb();
$this->html .= $this->displayConfirmation($this->l('Rules updated successfully'));
}

return $this->html.$this->renderForm();
Expand All @@ -120,66 +131,57 @@ public function hookHeader()
$this->context->controller->addJqueryPlugin(array('scrollTo', 'serialScroll', 'bxslider'));
}




/**
* @param array $products_id an array of product ids
* @return array
*/
protected function getOrderProducts(array $products_id)
{
$q_orders = 'SELECT o.id_order
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'order_detail od ON (od.id_order = o.id_order)
WHERE o.valid = 1 AND od.product_id IN ('.implode(',', $products_id).')';
$orders = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($q_orders);

$final_products_list = array();
$list_product_ids = join(',', $products_id);

if (Group::isFeatureActive()) {
$sql_groups_join = '
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = product_shop.id_category_default
AND cp.id_product = product_shop.id_product)
LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cp.`id_category` = cg.`id_category`)';
$groups = FrontController::getCurrentCustomerGroups();
$sql_groups_where = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '='.(int)Group::getCurrent()->id);
}

if (count($orders) > 0) {
$list = '';
foreach ($orders as $order) {
$list .= (int)$order['id_order'].',';
}
$list = rtrim($list, ',');

$list_product_ids = join(',', $products_id);

if (Group::isFeatureActive()) {
$sql_groups_join = '
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = product_shop.id_category_default
AND cp.id_product = product_shop.id_product)
LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cp.`id_category` = cg.`id_category`)';
$groups = FrontController::getCurrentCustomerGroups();
$sql_groups_where = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '='.(int)Group::getCurrent()->id);
}

$order_products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT od.product_id, pl.name, pl.description_short, pl.link_rewrite, p.reference, i.id_image, product_shop.show_price,
cl.link_rewrite category, p.ean13, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity
FROM '._DB_PREFIX_.'order_detail od
LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = od.product_id)
'.Shop::addSqlAssociation('product', 'p').
(Combination::isFeatureActive() ? 'LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa
ON (p.`id_product` = pa.`id_product`)
'.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
'.Product::sqlStock('p', 'product_attribute_shop', false, $this->context->shop) : Product::sqlStock('p', 'product', false,
$this->context->shop)).'
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = od.product_id'.Shop::addSqlRestrictionOnLang('pl').')
LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'
.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = od.product_id)
'.(Group::isFeatureActive() ? $sql_groups_join : '').'
WHERE od.id_order IN ('.$list.')
$sqlSelection = '
SELECT DISTINCT cpa.id_product_related as product_id, pl.name, pl.description_short, pl.link_rewrite, p.reference, i.id_image, product_shop.show_price,
cl.link_rewrite category, p.ean13, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity
FROM '._DB_PREFIX_.'crossselling_pair cpa
LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = cpa.id_product_related)
'.Shop::addSqlAssociation('product', 'p').
(Combination::isFeatureActive() ? 'LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa
ON (p.`id_product` = pa.`id_product`)
'.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
'.Product::sqlStock('p', 'product_attribute_shop', false, $this->context->shop) : Product::sqlStock('p', 'product', false,
$this->context->shop)).'
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = cpa.id_product_related'.Shop::addSqlRestrictionOnLang('pl').')
LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'
.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = cpa.id_product_related)
'.(Group::isFeatureActive() ? $sql_groups_join : '').'
WHERE cpa.id_product_main in ('.$list_product_ids.')
AND cpa.id_product_related NOT IN ('.$list_product_ids.')
AND pl.id_lang = '.(int)$this->context->language->id.'
AND cl.id_lang = '.(int)$this->context->language->id.'
AND od.product_id NOT IN ('.$list_product_ids.')
AND i.cover = 1
AND product_shop.active = 1
'.(Group::isFeatureActive() ? $sql_groups_where : '').'
ORDER BY RAND()
LIMIT '.(int)Configuration::get('CROSSSELLING_NBR'));
ORDER BY cpa.support desc
LIMIT '.(int)Configuration::get('CROSSSELLING_NBR');

$tax_calc = Product::getTaxCalculationMethod();
$order_products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sqlSelection);

if($order_products != false){
$tax_calc = Product::getTaxCalculationMethod();
foreach ($order_products as &$order_product) {
$order_product['id_product'] = (int)$order_product['product_id'];
$order_product['image'] = $this->context->link->getImageLink($order_product['link_rewrite'],
Expand All @@ -198,7 +200,6 @@ protected function getOrderProducts(array $products_id)
}
}
}

return $final_products_list;
}

Expand Down Expand Up @@ -250,10 +251,10 @@ public function displayProductListReviews($params)
*/
public function hookProductFooter($params)
{
$cache_id = 'crossselling|productfooter|'.(int)$params['product']->id;
$cache_id = 'crossselling|productfooter|'.(int)$params['product']['id'];
if (!$this->isCached('crossselling.tpl', $this->getCacheId($cache_id))) {
$final_products_list = $this->getOrderProducts(array($params['product']['id']));

if (!$this->isCached('crossselling.tpl', $this->getCacheId($cache_id))) {
$final_products_list = $this->getOrderProducts(array($params['product']->id));

if (count($final_products_list) > 0) {
$this->smarty->assign(
Expand Down Expand Up @@ -313,8 +314,9 @@ public function renderForm()
'title' => $this->l('Save'),
)
),
);

);

$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
Expand All @@ -332,7 +334,23 @@ public function renderForm()
'id_language' => $this->context->language->id
);

return $helper->generateForm(array($fields_form));
$forms = $helper->generateForm(array($fields_form));


$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Association rules')
),
'submit' => array(
'title' => $this->l('Process Transactions'),
)
),

);
$helper->submit_action = 'submitRefresh';
$forms .= $helper->generateForm(array($fields_form));
return $forms;
}

public function getConfigFieldsValues()
Expand All @@ -342,4 +360,14 @@ public function getConfigFieldsValues()
'CROSSSELLING_DISPLAY_PRICE' => Tools::getValue('CROSSSELLING_DISPLAY_PRICE', Configuration::get('CROSSSELLING_DISPLAY_PRICE')),
);
}

/**
* Processes all the carts in search of product association rules
* Saves them into the database
*/
public function processTransactionsDb(){
$wrapper = new fpgrowth\PrestashopWrapper();
$levels = $wrapper->getProductAssociationRules();
$wrapper->saveProductAssociationRules($levels);
}
}
Loading