Jump to content

Assigner automatiquement une catégorie à un produit en solde


Recommended Posts

Bonjour,

 

Je fais appel à vous car j'ai un problème j'ai créés des champs dans la table produits pour pouvoir insérer des promotions sans avoir à utiliser les specific price.

 

Je souhaiterais assigner automatiquement une catégorie à un produit donc le prix promo est renseigné et que la date actuelle est comprise ente date_debut_promo et date_fin_promo.

 

 

J'ai insérer ce code dans product.tpl :

{if (isset($product->prix_promo) && $product->date_debut_promo <= $now && $now <= $product->date_fin_promo) }
 
{$product->addToCategories([113])}

 

{else}
{$product->deleteCategories([113])}
 
mais je suis obliger de retourner sur ma page produit pour que la catégorie soit insérer ou supprimer.
Or je voudrais qu cette modification se fasse automatiquement des que mes champs sont modifié en base de donnée.
 
Avez-vous une solution à mon problème ?
 
Je reste à votre disposition,
 
Cordialement,
Link to comment
Share on other sites

Merci pour cette réponse,

 

Si j'ai bien compris il faut insérer une fonction dans ProductController.php pour pouvoir assigner une catégorie automatiquement à un produit  si (isset($product->prix_promo) && $product->date_debut_promo <= $now && $now <= $product->date_fin_promo) ?

 

J'ai réinventé la roue avec les promos car j'ai d'autres champs à faire apparaître en supplément des champs de prix spécifiques.

 

 

Cordialement,

 

Link to comment
Share on other sites

Merci pour ta réponse.

 

Je viens de tester et insérer le code dans un override ProductController.php mais malheureusement rien ne se passe lorsque je modifie les champs prix_promo, date_debut_promo et date_fin_promo. La catégorie désirée n'est ni ajoutée, ni supprimée.

   public function init()
    {
        parent::init();


          if ($id_product = (int)Tools::getValue('id_product')) {
           $now=$smarty.now|date_format:'%Y-%m-%d %H:%M:%S';

			$this->product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id);

			if (isset($product->prix_promo) && $product->date_debut_promo <= $now && $now <= $product->date_fin_promo) {

				$product->addToCategories([113]);

			} else {

				$product->deleteCategory(113);

			}

			$this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);


	}

        if (!Validate::isLoadedObject($this->product)) {
            header('HTTP/1.1 404 Not Found');
            header('Status: 404 Not Found');
            $this->errors[] = Tools::displayError('Product not found');
        } else {
            $this->canonicalRedirection();
            /*
             * If the product is associated to the shop
             * and is active or not active but preview mode (need token + file_exists)
             * allow showing the product
             * In all the others cases => 404 "Product is no longer available"
             */
            if (!$this->product->isAssociatedToShop() || !$this->product->active) {
                if (Tools::getValue('adtoken') == Tools::getAdminToken('AdminProducts'.(int)Tab::getIdFromClassName('AdminProducts').(int)Tools::getValue('id_employee')) && $this->product->isAssociatedToShop()) {
                    // If the product is not active, it's the admin preview mode
                    $this->context->smarty->assign('adminActionDisplay', true);
                } else {
                    $this->context->smarty->assign('adminActionDisplay', false);
                    if (!$this->product->id_product_redirected || $this->product->id_product_redirected == $this->product->id) {
                        $this->product->redirect_type = '404';
                    }

                    switch ($this->product->redirect_type) {
                        case '301':
                            header('HTTP/1.1 301 Moved Permanently');
                            header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));
                            exit;
                        break;
                        case '302':
                            header('HTTP/1.1 302 Moved Temporarily');
                            header('Cache-Control: no-cache');
                            header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));
                            exit;
                        break;
                        case '404':
                        default:
                            header('HTTP/1.1 404 Not Found');
                            header('Status: 404 Not Found');
                            $this->errors[] = Tools::displayError('This product is no longer available.');
                        break;
                    }
                }
            } elseif (!$this->product->checkAccess(isset($this->context->customer->id) && $this->context->customer->id ? (int)$this->context->customer->id : 0)) {
                header('HTTP/1.1 403 Forbidden');
                header('Status: 403 Forbidden');
                $this->errors[] = Tools::displayError('You do not have access to this product.');
            } else {
                // Load category
                $id_category = false;
                if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == Tools::secureReferrer($_SERVER['HTTP_REFERER']) // Assure us the previous page was one of the shop
                    && preg_match('~^.*(?<!\/content)\/([0-9]+)\-(.*[^\.])|(.*)id_(category|product)=([0-9]+)(.*)$~', $_SERVER['HTTP_REFERER'], $regs)) {
                    // If the previous page was a category and is a parent category of the product use this category as parent category
                    $id_object = false;
                    if (isset($regs[1]) && is_numeric($regs[1])) {
                        $id_object = (int)$regs[1];
                    } elseif (isset($regs[5]) && is_numeric($regs[5])) {
                        $id_object = (int)$regs[5];
                    }
                    if ($id_object) {
                        $referers = array($_SERVER['HTTP_REFERER'],urldecode($_SERVER['HTTP_REFERER']));
                        if (in_array($this->context->link->getCategoryLink($id_object), $referers)) {
                            $id_category = (int)$id_object;
                        } elseif (isset($this->context->cookie->last_visited_category) && (int)$this->context->cookie->last_visited_category && in_array($this->context->link->getProductLink($id_object), $referers)) {
                            $id_category = (int)$this->context->cookie->last_visited_category;
                        }
                    }
                }
                if (!$id_category || !Category::inShopStatic($id_category, $this->context->shop) || !Product::idIsOnCategoryId((int)$this->product->id, array('0' => array('id_category' => $id_category)))) {
                    $id_category = (int)$this->product->id_category_default;
                }
                $this->category = new Category((int)$id_category, (int)$this->context->cookie->id_lang);
                if (isset($this->context->cookie) && isset($this->category->id_category) && !(Module::isInstalled('blockcategories') && Module::isEnabled('blockcategories'))) {
                    $this->context->cookie->last_visited_category = (int)$this->category->id_category;
                }
            }
        }
    }

