Jump to content

Beginner task: Error.log message when something was added to the shopping cart


Recommended Posts

Hey guys,

I am a beginner in programming and I need to create a module in Prestashop for my university. The task is that when you add something to the shopping cart, a message appears in the error log. I've been sitting there for two days now and can't see what I've done wrong. Maybe someone of you can help me?

The module is already installed and I can also add things to the cart, however the error log message won't appear. 

image.thumb.png.4e56486842c043d85f3b502053ad465e.png

 

Link to comment
Share on other sites

Hi.

Sample:

$message = 'Helo from cartmodule: User made changes!';
$severity = 1;
$error_code = 1;
/*
$level_value = [
        0 => 'DEBUG',
        1 => 'INFO',
        2 => 'WARNING',
        3 => 'ERROR',
    ];
*/
$object_type = 'Cart';
$object_id = $params['id_cart'];
$allow_duplicate = true;
PrestaShopLogger::addLog($message, $severity, $error_code, $object_type , $object_id , $allow_duplicate);

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Hi @Nikola K.

Welcome to the forum. Interesting to see you have PrestaShop at university. But I think it is more to learn of analytical thinking then PrestaShop code itself.

So I would go step by step through code and verify each line.

  • irst is registerHook line, you can easily check that in back office and Design > Position
  • Next if nothing is written to log check if function name is correct, I would write capital A, public function hookActionCartUpdateQuantityBefore
  • Finally check alternative to error_log function, it could be it is disabled on server, use print_r,  var_dump, die(json_encode()) or best like @4you.software recommended included function PrestaShopLooger::addLog that will give you error messages displayed at back office at Advanced Parameters > Logs  

Also do use Google to find similar issues for reference:

https://www.prestashop.com/forums/topic/1046262-how-to-stop-a-product-from-being-added-to-cart-programmatically-why-out-of-stock-products-can-be-added-to-my-cart/

https://github.com/PrestaShop/PrestaShop/issues/13518

 

Good luck

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

There are several possibilities, for example in combination with JavaScript and event listening prestashop.on('updateCart ....

A header hook is added to the module and javascript is connected + messages are defined.

 

E.g.:

Module

/* install setion */
 && $this->registerHook('displayHeader');

/* function */
public function hookDisplayHeader()
{ 
	$this->context->controller->addJS(_PS_MODULE_DIR_.$this->name.'/views/js/front/front.js');	
	$jsDef = [
		'custom_success_message' => $this->l('Succes'),
	];
	Media::addJsDef($jsDef);
}

 

JS

prestashop.on(
    'updateCart',
    function (event) {
        var myDiv = document.createElement('div');
        myDiv.setAttribute('id', 'myDiv');
		myDiv.setAttribute('style', 'display:block; padding:5px; color:black; background: green; font-size:1.2em; position:sticky; top:10px; right:10px; text-align:center;'); 
		myDiv.textContent = custom_success_message;
		document.getElementById('header').appendChild(myDiv);
		setTimeout(function() {
    			$('#mydiv').remove();
		}, 3000);
    }
}

 

Edited by 4you.software (see edit history)
  • Thanks 1
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...