2

I hope someone can help me.

I need to create an observer with creating a block on the cart table, which allows the customer to choose from radio button the options I will provide in each item separately in the product table, within the cart page and save it into the database to let the merchant see the options that his clients chose.

To achieve this I use quote item options and events. Here's what I have done till now:

My config.xml

<sales_quote_collect_totals_before>
    <observers>
        <shippingoptions>
            <type>singleton</type>
            <class>shippingoptions/observer</class>
            <method>test</method>
        </shippingoptions>
    </observers>
</sales_quote_collect_totals_before>

My observer method

public function test($observer)
{
    $quote = $observer->getQuote();
    $quote_items = $quote->getItemsCollection();
    foreach ($quote_items as $item) 
    {
        $additionalOptions = array(
            array(
                'title' => 'custom option title',
                'type' => 'radio', // could be drop_down ,checkbox , multiple
                'is_require' => 1,
                'sort_order' => 0,
                'values' => $this->getOptions()
                )
        );
        $item->addOption(
            array(
                'code'  => 'additional_options',
                'value' => serialize($additionalOptions),
                )
            );
     }
}

Let's say that the getOptions return an array. So I can't, actually render the radio button in the cart table. It just adds the option like this output under each item:

<dd>custom option title<br>
    radio<br>
    1<br>
    0<br>
    Array
</dd>

So I figure that I need to use a block to do the rendering thing, I try to do that but it didn't go well from my side. So could you help me to do that with from the observer side without overwriting the cart.phtml or any checkout file (And of course for a good reason)?

Here is a screenshot just to clarify things up: enter image description here

Utsav Gupta
  • 1,243
  • 1
  • 18
  • 48
Ahmad alkaid
  • 170
  • 1
  • 8

1 Answers1

3

You can do it by Observer.
1.You need to add Following Event in Config.xml

<sales_quote_collect_totals_before>
        <observers>
            <hackathon_presentation>
                <type>singleton</type>
                <class>modulename/observer</class>
                <method>salesQuoteAddressCollectTotalsBefore</method>
            </hackathon_presentation>
        </observers>
</sales_quote_collect_totals_before>

In Observer.php add following mentioned code

 public function salesQuoteAddressCollectTotalsBefore($observer)
    {
        $quote = $observer->getQuote();
        $quote_items = $quote->getItemsCollection();
        foreach ($quote_items as $item) {
            $additionalOptions = array(
                array(
                    'code'  => 'my_code',
                    'label' => 'This text is displayed through additional options',
                    'value' => 'ID is ' . $item->getProductId() . ' and SKU is ' . $item->getSku()
                )
            );
            $item->addOption(
                array(
                     'code'  => 'additional_options',
                     'value' => serialize($additionalOptions),
                )
            );
        }
    }

For More see Here and Here

Arunendra
  • 7,446
  • 3
  • 29
  • 54
  • Hi @arunendra, I have used these articles in this extension, and as you can see in the screen shot that I couldn't set the radio buttons probably. So what I need is to set some sort of template to show the radio buttons and save the value in the database. – Ahmad alkaid Mar 01 '16 at 04:01
  • 1
    get More help here http://stackoverflow.com/questions/9334115/magento-change-custom-option-value-before-adding-it-to-cart/9344336#9344336 and http://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input/9496266#9496266 – Arunendra Mar 01 '16 at 04:57
  • @Arunendra I have some issue in observer throw exception, could you pls help me? https://magento.stackexchange.com/q/284914/57334 – zus Aug 09 '19 at 06:30
  • @zus link is not working – Arunendra Aug 09 '19 at 14:45