Skip to content
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

Adding a variant to the cart without selecting it gives an error #867

Closed
KingPatje opened this issue May 8, 2023 · 4 comments
Closed

Adding a variant to the cart without selecting it gives an error #867

KingPatje opened this issue May 8, 2023 · 4 comments
Labels

Comments

@KingPatje
Copy link

Description

When you press add to cart without selecting a Variant, you will get the following error:

DoubleThreeDigital\SimpleCommerce\Products\Product::variant(): Argument #1 ($optionKey) must be of type string, null given, called in D:\webdev\scommerce\vendor\doublethreedigital\simple-commerce\src\Http\Controllers\CartItemController.php on line 115

This is because in DoubleThreeDigital\SimpleCommerce\Http\Requests\CartItem\StoreRequest.php the validation rule says the variant may be nullable and if its not null it must be a string.

The Product::variant expects a string, so a null value isn't working. I tried making the variant required in the validation rules like this:

        $rules = [
            'product' => ['required', 'string'],
            'variant' => ['nullable', 'string'],
            'quantity' => ['required', 'numeric', 'gt:0'],

            'email' => ['nullable', 'email', function ($attribute, $value, $fail) {
                if (preg_match('/^\S*$/u', $value) === 0) {
                    return $fail(__('Your email may not contain any spaces.'));
                }
            }],
            'customer.email' => ['nullable', 'email', function ($attribute, $value, $fail) {
                if (preg_match('/^\S*$/u', $value) === 0) {
                    return $fail(__('Your email may not contain any spaces.'));
                }
            }],
        ];

        if(Product::find($this->get('product'))->purchasableType() == ProductType::Variant){
            $rules['variant'][0] = 'required';
        }

However, this gives a new error, where i lose track.

D:\webdev\scommerce\vendor\statamic\cms\src\View\Antlers\Language\Runtime\NodeProcessor
.php: 1799```




        

 

### Steps to reproduce

1. Install the starter kit from the documentation
2. Log in and create a Variant product
3. View the variant product and press Add to cart without selecting it

 

### Environment

this command does not work for me

@KingPatje KingPatje added the bug label May 8, 2023
@KingPatje KingPatje changed the title Adding a variant to the cart without selecting is gives an error Adding a variant to the cart without selecting it gives an error May 8, 2023
@duncanmcclean
Copy link
Owner

If the product has variants, you need to supply a variant before you can add the product to the cart.

@duncanmcclean duncanmcclean closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2023
@KingPatje
Copy link
Author

correct, but a customer should never see an exception, validation rules should handle this.

@duncanmcclean
Copy link
Owner

True, I'll re-open to fix that.

@github-actions
Copy link

Released as part of v5.0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants