0
votes

I already passed the ajax value to the front controller from js in prestashop 1.7.6. So I first set the link in hookFooter function of module class file with

// Create a link with the path
$link = new Link;
$parameters = array("action" => "send_changed_price");
$ajax_link = $link->getModuleLink('mymodule','ajax', $parameters);

Second, used this url in custom.js file and made a Ajax call with

    $.getJSON(
        ajax_link, 
        {parameter1 : 1100}, 
        function(data) {
            if(typeof data.status !== "undefined") {

                // Use your new datas here
                console.log(data);
            }

        }
    );

And get the Ajax value in init_content function of front controller mymoduele/controllers/front/ajax.php with

     switch (Tools::getValue('action')) {

            case 'send_changed_price':
            $var1 = Tools::getValue('parameter1');
            // $var1.=$var;//(float)$var1;
            // Edit default response and do some work here
            $response = array('status' => true, "message" => $var1);
            break;

        default:
            break;

    }

The problem is I want to use this ajax value in my module class, but not in front controller class. Because I have to use this value in hookActionCartSave(one of hook) function of my module class.

How can I pass this value to the module main class? Or is there any other way to pass Ajax value to main module class directly?

2

2 Answers

0
votes

From your FrontController you can call any module function you need by using $this->module->HERE_IS_NAME_OF_YOUR_MODULE_FUNCTION()

0
votes

First, I added these data to datable with one front controller which adapt with database:

<?php


class DbAdapter extends ObjectModel {
    
    public static $definition_array = array(
        'table' => 'tablename',
        'primary' => 'id',
        'fields' => array(
            'value1' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'value2' => array('type' => self::TYPE_FLOAT, 'validate' => 'isNegativePrice', 'required' => true)
        )
    );

    public static function insertData($value1, $value2, ...) {
            $result=Db::getInstance()->insert('tablename', array(
                'value1' => $value1,
                'value2' => $value2,
                ...........
            ));
    }

    public static function getData($value1) {
        $query = "SELECT * FROM `"._DB_PREFIX_."tablename` WHERE `value1` = $value1 ORDER BY id DESC";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query);
    }
}

In front controller, after get the Ajax value, save them with that datatable adapter function.

switch (Tools::getValue('action')) {

            case 'send_changed_price':
                $var1 = Tools::getValue('parameter1');
                $var2 = "additional_value";

                DbAdapter::insertData($var1, $var2);
                break;

            default:
                break;

        }

And in the module main class just used getData() function

$stitch_data_row= DbAdapter::getStitch($specific_price->id_cart);

Of course, you have to include this Db adapter class php file at the top in module main class and Ajax class file.

include_once(_PS_MODULE_DIR_ . 'modulename/classes/DbAdapter.php');