-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4.x]: forgetCart() not working as expected #3353
Comments
Any thoughts on this would be much appreciated. Comes with the knock on impact of being unable to update beyond version 4.0.1.1 of the Stripe Commerce plugin as 4.1.0 now requires 4.3.3. |
Hi @samhibberd Thank you for your message, due to the nature of the getting the cart (now it uses cookies) you need to make sure you are omitting the cookies in your separate request. Below is a little AJAX example you can drop into you templates to test this theory.
Hope this helps, thanks! |
Hi @nfourtythree, thanks for the example but it's not quite representative of our use case. In 4.3.2 (and previous versions) we needed to use the forgetCart() method to allow us to call the native getCart() method to create a new cart, rather than returning the cart in the current session (which getCart() always does if it exists), we then load the session cart back in after we have retrieved are new cart. We use this for a quick checkout independent of any cart / basket that currently exists. The process goes:
The reason we need to do this is to take advantage of all the create cart logic which sets attributes, addresses etc etc: If this could be abstracted out of the getCart() method and called directly (or have a $forceNew argument?) then we could skip all of this, just create a new empty cart and that would be problem solved!! |
Hi @samhibberd Thank you for your reply. Below is a rough outline of some PHP code that I think could point you in the right direction for what you are trying to achieve. <?php
namespace modules\controllers;
use Craft;
use craft\commerce\Plugin;
use craft\web\Controller;
use yii\web\Response;
class CartController extends Controller
{
public function actionQuick(): Response
{
$request = Craft::$app->getRequest();
$cookies = $request->getCookies();
// Get current cookie info
$cookieName = Plugin::getInstance()->getCarts()->cartCookie['name'];
$cookie = $cookies->get($cookieName);
$number = $cookie->value;
// Set value
$cookie->value = null;
$newCart = Plugin::getInstance()->getCarts()->getCart();
// Set new cart data
// $newCart->setLineItems([
// // ...
// ]);
// Save new cart
Craft::$app->getElements()->saveElement($newCart);
// Forget cart
Plugin::getInstance()->getCarts()->forgetCart();
// Restore original cookie
$cookie->value = $number;
// This will be the original cart form the session
$currentCart = Plugin::getInstance()->getCarts()->getCart();
return $this->asSuccess('Success', [], '/shop');
}
} Hope this helps! |
Thanks @nfourtythree, will have a play now. |
Hi @samhibberd Just to keep you updated, I took a further look into this issue and have now pushed up a fix which we will look to get into a release of Commerce soon. This fix updates the Thanks! |
Thanks @nfourtythree, nice update. |
…working-as-expected Fixed #3353 `forgetCart()` not completely forgetting the cart
Commerce Thanks! |
Hi @nfourtythree, we finally had some time to update our craft install and test this fix properly, unfortunately it's still not working for us, unless we are doing something wrong?! In the example below it return the cart from the session and not the a newly created one as expected: // No cart passed let's create and save a new one
// - If there is a cart in the current session we need to forget it whilst we get / create the quick checkout cart
$sessionCartNumber = $cartsService->getHasSessionCartNumber() ? $cartsService->getSessionCartNumber() : false;
if($sessionCartNumber) {
$cartsService->forgetCart();
}
// - Create and save a new cart
$cart = $cartsService->getCart(forceSave: true);
// - Now forget the current session cart number again to remove the newly created cart from the session
$cartsService->forgetCart();
// - If there is one add the orginal cart back into the session
if ($sessionCartNumber) {
$cartsService->setSessionCartNumber($sessionCartNumber);
} Any thoughts?? |
@nfourtythree any thought on this? Or do you think this could be a different issue? |
Hi @samhibberd Thank you for your reply. We have pushed another update to the carts service that should fix this issue. You will need to update your code as it looks like you were trying to call a protected method on the carts service. Here is my advised update: $cartNumber = null;
if ($cartsService->getHasSessionCartNumber()) {
$cartNumber = $cartsService->getCart()?->number;
$cartsService->forgetCart();
}
// - Create and save a new cart
$cart = $cartsService->getCart(forceSave: true);
// - Now forget the current session cart number again to remove the newly created cart from the session
$cartsService->forgetCart();
// - If there is one add the orginal cart back into the session
if ($cartNumber) {
$cartsService->setSessionCartNumber($cartNumber);
} This fix will be included in the next release of Commerce. To get this early, change your "require": {
"craftcms/commerce": "dev-develop#04bbd83f8588a41295430a06e79d7a1765eb0bfe as 4.5.0",
"...": "..."
} Then run Thanks! |
Hey @nfourtythree great and thanks for fixing. Will test our side. Thanks for reviewing our code, that was actually an untested example of what we are doing. We actually use custom behaviors in this case to get a session number only if it exists, which allows us to access the protected properties. |
4.5.1 is now out with a fix for this. Happy leap day! |
What happened?
It looks like the changes to the Carts service
getSessionCartNumber()
method have introduced some unexpected behavior with theforgetCart()
logic, as far as I can tell, it's not working for our use case:We have some quick checkout logic that operates separate to the session cart and we used to be able to use the following to create a new cart separate from the session (before reloading in the original cart back into the session once we have finished):
But whatever we do, in 4.3.3, the
getCart()
logic in the example above returns the cart currently stored in the session (if there is one). Reverting to 4.3.2 reverts to the expected behavior.It looks to be related to the request cookies I think??
Craft CMS version
4.5.12
Craft Commerce version
4.3.3
PHP version
8.2
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
The text was updated successfully, but these errors were encountered: