Comments (17)
Sorry I forgot to explain the changes of the commit.
If you are working with product variants you have to use key
instead of id
to update the cart item.
This is how it should work.
$cart->updateItem([
'key' => $data['variation'],
'quantity' => (float) ($data['quantity'] ?? 1),
]);
from merx.
@alexpi Today, I tried a different approach.
With the latest commit (d05dd4d) in the develop branch I modified the Cart::add()
method. The Cart::add()
method now can handle one or two arguments.
You can provide a unique key as first argument and your cart item as the second argument. As long as the key
is unique the items won’t be merged. So, the following would be possible.
$cart->add('jacket-48', [
'id' => 'jacket',
'size' => 48,
]);
$cart->add('jacket-50', [
'id' => 'jacket',
'size' => 50,
]);
To remove a cart item your code must be as follows.
$cart->remove('jacket-48');
from merx.
@tobiasfabian I tried your last solution and adding to cart works for me! i will come back after testing more with the cart update and remove.
from merx.
@tobiasfabian found a solution for me with the add
method. looks like this inside my config.php
:
'routes' => [
[
'method' => 'post',
'pattern' => 'shop-api/add-to-cart',
'action' => function() {
try {
$data = kirby()->request()->data();
if (merx()->cart()->find($data['id'] . $data['size'])) {
merx()->cart()->add($data['id'] . $data['size'], [
'id' => $data['id'],
'quantity' => merx()->cart()->find($data['id'] . $data['size'])['quantity'] + $data['quantity'],
'size' => $data['size'],
]);
} else {
merx()->cart()->add($data['id'] . $data['size'], [
'id' => $data['id'],
'quantity' => $data['quantity'],
'size' => $data['size'],
]);
}
go(page($data['id'])->url() . '/addedtoorder:yes');
} catch (Kirby\Exception\Exception $ex) {
return $ex->toArray();
}
},
],
]
from merx.
Hello @sigistardust. Did you find a solution to your problem? I am in the exact same situation.
from merx.
hi @alexpi
i actually found that splitting the variants up in different products is the better solution for me.
but still, this question is very interesting to me, especially for future projects.
from merx.
It would be possible to compare every attribute of a product and only merge products when every single attribute (except quantity
) is the same. This would require some modifications of the $productList->updateItem() method.
The following code would result in two ProductList/Cart entries and won’t be merged.
$cart->add([
'id' => 'jacket',
'size' => 48,
]);
$cart->add([
'id' => 'jacket',
'size' => 50,
]);
What do you think?
from merx.
So, if I get this right, this modification would allow products with the same id to be added to the cart, and abollish the necessity for subpage variations?
from merx.
Yes, that’s correct.
from merx.
So, should I try to modify your code to enable this functionality or are you considering adding it to the plugin?
from merx.
@tobiasfabian @alexpi glad to see that you are looking for possible improvements on that! shall I leave the issue open?
from merx.
@sigistardust I haven't tried the solution yet (I will in a few days), but it seems like it will solve my use case perfectly.
from merx.
@tobiasfabian @alexpi glad to see that you are looking for possible improvements on that! shall I leave the issue open?
Yes, I’ll close it as soon as I think the issue is solved.
@sigistardust I haven't tried the solution yet (I will in a few days), but it seems like it will solve my use case perfectly.
Looking forward to your feedback.
from merx.
@tobiasfabian could you please tell me what the easiest way is to:
- filter the cart for a certain variation
- update the quantity of that variation
?
thank you very much!
from merx.
@tobiasfabian I finally got to try this. It works great! I can't find how to update a variation on the cart though.
from merx.
I can't find how to update a variation on the cart though.
The latest commit (8df5d40) should fix this.
from merx.
Sorry, I don't understand how to use updateItem to update a variation. I have the following code:
'pattern' => 'shop-api/update-cart-item',
'method' => 'POST',
'action' => function() {
try {
$data = kirby()->request()->data();
$product = kirby()->page($data['id']);
$cart = merx()->cart();
$cart->updateItem([
'id' => $data['variation'],
'quantity' => (float) ($data['quantity'] ?? 1),
]);
$item = [
'id' => $data['id'],
'variation' => $data['variation'],
'buildtime' => ((float) $data['quantity'] === 1) ? 'false' : buildtime($product, $data['material'], $data['variations']),
'total' => $cart->find($data['variation'])['sum']
];
return [
'status' => 200,
'item' => $item,
'cart' => [
'total' => $cart->getSum(),
'count' => $cart->count()
]
];
} catch (Kirby\Exception\Exception $ex) {
return $ex->toArray();
}
}
When calling updateItem
, I am passing the variation
in order to find it in the cart, but this action also changes the product's id
, which I need in order to get the original product page.
from merx.
Related Issues (20)
- more examples HOT 4
- $cart->getSum() includes tax? HOT 1
- Documentation website down HOT 2
- Syntax error with Starter Kit HOT 1
- No such payment_intent HOT 1
- Order validation on multi-language sites breaks on Kirby 3.6.4+ HOT 6
- Compatibility issue with Kirby 3.7.1 HOT 4
- Is it possible to disable online payments? HOT 1
- Upgrade to Paypal API v2 HOT 5
- Wagnerwagner\Merx\ProductList::__set(): Return type must be void when declared HOT 2
- Discount per product? HOT 1
- How do you add different types of sizes for a product? HOT 2
- Deprecated Stripe API
- Save orders in a specific language HOT 1
- Order pages with identical page num created HOT 3
- Declaration of Wagnerwagner\Merx\ProductList::__set(string $key, $value) must be compatible with Kirby HOT 1
- Cart and Checkout problem HOT 1
- Cart()->remove() not removing product - Issue with UUID's HOT 2
- Dynamic shipping prices (request) HOT 1
- Allow donation or 'pay what you want' HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from merx.