0

I have a simple product with a custom product option defined as a text field in the backend.

After adding the simple product to the cart, with no value set for the custom product option, I'd like to programmatically set the custom product option on the quote item.

It's important that the custom product option is stored exactly the same way as if it was added manually by the user on the product page, before adding the product to the cart.

2
  • you can do that with an observer on add to cart, then just set the product data with that custom option. do you need help with teh code?
    – Haim
    Commented Oct 31, 2018 at 15:07
  • Yes, if you have any working code, that'd be great. I've been trying to do exactly as you described, but without success so far.
    – Louis B.
    Commented Nov 1, 2018 at 9:53

1 Answer 1

0

Finally figured it out, here's the syntax I'm using. Keep in mind that this only sets one custom option for a product. If you have multiple custom options you'd have to change it a bit:

$customOptionProduct = $quoteItem->getProduct();
$customOptionProductId = $customOptionProduct->getId();
$customOptionId = 123;
$customOptionValue = 'foo';

$quoteItem->addOption(new Varien_Object(
    array(
        'product_id' => $customOptionProductId,
        'product' => $customOptionProduct,
        'code' => 'option_ids',
        'value' => $customOptionId
    )
));

$quoteItem->addOption(new Varien_Object(
    array(
        'product_id' => $customOptionProductId,
        'product' => $customOptionProduct,
        'code' => 'option_' . $customOptionId,
        'value' => $customOptionValue
    )
));

This is all done in an observer observing checkout_cart_product_add_after, but could probably be done anywhere a quote item can be accessed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.