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

Product variant option value not selected in admin #8999

Closed
nbjohan opened this issue Dec 1, 2017 · 4 comments
Closed

Product variant option value not selected in admin #8999

nbjohan opened this issue Dec 1, 2017 · 4 comments
Labels
Potential Bug Potential bugs or bugfixes, that needs to be reproduced.

Comments

@nbjohan
Copy link

nbjohan commented Dec 1, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? ???
RFC? no
Sylius version 1.0.3

Problem

In the Sylius admin, when editting a product variant with two options: Color and Size a situation can occur when the correct option value (eg: red or XL) is not loaded in the <select> and the default value is selected instead.

It turns out to be caused by the order in which the option values are saved. This is visible in \Symfony\Component\Form\ChoiceList\ArrayChoiceList::getValuesForChoices where $this->options is a list of colors, but the $choices array contains a size.

Expected situation

The correct option value is selected regardless of the order the options are saved in the database.

Solution

As for a solution I can think of three possibilities:

  • Correctly sort the values in the query (but doctrine fecthes the options, so i think this is not the best solution/possible)
  • Add a sort somewhere in the code, before values/options are passed to the Symfony formbuilder.
  • Always make sure the options/values are in the correct order in the database (bad practise imo.)

I have not managed to find a solution. Maybe I can create a patch if someone can confirm the situation/point in a direction.

Additional data

Working situation:

SELECT
  t0.code AS code_1,
  t0.id AS id_2,
  t0.option_id AS option_id_3
FROM
  sylius_product_option_value t0
  INNER JOIN sylius_product_variant_option_value
    ON t0.id = sylius_product_variant_option_value.option_value_id
WHERE sylius_product_variant_option_value.variant_id = 5206;
code_1   id_2  option_id_3  
sz-s     25    3            
cl-navy  29    4  

form_working
code_working

Bug situation:

SELECT
  t0.code AS code_1,
  t0.id AS id_2,
  t0.option_id AS option_id_3
FROM
  sylius_product_option_value t0
  INNER JOIN sylius_product_variant_option_value
    ON t0.id = sylius_product_variant_option_value.option_value_id
WHERE sylius_product_variant_option_value.variant_id = 5196;
code_1   id_2  option_id_3  
cl-aqua  24    4            
sz-s     25    3 

form_broken
code_broken

Maybe related to:
#8144

@pamil pamil added the Potential Bug Potential bugs or bugfixes, that needs to be reproduced. label Dec 1, 2017
@pamil pamil added this to the 1.0 milestone Dec 1, 2017
@czende
Copy link
Contributor

czende commented Dec 17, 2017

Related to #8744

@stefandoorn
Copy link
Contributor

I can confirm this still exists on a v1.2.x installation. I unfortunately have no clue why it occurs though. It seems the same as issue as #8144. Weird thing is that client created product on staging and had the options available, now did it at production and no options shown (setups + code are identical). I will try to figure out what they've done differently this time (e.g. order of adding things).

@stefandoorn
Copy link
Contributor

Product options are created in the same order both on staging & production in my case. It's hard to see whether the option values have been created at the same time or later. The variants in both cases are generated automated, which basically takes the output of the sylius.generator.product_variant service, add some default pricing and categories on it, sets a default name and saves it all, short version here:

        $variantGenerator = $this->get('sylius.generator.product_variant');
        $variantGenerator->generate($product);

        .....
        $countNew = 0;
        foreach ($product->getVariants() as $productVariant) {
            /** @var ProductVariantInterface $productVariant */
            // Skip existing variants
            if ($productVariant->getId() !== null) {
                continue;
            }

            ++$variantCount;

            $name = $variantCount;
            foreach ($productVariant->getOptionValues() as $productOptionValue) {
                $name .= '-' . $productOptionValue->getValue();
            }

            $productVariant->setName($name);
            $productVariant->setCode(sprintf('%s-variant-%d', $product->getCode(), $variantCount));
            ...

            foreach ($channelRepository->findAll() as $channel) {
                /** @var Channel $channel */
                /** @var ChannelPricingInterface $channelPricing */
                $channelPricing = $channelPricingFactory->createNew();
                $channelPricing->setChannelCode($channel->getCode());
                $channelPricing->setPrice(0);

                $productVariant->addChannelPricing($channelPricing);
            }

            $countNew++;
        }

        // Save it
        if ($countNew > 0) {
            /** @var EntityManager $productManager */
            $productManager = $this->get('sylius.manager.product');
            $productManager->persist($product);
            $productManager->flush();
        }

Client claims this has been used both on staging as on production, which makes me believe the order is similar.

@stefandoorn
Copy link
Contributor

Ah, it seems to be easier in my case; there is no option selected (and therefore the placeholder is active) due to option values being deactivated. So the variant can still exist, option value is disabled, it won't be used in the form anymore and as soon as you want to save that variant for whatever reason it will fail. Not sure that's intended of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Potential Bug Potential bugs or bugfixes, that needs to be reproduced.
Projects
None yet
Development

No branches or pull requests

4 participants