Last active
September 1, 2021 14:51
-
-
Save PululuK/f37a508975f944d6fba70534eb82490d to your computer and use it in GitHub Desktop.
Check if prestashop product as cart rule / promo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function checkIfProductHasPromo(int $idProduct, ?bool $checkValidity = true): bool{ | |
$productObject = new Product($idProduct); | |
if(!Validate::isLoadedObject($productObject)){ | |
return false; | |
} | |
$sql = new DbQuery(); | |
$sql->select('*'); | |
$sql->from('cart_rule_product_rule', 'crpr'); | |
$sql->leftJoin( | |
'cart_rule_product_rule_value', | |
'crprv', | |
'crpr.id_product_rule = crprv.id_product_rule' | |
); | |
$sql->leftJoin( | |
'cart_rule_product_rule_group', | |
'crprg', | |
'crprg.id_product_rule_group = crpr.id_product_rule_group' | |
); | |
$cartsRules = Db::getInstance()->executeS($sql); | |
if(empty($cartsRules)) { | |
return false; | |
} | |
foreach($cartsRules as $cartsRule){ | |
$hasPromo = false; | |
switch ($cartsRule['type']) { | |
case "products": | |
if((int) $cartsRule['id_item'] == (int) $productObject->id){ | |
$hasPromo = true; | |
} | |
break; | |
case "categories": | |
$productCategories = $productObject->getCategories(); | |
foreach ($productCategories as $category){ | |
if((int) $cartsRule['id_item'] == (int) $category['id_category']){ | |
$hasPromo = true; | |
} | |
} | |
break; | |
case "manufacturers": | |
if((int) $cartsRule['id_item'] == (int) $productObject->id_manufacturer){ | |
$hasPromo = true; | |
} | |
break; | |
case "suppliers": | |
if((int) $cartsRule['id_item'] == (int) $productObject->id_supplier){ | |
$hasPromo = true; | |
} | |
break; | |
} | |
if($hasPromo && $checkValidity){ | |
$cartRuleObject = new CartRule((int)$cartsRule['id_cart_rule']); | |
//$cartRuleIsValable = $cartRuleObject->checkValidity($this->context, true, false); | |
$cartRuleIsValable = $cartRuleObject->active | |
&& $cartRuleObject->quantity | |
&& (strtotime($cartRuleObject->date_from) < time()) | |
&& (strtotime($cartRuleObject->date_to) > time()) | |
; | |
return $cartRuleIsValable; | |
} | |
if ($hasPromo && !$checkValidity){ | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment