Toggle Nav
My Cart 0

Not able to add product to cart with customizable options of type checkbox through REST API

Not able to add product to cart with customizable options of type checkbox through REST API

In Magento 2, REST API is the powerful feature to work with different core functionality and to create Magento-based eCommerce Applications. Magento provides many APIs to fulfill our different requirements to create a well-designed eCommerce application. We want to point out one known issue in REST API. Many merchants might have gone through this issue. this issue is related to cart API. when you want to add a product with different types of custom options then you can use the below API:


API endpoint: {{website_url}}/rest/default/V1/carts/mine/items
Method: POST

Request Body:

{
  "cartItem": {
    "item_id": 0,
    "sku": "string",
    "qty": 0,
    "name": "string",
    "price": 0,
    "product_type": "string",
    "quote_id": "string",
    "product_option": {
      "extension_attributes": {
        "custom_options": [
          {
            "option_id": "string",
            "option_value": "string",
            "extension_attributes": {
              "file_info": {
                "base64_encoded_data": "string",
                "type": "string",
                "name": "string"
              }
            }
          }
        ],
        "downloadable_option": {
          "downloadable_links": [
            0
          ]
        },
        "configurable_item_options": [
          {
            "option_id": "string",
            "option_value": 0,
            "extension_attributes": {}
          }
        ],
        "bundle_options": [
          {
            "option_id": 0,
            "option_qty": 0,
            "option_selections": [
              0
            ],
            "extension_attributes": {}
          }
        ]
      }
    },
    "extension_attributes": {
      "discounts": [
        {
          "discount_data": {
            "amount": 0,
            "base_amount": 0,
            "original_amount": 0,
            "base_original_amount": 0
          },
          "rule_label": "string",
          "rule_id": 0
        }
      ]
    }
  }
}

Sample request Body: 

{
    "cartItem": {
        "sku": "sku",
        "qty": 1,
        "quote_id": "quoteId",
        "product_option": {
            "extension_attributes": {
                "custom_options": [
                    {
                        "option_id": "12",
                        "option_value": "20,21,22"
                    }
                ]
            }
        }
    }
}

Above is a general format of the Add to cart API. As for the products with custom options you can pass option id and option value under the "custom_options" parameter as mentioned in "Sample request Body". you can pass different types of options like dropdown, radio button, etc without any issues but when you try to pass the single/multiple values in the custom option of checkbox type then you might get an error in response like below even if you have passed the correct values:



{
    "message": "The product's required option(s) weren't entered. Make sure the options are entered and try again."
}

No worries it is a Magento bug. There is one patch available in GitHub to fix this issue in Magento v2.3. please check the patch given in this link: Git

Solution:

Just add the below mentioned code on line no.:87 in "validateUserValue()" function in the "/vendor/magento/module-catalog/Model/Product/Option/Type/Select.php" file. check the updated function below.


public function validateUserValue($values)
{
	parent::validateUserValue($values);

	$option = $this->getOption();
	$value = $this->getUserValue();

	if (empty($value) && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
		$this->setIsValid(false);
		throw new LocalizedException(
			__("The product's required option(s) weren't entered. Make sure the options are entered and try again.")
		);
	}
	if (!$this->_isSingleSelection()) {
		/*Solution start*/
		if (is_string($value)) {
			$value = explode(',', $value);
		}
		/*Solution end*/
		$valuesCollection = $option->getOptionValuesByOptionId($value, $this->getProduct()->getStoreId())->load();
		$valueCount = is_array($value) ? count($value) : 0;
		if ($valuesCollection->count() != $valueCount) {
			$this->setIsValid(false);
			throw new LocalizedException(
				__(
					"The product's required option(s) weren't entered. "
					. "Make sure the options are entered and try again."
				)
			);
		}
	}
	return $this;
}

Flush the Magento cache from the admin after making changes in the code. Now you can add a checkbox type custom option using REST API without any issues.

NOTE: This issue is only found in Magento 2.3. it is solved in Magento 2.4. So no need to implement the patch if you are using Magento 2.4

Credits: https://github.com/magento/magento2/issues/23863

May 11, 2021
Did you like this post?
0
0