Giter Site home page Giter Site logo

Comments (17)

tobiasfabian avatar tobiasfabian commented on May 30, 2024 2

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.

tobiasfabian avatar tobiasfabian commented on May 30, 2024 1

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

sigistardust avatar sigistardust commented on May 30, 2024 1

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

sigistardust avatar sigistardust commented on May 30, 2024 1

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

alexpi avatar alexpi commented on May 30, 2024

Hello @sigistardust. Did you find a solution to your problem? I am in the exact same situation.

from merx.

sigistardust avatar sigistardust commented on May 30, 2024

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.

tobiasfabian avatar tobiasfabian commented on May 30, 2024

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.

alexpi avatar alexpi commented on May 30, 2024

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.

tobiasfabian avatar tobiasfabian commented on May 30, 2024

Yes, that’s correct.

from merx.

alexpi avatar alexpi commented on May 30, 2024

So, should I try to modify your code to enable this functionality or are you considering adding it to the plugin?

from merx.

sigistardust avatar sigistardust commented on May 30, 2024

@tobiasfabian @alexpi glad to see that you are looking for possible improvements on that! shall I leave the issue open?

from merx.

alexpi avatar alexpi commented on May 30, 2024

@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 avatar tobiasfabian commented on May 30, 2024

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

sigistardust avatar sigistardust commented on May 30, 2024

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

alexpi avatar alexpi commented on May 30, 2024

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

tobiasfabian avatar tobiasfabian commented on May 30, 2024

I can't find how to update a variation on the cart though.

The latest commit (8df5d40) should fix this.

from merx.

alexpi avatar alexpi commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.