Magento2: Unable to set negative custom option fixed price in admin view.

Created on 6 Nov 2016  路  16Comments  路  Source: magento/magento2

In versions 2.1.1 and 2.1.2, setting custom options for a product with fix price settings will not allow a negative value. I'm not sure if this is a simple ui problem or if there is a validator issue. Either way, this was always an option in Magento 1.x in which you can set discounts on an item via custom option. Uploading values via CSV works correctly, however making admin view changes for given product will fail validation via negative values.

Preconditions


  1. Magento 2.1.2
  2. PHP 7

Steps to reproduce

  1. Freshly install Magento.
  2. Create product with a custom option.
  3. Attempt to place a negative value in the custom option price input.

Expected result

  1. Item validates and clients using the custom option allows a price reduction.

Actual result

  1. Cannot save item. Validator fails.

capture

Catalog distributed-cd Fixed in 2.2.x Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x bug report

Most helpful comment

I am aware that editing the Magento core is a no-no, however I am pressed for time and needed a very quick fix. I'll be learning how to override things the proper way any day now....

A nice gentleman was kind enough to email me what he found:

vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

Set false for validate-zero-or-greater

vendor/magento/module-catalog/Model/Product/Option/Validator/DefaultValidator.php

protected function isNegative($value)
{
//return intval($value) > 0;
return false;
}

The code speaks for itself. The isNegative will always return false. I am not sure what other implications there are for doing it this way, however I have tested that a negative value will still offset the price as intended.

All 16 comments

Thank you for reporting. Internal issue created: MAGETWO-60573

Hi @WaterSlide did you fix this issue?
I have the same issue in Magento 2.1.3

I am aware that editing the Magento core is a no-no, however I am pressed for time and needed a very quick fix. I'll be learning how to override things the proper way any day now....

A nice gentleman was kind enough to email me what he found:

vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

Set false for validate-zero-or-greater

vendor/magento/module-catalog/Model/Product/Option/Validator/DefaultValidator.php

protected function isNegative($value)
{
//return intval($value) > 0;
return false;
}

The code speaks for itself. The isNegative will always return false. I am not sure what other implications there are for doing it this way, however I have tested that a negative value will still offset the price as intended.

I have this issue as well! Needs to be solved in core Magento... thank you WaterSlide for the solution for now. :-)

I need this also

@WaterSlide I tried your solution but it still give me "Invalid option value" error when I try to set negative price to some custom option.

@WaterSlide Thanks alot. Its really works for me.

Any news on a fix for this issue?

Any news on a fix for this issue?

Thanks @WaterSlide for the quick fix! Having one last cosmetic issue, the frontend still shows a + sign before the -. Anyone knows how to remove it?

screen shot 2017-09-21 at 7 00 23 pm

@WaterSlide, thank you for your report.
We've created internal ticket(s) MAGETWO-60573 to track progress on the issue.

Thanks for the fix. It works on Radio, but Drop Down adds a +- in front of the number. I really hope this gets fixed soon. I have 2 sites to migrate after the holidays that depend on it.

I'm running M 2.2.1. This remains an issue. We need to be able to set a negative value for price in the product's "customizable options" section. What is the progress on MAGETWO-60573?... or should I use this solution below?

vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

Set false for validate-zero-or-greater

vendor/magento/module-catalog/Model/Product/Option/Validator/DefaultValidator.php

protected function isNegative($value)
{
//return intval($value) > 0;
return false;
}

I've created a pull request for this issue, for future releases. In case you run into this, here is a fix for 2.1.x (tested on 2.1.9):

https://github.com/luckyduck/negative-option-value

It also solves the cosmetic frontend issue, where negative option values have a plus sign as prefix (like +-3,50)

I tried to use Luckyduck's module on Magento 2.2.5 but this caused a problem in the Admin backend where under Customisable Options there would be no boxes to enter $ values.

To fix this, I edited:
UiDataProviderProductFormModifierCustomOptions.php

Modify from line 25 onwards:

class CustomOptions extends Options
{
    protected function getPriceFieldConfig($sortOrder)
    {
        return [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => __('Price'),
                        'componentType' => Field::NAME,
                        'component' => 'Magento_Catalog/js/components/custom-options-component',
                        'formElement' => Input::NAME,
                        'dataScope' => static::FIELD_PRICE_NAME,
                        'dataType' => Number::NAME,
                        'addbefore' => $this->getCurrencySymbol(),
                        'addbeforePool' => $this->productOptionsPrice->prefixesToOptionArray(),
                        'sortOrder' => $sortOrder,
                        'validation' => [
                            'validate-zero-or-greater' => false
                        ],
                    ],
                ],
            ],
        ];
    }
}

Hi @WaterSlide

This issue is now fixed and it's expected to be delivered with the upcoming 2.2.6 release. Meanwhile, you may use this commit as a reference 8d4124cc4125e81d4a41ae71aca12c8e12f297a6

Thanks

Was this page helpful?
0 / 5 - 0 ratings