Jump to content

Potential Issue "If no employee is assigned in the context, cart ID must be provided to this method."


Recommended Posts

Hi guys,

 

I run an eCommerce store using Prestashop, and I noticed if using the language / currency switching system while having any item in the cart, I would get the following error "If no employee is assigned in the context, cart ID must be provided to this method." - I didn't see much of this online so I thought id post about it to see if this was a common issue.

 

The way I worked around this fault was by going into the classes/Product.php file and removing the line, this seemed to stop the error page displaying, however I obviously don't know if this will cause issues down the line?

 

Any ideas here are much appreciated.

 

 

Link to comment
Share on other sites

  • 4 weeks later...
  • 2 weeks later...
  • 2 months later...

Same here with Prestashop 8.1.7

- Fresh install
- Default theme

In debug mode throw ths error:

Fatal error: Uncaught PrestaShopException: If no employee is assigned in the context, cart ID must be provided to this method. in /var/www/html/classes/Tools.php:1124 Stack trace: #0 /var/www/html/classes/Product.php(3663)

Extract code from Product.php class:
 

if (!is_object($cur_cart) || (Validate::isUnsignedInt($id_cart) && $id_cart && $cur_cart->id != $id_cart)) {
            /*
            * When a user (e.g., guest, customer, Google...) is on PrestaShop, he has already its cart as the global (see /init.php)
            * When a non-user calls directly this method (e.g., payment module...) is on PrestaShop, he does not have already it BUT knows the cart ID
            * When called from the back office, cart ID can be inexistant
            */
            if (!$id_cart && !isset($context->employee)) {
                die(Tools::displayError('If no employee is assigned in the context, cart ID must be provided to this method.'));
            }
            $cur_cart = new Cart($id_cart);
            // Store cart in context to avoid multiple instantiations in BO
            if (!Validate::isLoadedObject($context->cart)) {
                $context->cart = $cur_cart;
            }
  }


Any news?

Thanks!! :)

Link to comment
Share on other sites

  • 3 weeks later...

Hi,

 

Is there any update on this? I have this only when ordering a Giftcard through Knowband Giftcard module. All other orders work as expected. Although When I click the refresh in the browser when getting the error page "If no employee is assigned in the context, cart ID must be provided to this method." - I will be directed to the selected payment (mobile pay / Credit card etc) Running version PS 8.1.6?

Edited by TwinkleEye
context (see edit history)
Link to comment
Share on other sites

Hi again,

 

I did change the above code snip in the product.php with the below - now it seems to work:

 

if (!is_object($cur_cart) || (Validate::isUnsignedInt($id_cart) && $id_cart && $cur_cart->id != $id_cart)) {
// When a user (e.g., guest, customer, Google...) is on PrestaShop, they already have their cart as a global (see /init.php).
// When a non-user calls this method directly (e.g., payment module...), they do not have it but know the cart ID.
// When called from the back office, cart ID may be nonexistent.

if (!$id_cart && !isset($context->employee)) {
throw new Exception(Tools::displayError('If no employee is assigned in the context, cart ID must be provided to this method.'));
}

$cur_cart = new Cart($id_cart);

// Store cart in context to avoid multiple instantiations in BO
if (!Validate::isLoadedObject($context->cart)) {
$context->cart = $cur_cart;
}
}

Link to comment
Share on other sites

Hi,

It sounds like the issue might be related to the session handling when switching languages or currencies. Instead of modifying the core files, which can cause issues during updates, try the following:

Check if the issue persists in the latest version of PrestaShop.

If the error still occurs, consider using a custom module or override to handle the cart context during the switch. This way, you can maintain core integrity and avoid potential issues later on.

This should provide a cleaner and more maintainable solution. Let me know if it helps!

Edited by alexaali (see edit history)
Link to comment
Share on other sites

  • 5 weeks later...
  • 2 weeks later...

The issue is related to method Product::getPriceStatic being called with no context of current user cart. I recently encountered this issue when ussing hook ActionCartSave calling getProducts method which is using Product::getProductProperties which calls the method above.

If you have any module that uses hook ActionCartSave you can simply fix it by assigning cart to action hook context. 
 

$this->context->cart = $params['cart'];

If you still have this issue please paste entire error stack so we it's easier to find what's causing the problem.

Depending on the ps version you might also want to check for currency object if its loaded properly.

        if(!Validate::isLoadedObject($this->context->currency)){
            $curreny = new Currency($params['cart']->id_currency);
            $this->context->currency = $curreny;
        }

 

Edited by WisQQ (see edit history)
  • Like 1
  • 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...