Jump to content

[PS1.7.2] Form process with loggedin customer


Recommended Posts

Hi all,

I'm facing an issue with a little custom module I'm developping.

The aim is to put the gift message and gift wrapping box on the Cart page.

I can display the form, the checkbox in a custom hook.

Everything is working fine with a visitor, but it doesn't work with a logged in customer.

My code :

 

My Module :

<?php
if (!defined('_PS_VERSION_'))
{
    exit;
}
class CandyMessageOnCart extends Module
{
    public function __construct()
    {
        $this->name = 'candymessageoncart';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Julien FOURNIER';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Carte cadeau au panier');
        $this->description = $this->l('Ajout du message cadeau au panier');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

        /*if (!Configuration::get('candyextendcorews'))
            $this->warning = $this->l('No name provided');*/
    }

    public function install()
    {
        if (Shop::isFeatureActive())
            Shop::setContext(Shop::CONTEXT_ALL);

        if (!parent::install() ||
            !$this->registerHook('displayMessageOnCart') || !$this->registerHook('header')
        )
            return false;

        return true;
    }

    public function uninstall()
    {
        if (!parent::uninstall())
            return false;
        return true;
    }

    public function hookdisplayMessageOnCart()
    {


        Logger::addLog('Candymessage : affichage module', 1);
        if($this->context->customer->isLogged())
            dump($this->context->customer->id);
        $this->processSaveMessageToCart();
        $cart_id = $this->context->cart->id;
        $cart = new Cart($cart_id);
        $message_gift = $cart->gift_message;
        $gift = $cart->gift;
        //$message_gift = $this->context->cart->gift_message;

        $this->context->smarty->assign(array('gift_message'=>$message_gift, 'gift'=>$gift));


        return $this->context->smarty->fetch('module:candymessageoncart/views/templates/hook/cart.tpl');
    }

    public function processSaveMessageToCart()
    {
        Logger::addLog('Candymessage : ProcessMessage', 1);

        if(Tools::isSubmit('save_msg_candymessage'))
        {
            Logger::addLog('Candymessage : Formulaire soumis', 1);
            if($this->context->customer->isLogged())
                dump($this->context->customer->id);
            $cart_id = $this->context->cart->id;
            $message = Tools::getValue('gift_message');
            $gift = Tools::getValue('gift_option');

            $gift_bool = 0;
            if($gift == 'on')
                $gift_bool = 1;



            $cart = new Cart($cart_id);
            $cart->gift = $gift_bool;
            $cart->gift_message = $message;

            $cart->update();
        }

    }

TPL file :

<div class="message col-xs-12 col-lg-12 col-sm-12">
    <h4 style="color: #FF0090;text-transform: uppercase">J'ajoute un joli mot à ma commande :<small> <i style="display: inline" class="fa fa-question-circle" data-toggle="tooltip" data-html="true" title="<strong>Ajoutez un mot d'accompagnement à votre commande.</strong><br /> Souhaitez un joyeux annivesaire, un prompt rétablissement ou félicitez votre famille !" data-placement="left"></i></small></h4>

<form method="get" action="">
    {*<input type="hidden" name="cart_id" value="{$cart_id}">*}
    <textarea name="gift_message" style="width: 100%" class="form-control candymessage_message">{$gift_message}</textarea>
    <input type="checkbox" name="gift_option" {if $gift}checked="checked"{/if}>
    <label for="gift_option" class="candymessage_label">Je souhaite que mon message soit manuscrit et accompagné d'une brochette de bonbons (+2.90€TTC)</label>
    <input type="submit" name="save_msg_candymessage" class="btn save_msg fancy-btn fancy-btn-small" value="Enregistrer">

</form>
</div>

 

With a visitor, the "Tools::isSubmit" is running. Then, it redirects me to the cart page.

With a customer, nothing is saved and it redirects me to the homepage.

I can't see what is happening there !

 

Thanks for your help.

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...