Add a quote item with a info_buyRequest buy request

Add a quote item with a info_buyRequest buy request

July 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add a quote item with a info_buyRequest buy request

Adding a Quote Item with info_buyRequest Buy Request

As a senior developer, I understand your need to calculate product prices using a custom formula without adding the item to the cart in Magento 2. You’re on the right track by trying to add a quote item and set the info_buyRequest option.

Let’s break down your issue and provide a step-by-step solution.

The Current Approach

You’ve attempted to create a quote item, load product options, and add them to the item. However, you’re facing challenges in setting up the info_buyRequest correctly and getting the desired output from the _getBuyRequest() function.

Here’s what you’ve tried:

  1. Loaded the product using the product ID.
  2. Retrieved custom product options.
  3. Created a new quote item object.
  4. Attempted to add the product to the cart and save the quote.
  5. Set product options for the item.
  6. Created an info_buyRequest array with encoded URL, product ID, and quantity.
  7. Tried to set this array as an option for the item.

The Issue

The main problem lies in how you’re setting up the info_buyRequest. You’re trying to create a new Quote\Item\Option object with the buy request data directly, which isn’t the correct approach.

Additionally, when you call $item->setProductOptions($infoBuyRequest), it’s expecting an array of options, not just one option. This is why you don’t see the expected output in your current implementation.

The Solution

To resolve this issue, we need to follow these steps:

  1. Load the product and its options correctly.
  2. Create a new quote item and associate it with the loaded product.
  3. Prepare the info_buyRequest data correctly as an array of options.
  4. Add this option to the quote item along with other custom options.

Here’s an example of how you can achieve this:

$productId = 5862;
$qty = 100;

$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$customOptions = $this->_objectManager->get('Magento\Catalog\Model\Product\Option')
    ->getProductOptionCollection($product);

$quote = $this->cart->getQuote();
$quote->addProduct($product, $qty)->save();

$item = $quote->getItemByProduct($product);
$options = [];

// Prepare info_buyRequest option
$infoBuyRequestOptions = [
    'info_buyRequest' => [
        "uenc" => "aHR0cHM6Ly93d3cud2V0YWcuY2EvZW5fY2EvbmFtZS10YWdzL25hbWUtdGFncy1pbnNwaXJhdGlvbi9jdXN0b20tbmFtZS10YWctb3JkZXIv",
        "product" => $productId,
        "qty" => $qty
    ]
];
$options['info_buyRequest'] = json_encode($infoBuyRequestOptions);

// Add other custom options if needed
foreach ($customOptions as $_option) {
    $options[$_option->getCode()] = $_option->getValue();
}

// Set product options for the item
$item->setProductOptions($options);

// Get the buy request data
var_dump($this->_getBuyRequest($item));

Key Points to Consider

  1. Make sure you have loaded the required dependencies and have access to the needed objects.
  2. Ensure that your custom formula for calculating product prices is correctly implemented in a separate module.
  3. Remember that this approach doesn’t actually add the item to the cart; it only temporarily associates it with the quote.

Conclusion

Adding a quote item with an info_buyRequest buy request can be achieved by following the correct data structure and setting up the options array properly. By loading the product correctly, preparing the info_buyRequest as an option, and adding it to the item along with other custom options, you can successfully retrieve the desired data using the _getBuyRequest() function.

Remember that this is a temporary association; the item is not actually added to the cart. If you need to persist this information or use it in a different context, you may need additional logic to handle the quote and item management.

For more detailed guides on Magento 2 development and custom module creation, consider visiting Laravel Company for comprehensive resources.

Happy coding!

Magento 2 Checkout Catalog Configuration

Our network