add more checking when saving product samples in cart and log all changes in the cart
This commit is contained in:
parent
1c3f6a80ea
commit
f392af9b30
@ -26,6 +26,10 @@
|
||||
|
||||
class Cart extends CartCore
|
||||
{
|
||||
const ECHANTILLONS_TOTAL_WT_THRESHOLD = 100;
|
||||
const ECHANTILLONS_NB_MAX_ABOVE_THRESHOLD = 5;
|
||||
const ECHANTILLONS_NB_MAX_DEFAULT = 3;
|
||||
|
||||
public static function getNbProducts($id)
|
||||
{
|
||||
// Must be strictly compared to NULL, or else an empty cart will bypass the cache and add dozens of queries
|
||||
@ -518,4 +522,69 @@ class Cart extends CartCore
|
||||
|
||||
return Db::getInstance()->executeS($sql);
|
||||
}
|
||||
|
||||
public function getMaxEchantillonsAllowed()
|
||||
{
|
||||
$summary = $this->getSummaryDetails();
|
||||
if(is_array($summary) && $summary['total_products_wt'] - $summary['total_discounts']>self::ECHANTILLONS_TOTAL_WT_THRESHOLD){
|
||||
return self::ECHANTILLONS_NB_MAX_ABOVE_THRESHOLD;
|
||||
}
|
||||
return self::ECHANTILLONS_NB_MAX_DEFAULT;
|
||||
}
|
||||
|
||||
public function isNbEchantillonsInCartAllowed()
|
||||
{
|
||||
$nb_echantillons = Db::getInstance()->getValue(
|
||||
'SELECT COUNT(*) FROM `'._DB_PREFIX_.'cart_product` WHERE `echantillon` = 1 and `id_cart` = '.(int)$this->id
|
||||
);
|
||||
|
||||
return ($nb_echantillons <= $this->getMaxEchantillonsAllowed());
|
||||
}
|
||||
|
||||
public function saveEchantillons($array_ids_product)
|
||||
{
|
||||
// ensure no "sample" has been saved before
|
||||
$this->deleteEchantillons(false);
|
||||
|
||||
if (!is_array($array_ids_product)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$nb_echantillons = count($array_ids_product);
|
||||
if ($nb_echantillons>0) {
|
||||
|
||||
if ($nb_echantillons <= $this->getMaxEchantillonsAllowed() ) {
|
||||
foreach($array_ids_product as $id_product) {
|
||||
Db::getInstance()->execute('
|
||||
INSERT INTO '._DB_PREFIX_.'cart_product
|
||||
(id_shop,id_cart,id_address_delivery,id_product,date_add,echantillon,quantity)
|
||||
VALUES
|
||||
('.(int)$this->id_shop.','.(int)$this->id.','.(int)$this->id_address_delivery.','.(int)$id_product.',\''.date('Y\-m\-d H\:i\:s').'\',1,1)
|
||||
');
|
||||
}
|
||||
}
|
||||
|
||||
// this log is called only by the current method. Only for checking in case of anomaly.
|
||||
$this->logSaveEchantillons($nb_echantillons);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function logSaveEchantillons($nb_echantillons)
|
||||
{
|
||||
$nbProductsNotEchantillon = (int)Db::getInstance()->getValue('
|
||||
SELECT COUNT(*)
|
||||
FROM `'._DB_PREFIX_.'cart_product`
|
||||
WHERE echantillon = 0 and `id_cart` = '.(int)$this->id
|
||||
);
|
||||
|
||||
$f = fopen(_PS_ROOT_DIR_.'/log/cart_override_saveechantillons.log', 'a');
|
||||
if ($f) {
|
||||
fwrite($f,
|
||||
'id_cart : '.$this->id.' | date_add : '.$this->date_add.' | date_upd : '.$this->date_upd.' | Nb echantillons : '.$nb_echantillons.' | Nb not echantillons : '.$nbProductsNotEchantillon.' | id_customer : '.$this->id_customer.' | mobile_theme : '.($this->mobile_theme?'true':'false').' | id_carrier : '.$this->id_carrier.PHP_EOL);
|
||||
fclose($f);
|
||||
}
|
||||
}
|
||||
}
|
@ -33,23 +33,15 @@ class OrderController extends OrderControllerCore
|
||||
if(Tools::isSubmit('submitPanier')){
|
||||
//si un un echantillon est posté :
|
||||
|
||||
$id_cart=$this->context->cart->id;
|
||||
//vider les échantillons
|
||||
$result = Db::getInstance()->execute('delete FROM '._DB_PREFIX_.'cart_product WHERE echantillon=1 and id_cart = '.(int)$id_cart);
|
||||
$this->context->cart->deleteEchantillons(false);
|
||||
|
||||
|
||||
$id_shop=$this->context->cart->id_shop;
|
||||
$id_address_delivery=$this->context->cart->id_address_delivery;
|
||||
$date_add=date('Y\-m\-d H\:i\:s');
|
||||
|
||||
$echantillons=$_POST['echantillon'];
|
||||
foreach($echantillons as $id_product){
|
||||
// Rajouter les échantillons dans le panier
|
||||
|
||||
$result = Db::getInstance()->execute('insert into '._DB_PREFIX_.'cart_product
|
||||
(id_shop,id_cart,id_address_delivery,id_product,date_add,echantillon,quantity)
|
||||
values
|
||||
('.(int)$id_shop.','.(int)$id_cart.','.(int)$id_address_delivery.','.(int)$id_product.',\''.$date_add.'\',1,1)');
|
||||
$echantillons_ids_product=$_POST['echantillon'];
|
||||
|
||||
// Rajouter les échantillons dans le panier
|
||||
if (!$this->context->cart->saveEchantillons($echantillons_ids_product)) {
|
||||
Tools::redirect('index.php?controller=order&step=0&error_echantillons=1');
|
||||
}
|
||||
}
|
||||
if ((int)(Tools::getValue('gift')))
|
||||
@ -107,30 +99,16 @@ class OrderController extends OrderControllerCore
|
||||
|
||||
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateEchantillons')
|
||||
{
|
||||
|
||||
$id_cart=$this->context->cart->id;
|
||||
//vider les échantillons
|
||||
$result = Db::getInstance()->execute('delete FROM '._DB_PREFIX_.'cart_product WHERE echantillon=1 and id_cart = '.(int)$id_cart);
|
||||
$this->context->cart->deleteEchantillons(false);
|
||||
|
||||
$id_shop=$this->context->cart->id_shop;
|
||||
$id_address_delivery=$this->context->cart->id_address_delivery;
|
||||
$date_add=date('Y\-m\-d H\:i\:s');
|
||||
$echantillons_ids_product=array_unique(explode('-',pSQL(Tools::getValue('lesEchantillons'))));
|
||||
|
||||
$echantillons=array_unique(explode('-',pSQL(Tools::getValue('lesEchantillons'))));
|
||||
|
||||
for($i=0;$i<count($echantillons);$i++){
|
||||
// Rajouter les échantillons dans le panier
|
||||
$id_product=$echantillons[$i];
|
||||
// echo $id_product."<br/><br/>";
|
||||
|
||||
$sql='insert into '._DB_PREFIX_.'cart_product
|
||||
(id_shop,id_cart,id_address_delivery,id_product,date_add,echantillon,quantity)
|
||||
values
|
||||
('.(int)$id_shop.','.(int)$id_cart.','.(int)$id_address_delivery.','.(int)$id_product.',\''.$date_add.'\',1,1)';
|
||||
// echo $sql."<br/>";
|
||||
$result = Db::getInstance()->execute($sql);
|
||||
|
||||
// Rajouter les échantillons dans le panier
|
||||
if (!$this->context->cart->saveEchantillons($echantillons_ids_product)) {
|
||||
Tools::redirect('index.php?controller=order&step=0&error_echantillons=1');
|
||||
}
|
||||
|
||||
die(true);
|
||||
}
|
||||
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'deleteEchantillons')
|
||||
@ -260,6 +238,10 @@ class OrderController extends OrderControllerCore
|
||||
break;
|
||||
|
||||
case 1:
|
||||
if (!$this->context->cart->isNbEchantillonsInCartAllowed()) {
|
||||
Tools::redirect('index.php?controller=order&step=0&error_echantillons=1');
|
||||
}
|
||||
|
||||
// echo "test";
|
||||
// die();
|
||||
// $this->processCarrier();
|
||||
@ -282,6 +264,10 @@ class OrderController extends OrderControllerCore
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (!$this->context->cart->isNbEchantillonsInCartAllowed()) {
|
||||
Tools::redirect('index.php?controller=order&step=0&error_echantillons=1');
|
||||
}
|
||||
|
||||
$this->processCarrier();
|
||||
// if (Tools::isSubmit('processAddress'))
|
||||
// $this->processAddress();
|
||||
@ -379,6 +365,10 @@ class OrderController extends OrderControllerCore
|
||||
break;
|
||||
|
||||
case 10:
|
||||
if (!$this->context->cart->isNbEchantillonsInCartAllowed()) {
|
||||
Tools::redirect('index.php?controller=order&step=0&error_echantillons=1');
|
||||
}
|
||||
|
||||
$this->_assignEchantillons();
|
||||
$this->_assignSummaryInformations();
|
||||
$this->_assignCarrier();
|
||||
@ -798,5 +788,6 @@ class OrderController extends OrderControllerCore
|
||||
$this->context->cart->checkedTOS = '1';
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,9 @@
|
||||
|
||||
|
||||
<p style="display:none" id="emptyCartWarning" class="warning">{l s='Your shopping cart is empty.'}</p>
|
||||
{if isset($smarty.get.error_echantillons)}
|
||||
<p id="emptyCartWarning" class="warning">{l s='Too much samples selected.'}</p>
|
||||
{/if}
|
||||
<div id="order-detail-content" class="table_block">
|
||||
<table id="cart_summary" class="std">
|
||||
<tfoot>
|
||||
|
Loading…
Reference in New Issue
Block a user