duncanmcclean / simple-commerce

A simple, yet powerful e-commerce addon for Statamic.
https://statamic.com/addons/duncanmcclean/simple-commerce
Other
143 stars 37 forks source link

Products with no variants are having empty arrays saved into their content files which is breaking the product variant dropdown AND preventing adding to cart #892

Closed simonfrost closed 1 year ago

simonfrost commented 1 year ago

Description

Hi there.

We have ~200 products, of which around 150 are only available in the bricks and mortar store (we've hidden the 'Add to Cart' button based on a flag. Of the others, around 25 have product variants, and 25 do not.

Our product blueprint has the Product Variants fieldset on a tab called Product Options as per your documentation.

Screenshot 2023-07-28 at 19 27 12

On our purchasable products that DO have variants, everything seems to be in order. We can see the options, validation requires us to choose one, and we can successfully add to cart.

Screenshot 2023-07-28 at 19 30 23

Screenshot 2023-07-28 at 19 30 54

On our purchasable products that DO NOT have variants however, we STILL see the dropdown, but there are no options displayed.

Screenshot 2023-07-28 at 19 32 05

Screenshot 2023-07-28 at 19 32 28

Nothing can be selected, so we get the 'Please select an item in the list' when hitting Add to Cart.

If we temporarily hide the select box loop from the view, we can then submit, but get an error on the cart end, saying:

DoubleThreeDigital\SimpleCommerce\Products\Product::variant(): Argument #1 ($optionKey) must be of type string, null given, called in /Users/simonfrost/Sites/sound_org/vendor/doublethreedigital/simple-commerce/src/Http/Controllers/CartItemController.php on line 114

Screenshot 2023-07-28 at 19 39 56

We discovered that even if a product has no variants, whenever it is saved (or a new one created) there is an empty array saved in the product_name.md file.

Screenshot 2023-07-28 at 19 41 23

If we manually remove this entry (lines 73-75 in the example) and save, the options dropdown now correctly doesn't show (as there are no variants), and we can add the non variant product to our cart.

The problem we have is that any newly added products, or edited products ALWAYS seem to get this blank array written back to them, and this prevents us adding products without variants to the cart.

Maybe we're not understanding the functionality. Maybe once you have added product variants fieldset to the products blueprint, ALL products need at least one variant. Seems unlikely though. I hope I've explained the issue we're having satisfactorily, and you can offer some kind of solution!

Fabulous add-on btw. We're about to launch and purchase :-)

Steps to reproduce

Please see above.

Environment

Laravel Version: 9.33.0 PHP Version: 8.1.21 Composer Version: 2.4.2 Statamic Addons: 3 Antlers: runtime Stache Watcher: Enabled Static Caching: Disabled Version: 3.4.7 PRO

Statamic Addons doublethreedigital/simple-commerce: 4.5.8 jezzdk/statamic-google-maps: 1.2.2 rias/statamic-data-import: 1.2.2

duncanmcclean commented 1 year ago

Simple Commerce looks for a product_variants field in your blueprint - if it finds one, it assumes it's a variant product. Otherwise, it'll expect a 'Price' field to exist on the blueprint.

The workaround for now (which is the way it's done in the starter kit) is to use two different product blueprints if you need ones with variants & ones without variants.

Although, I might be able to change it to just not save those empty arrays which might fix your issue. I'd need to do some testing.

simonfrost commented 1 year ago

Thanks @duncanmcclean - much appreciated.

I'll have a play with 2 x blueprints (say simple_product and variant_product) on a branch, but if you could have a look to not save blank product_variants arrays if you haven't explicitly added a variant, that would save me a lot of bother, and I think would make for a more flexible/intuitive solution because you'd only need the one product blueprint.

Also - again, this may be my lack of knowledge, but if I go down the 2 x blueprint path, what if I have a product that I subsequently decide I do want variants on (or vice versa)? Can you swap the blueprint on an existing product with data, or do you need to recreate it? That would be a pain given the majority of our products are using the product_variants fieldset (with no variants added) but the fact that we conditionally removed the options/qty/add to cart UI (can only buy them in the physical shop) means we missed the glitch while adding and testing them!

Really appreciate your quick response. Thanks again.

github-actions[bot] commented 1 year ago

Released as part of v5.3.3.

duncanmcclean commented 1 year ago

I've just tagged a release which should fix product_variants fields being saved with empty arrays.

Also - again, this may be my lack of knowledge, but if I go down the 2 x blueprint path, what if I have a product that I subsequently decide I do want variants on (or vice versa)? Can you swap the blueprint on an existing product with data, or do you need to recreate it? That would be a pain given the majority of our products are using the product_variants fieldset (with no variants added) but the fact that we conditionally removed the options/qty/add to cart UI (can only buy them in the physical shop) means we missed the glitch while adding and testing them!

Yes, you can swap blueprints manually in the entry's markdown file:

blueprint: variants_product_blueprint