Jump to content

Use a variable from Module.php in my .js file


Recommended Posts

Hi, 

I'm trying to get my module working. In my shop i'm using displayShoppingCart hook to display message about point value of cart, and the hook is obviously in the Module.php file

    public function hookDisplayShoppingCart($params)
    {
        $points = $this->calculateCartValueInPoints($params);

        if ($points < 0) {
            $points = 0;
        }
        Configuration::updateValue('CART_POINTS', $points);
        $this->smarty->assign('points', $points);

        return $this->display(getModulePath(__FILE__), 'views/templates/front/cart.tpl');
    }

Then I have another hook actionCartSave where I calculate points after cart change, and I want to display them again but doesn't know how.

    public function hookActionCartSave($params)
    {
        $points = $this->calculateCartValueInPoints($params);
        Configuration::updateValue('CART_POINTS', $points);
        $this->smarty->assign('points', $points);
    }

I'm trying to somehow pass $points variable to my .js value which is registerd in displayHeader

public function hookDisplayHeader($params)
{
	$this->context->controller->registerJavascript('mymodule', 'modules/' . $this->name . '/views/js/cart_points_update.js', ['position' => 'bottom', 	'priority' => 200]);
}

I found that Presta published some js methods to check when the cart is updated, but still I don't know how to use my PHP value in .js code

prestashop.on(
    'updateCart',
    function (event) {
        const points = document.getElementById("points_text");
        points.innerHTML = {$points};
    }
);

$points doesn't work as in .tpl files.

Can someone help me?

Link to comment
Share on other sites

I managed to do a dirty workaround, because i've searched whole Internet and didn't find any solution to this.

I simply write data to txt file from Module.php and then read it in .js file with httprequest. Data i'm using here are only for display purpose so it is safe here.

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