Aurais tu une autre méthode a me proposer?

 

Merci de ton aide

Link to comment
Share on other sites

J'ai supprimé le class_index.php et j'ai une erreur 500. Comme tu l'as dit je pense que ce n'est pas la bonne méthode. 

 

Je souhaiterais assigner automatiquement une catégorie à l'enregistrement du produit quand je remplis les champs les champs date_debut_promo et date_fin_promo. Quelle est le fichier a modifier ?

 

Merci beaucoup,

 

Cordialement,
Link to comment
Share on other sites

Effectivement, erreur de débutant :)

 

J'ai toujours une erreur 500 : 

 

Notice: Undefined variable: product in /override/controllers/front/ProductController.php on line 96

Fatal error: Call to a member function deleteCategory() on null in /override/controllers/front/ProductController.php on line 96

Link to comment
Share on other sites

La suppression de catégorie se fait mais pas l'ajout.


          if ($id_product = (int)Tools::getValue('id_product')) {
           $now = date_create('now')->format('Y-m-d H:i:s');

			$this->product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id);

			if ($this->product->prix_promo>0 && $this->product->date_debut_promo <= $now && $now <= $this->product->date_fin_promo) {

				$this->product->addToCategories[(113)];

			} else {

				$this->product->deleteCategory(113);

			}

			$this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);


	}

Aurais tu une idée?

Link to comment
Share on other sites

Tout fonctionne à présent :

 

      if ($id_product = (int)Tools::getValue('id_product')) {
           $now = date('Y-m-d H:i:s');
 
$this->product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id);
 
if (($this->product->prix_promo) > 0 && ($this->product->date_debut_promo) <= date('Y-m-d H:i:s') && date('Y-m-d H:i:s') <= ($this->product->date_fin_promo)) {
 
$this->product->addToCategories(113);
 
} else {
 
$this->product->deleteCategory(113);
 
}
 
$this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);
 
 
}
 
Par contre, je suis obligé de retourné sur le produit pour que les catégories soit ajouter ou supprimer.
 
Je souhaiterais assigner automatiquement une catégorie à l'enregistrement du produit après remplis les champs les champs date_debut_promo et date_fin_promo. 
 
Y aurait-il une autre méthode pour que cela se fasse automatiquement? Quelle est le fichier a modifier ?
 
Merci
 
Cordialement
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...