Giter Site home page Giter Site logo

Comments (7)

tobiasfabian avatar tobiasfabian commented on May 29, 2024 1

I’m sorry, we won’t add something like extras to our Cart class in the near future.

I would suggest the following workaround:

Ice cream toppings should be own products. When you put your ice cream (with topping) into the cart you should – at the same time – put the topping product into the cart. When you add the topping product you can add a for key to match the topping with the ice cream.

$cart = cart();
$cart->add([
  'id' => 'vanilla-ice-cream',
]);
$cart->add([
  'id' => 'chocolate-sprinkles',
  'for' => 'vanilla-ice-cream',
]);

from merx.

plagasul avatar plagasul commented on May 29, 2024 1

I confirm my approach works for my case.

'Frame' is created as a separate product, and so it is included in merx calculations.

When adding a frame to the cart, the print this frame belongs to, receives a +1 on a 'frame' attribute, so we can keep track of how many frames are associated to each print among the total ammount of frames in the cart.

Finally it is often needed to filter 'frame' as a product, because we don't really want the customer to buy it separately,

from merx.

plagasul avatar plagasul commented on May 29, 2024

Thank you for the workaround.

Take care.

from merx.

plagasul avatar plagasul commented on May 29, 2024

@tobiasfabian I've just noticed a possible problem with this workaround.

  • I add vanilla-ice-cream to the cart
  • I add chocolate-sprinkes to the cart with a for key vanilla-ice-cream
  • I add stracciatella-ice-cream to the cart
  • I add chocolate-sprinkes to the cart with a for key stracciatella-ice-cream

Since chocolate sprinkles is a product, adding it twice should just incremenet the quantity right?

I can't have two different instances of a product with two different for keys, or can I ?

Danke

from merx.

tobiasfabian avatar tobiasfabian commented on May 29, 2024

Oops, I think you are right, this won’t work out.

The for key could be an array. But this would be a quite shaky solution.


Maybe every product variant has to be its own product (I think this is the default behavior of shop systems, at least Magento works like this)

You can have a master product e.g. Vanilla Ice Cream. And each master product could have variants/children (e.g. Chocolate Sprinkles).

So you don’t have to create variants for each Ice Cream you could use Virtual Pages.

Untested Example

/site/models/product.php

<?php
class ProductPage extends Kirby\Cms\Page
{
    public function children()
    {
        $variants = [];

        foreach ($this->site()->variants()->toStructure() as $variant) {
            $variants[] = [
                'slug'     => $variant->slug(),
                'num'      => 0,
                'template' => 'product-variant',
                'model'    => 'product-variant',
                'content'  => [
                    'title'  => $variant->title(),
                    'price'  => $this->price()->toFloat() + $variant->additionalPrice()->toFloat(),
                ]
            ];
        }

        return Pages::factory($variants, $this);
    }
}

If the customer wants Vanilla Ice Cream with Chocolate Sprinkles the code would look like this

$cart->add('vanilla-ice-cream/chocolate-sprinkles');

from merx.

plagasul avatar plagasul commented on May 29, 2024

So you are overriding the children() method of the page class for the product page, and you use ::factory method to return variants as virtual pages. That's very clever, I wouldn't have thought about it

It does not fit my use-case tho, which was not really about ice-cream ( It seemed to me that was a clearer example) but I thought about a way that does fit. This is my cart page

image

My only 'extra' or 'variant' is adding a frame to the print, so I could:

- Add 'Frame' as its own product
- Include 'frames' as key when adding a product to cart, or as a cart.field of the product page.

then

- When user clicks (+) on the frame section of the Cart Page
  - I add +1 Frame product to the cart 
  - I add +1 to the 'frames' key of the associated product (Double Cherry, in the image)

As frames are products, their price is included in cart()'s calculations.

And with the 'Frames' key I can track how many frames for each product, which should make it relatively easy to associate frames and their price, and echo the data in the page, such as adding frames to the product total (they are already added to cart's total)

Any comments appreciated.

I'll come back when I test it.

Thank you

from merx.

scsskid avatar scsskid commented on May 29, 2024

this issue should be linked in the cookbook

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.