bootstrap = true; $this->className = 'CmsPack'; $this->table = 'cms_pack'; $this->identifier = 'id_pack'; $this->addRowAction('edit'); $this->addRowAction('delete'); $this->fields_list = array( 'id_pack' => array( 'title' => $this->l('ID'), 'align' => 'center', ), 'title' => array( 'title' => $this->l('Nom du pack'), 'align' => 'center', ), 'product' => array( 'title' => $this->l('Produits'), 'align' => 'center', 'callback' => 'getProductDetails', 'filter' => false, 'search' => false ), 'remise' => array( 'title' => $this->l('Remise %'), 'align' => 'center', 'callback' => 'getRemise', 'filter' => false, 'search' => false ), ); $this->_select = ' "product" as product, "remise" as remise'; $this->_group = ' GROUP BY a.`id_pack`'; parent::__construct(); } public function getRemise($data, $all_data) { $obj = new CmsPack($all_data['id_pack']); return $obj->getPercentageReduction(); } public function getProductDetails($data, $obj) { $pack = new CmsPack($obj['id_pack']); $infos = $pack->getInfosWithValues(); $result = ''; foreach ($infos as $key => $info) { $result.= $info['content'] . ' - ' ; } return $result; } public function renderForm() { if (!($obj = $this->loadObject(TRUE))) return; $this->fields_form = array( 'legend' => array( 'title' => $this->className, ), 'submit' => array( 'name' => 'submitCMSPack', 'title' => $this->l('Save'), ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Nom du pack'), 'name' => 'title', 'size' => 114, ), array( 'type' => 'relation_product', 'label' => $this->l('Sélectionner des produits'), 'name' => 'relation_product', 'class' => 'relation_product', 'id' => 'product-pack', 'query' => $this->object->getInfosWithValues('product'), ), array( 'type' => 'text', 'label' => $this->l('Montant de la réduction (%)'), 'name' => 'montant_reduction', 'size' => 114, ), array( 'type' => 'hidden', 'label' => $this->l('Id cart rule'), 'name' => 'id_cart_rule', ), ) ); $this->fields_value = array( 'montant_reduction' => $this->getPercentageReduction() ); return parent::renderForm(); } public function getPercentageReduction() { if(Tools::getIsset('id_pack')) { if (Validate::isLoadedObject($obj = $this->loadObject())) { return $obj->getPercentageReduction(); } } } public function processSave() { parent::processSave(); $relations = Tools::getValue('input-product-pack', array()); if ($this->object->deleteRelations()) { foreach ($relations as $key => $relation) { $this->object->addRelation($relation); } } if (Validate::isInt(Tools::getValue('montant_reduction'))) { // Cart rule doesnt exists if (empty(Tools::getValue('id_cart_rule')) || Tools::getValue('id_cart_rule') == 0 ) { $cartRule = new CartRule(); $languages = Language::getLanguages(); foreach ($languages as $key => $language) { $cartRule->name[$language['id_lang']] = Tools::getValue('title'); } $cartRule->id_customer = 0; $cartRule->date_from = date('Y-m-d H:i:s'); $cartRule->date_to = date('Y-m-d H:i:s', strtotime('+3 year')); $cartRule->quantity = 999999; $cartRule->quantity_per_user = 999999; $cartRule->partial_use = 1; $cartRule->priority = 1; $cartRule->highlight = 1; $cartRule->code = Tools::passwdGen(8); $cartRule->minimum_amount = 0; $cartRule->minimum_amount_tax = 0; $cartRule->reduction_product = -2; $cartRule->minimum_amount_currency = 1; $cartRule->product_restriction = 1; $cartRule->reduction_percent = (int) Tools::getValue('montant_reduction'); $cartRule->active = 1; if ($cartRule->add()) { Db::getInstance()->execute(' UPDATE `ps_cms_pack` SET `id_cart_rule` = ' .(int)$cartRule->id . ' WHERE `id_pack` = '.(int) Tools::getValue('id_pack') ); self::saveRelationCartRule($relations, $cartRule->id); } } else { $cartRule = new CartRule(Tools::getValue('id_cart_rule')); $cartRule->reduction_percent = (int) Tools::getValue('montant_reduction'); $cartRule->update(); self::deleteRelationCartRule($cartRule->id); self::saveRelationCartRule($relations, $cartRule->id); } } } public static function deleteRelationCartRule($id_cart_rule) { Db::getInstance()->execute(' DELETE FROM `ps_cart_rule_product_rule_value` WHERE `id_product_rule` IN ( SELECT c1.`id_product_rule` FROM `ps_cart_rule_product_rule` c1 LEFT JOIN `ps_cart_rule_product_rule_group` c2 on (c1.`id_product_rule_group` = c2.`id_product_rule_group`) WHERE c2.`id_cart_rule` = '.(int)$id_cart_rule.' ) '); Db::getInstance()->execute(' DELETE FROM `ps_cart_rule_product_rule` WHERE `id_product_rule_group` IN (SELECT `id_product_rule_group` FROM `ps_cart_rule_product_rule_group` WHERE `id_cart_rule` = '.(int)$id_cart_rule.') '); Db::getInstance()->execute(' DELETE FROM `ps_cart_rule_product_rule_group` WHERE `id_cart_rule` = '.(int)$id_cart_rule.' '); } public static function saveRelationCartRule($products, $id_cart_rule) { foreach ($products as $key => $id_product) { Db::getInstance()->execute(' INSERT INTO `ps_cart_rule_product_rule_group` (`id_cart_rule`, `quantity`) VALUES ('.(int)$id_cart_rule.',1) '); $id_rule_group = Db::getInstance()->Insert_ID(); Db::getInstance()->execute(' INSERT INTO `ps_cart_rule_product_rule` (`id_product_rule_group`, `type`) VALUES ('.(int) $id_rule_group.',"products") '); $id_product_rule = Db::getInstance()->Insert_ID(); Db::getInstance()->execute(' INSERT INTO `ps_cart_rule_product_rule_value` (`id_product_rule`, `id_item`) VALUES ('.(int) $id_product_rule.', '.(int)$id_product.') '); } } public function processDelete() { if (Validate::isLoadedObject($object = $this->loadObject())) { $object->deleteRelations(); } parent::processDelete(); } }