Pregunta

Quiero añadir opción personalizada para quoteitem usando observador cuyo caso observador checkoutCartProductAddAfter y los incendios después de Producto añadido al carrito.

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{

 $item = $observer->getQuoteItem();  
  $item->addOption(new Varien_Object(
            array(
                    'product' => $item->getProduct(),
                    'label' => 'Free Gifts',
                    'value' => 'Spend $50 and get gift product worth $9.99'
                 )
        ));
    return;

}

Mi trabajo es observador pero yo no soy capaz de añadir opción personalizada al producto agregado. por favor proporcione ayuda a añadir la opción personalizada mediante observador a producto se agregó.

¿Fue útil?

Solución

@Tim dio una charla sobre este tema en el fin de semana: https: / /docs.google.com/presentation/d/1efPznQSVTrT1HAD1xQvCPC-Tgvr8jYok4X7ZEJhm9jE/edit

Lo que queremos es Método 2: Añadir siguiente suceso en 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>

En Observer.php Añadir según Método

   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),
                )
            );
        }
    }

Aquí es más sobre este tema:

https: // stackoverflow .com / preguntas / 9334115 / magento-cambio-aduana-valor-opción-antes-adición-it-a-compra / 9344336 # 9344336

y más:

https: // stackoverflow .com / preguntas / 9412074 / magento-cotización en orden a productos objeto-atributo basado en el usuario-entrada / 9496266 # 9496266

Otros consejos

The appropiate event to add custom options on the fly is catalog_product_type_prepare_full_options, which is triggered just before the product with its custom options is converted to a quote item.

Should the own buyRequest data have effect on product attributes or options, an observer on the event catalog_product_type_prepare_{$processMode}_options is a good choice, where $processMode is the validation mode and can be „full“ or „lite“. The „full“ mode is used when a product is regularly added to the cart and validates if all required options are set and the whole configuration is valid. In the „lite“ mode only options contained in the request are validated, it is used when adding a product to the wishlist, but also possible when creating an order from the backend. To process the data in any case you can register the observer for both events. Should there be validation you should differentiate the events of course.

The events are triggered in Mage_Catalog_Model_Product_Type_Abstract::_prepareOptions() and the following parameters are available:

  • transport: Transport object for all custom options (but no other options, for example bundle options), so you can change them in the observer. transport->options is an array in the form option_id => option_value. Attention, transport itself is a stdClass object, not an instance of Varien_Object, as you might expect. So there are no getter and setter methods for transport->options.
  • buy_request: The buyRequest object, you can read it here and still modify it as well.
  • product: The product that will be converted to a quote item later on. Here you can manipulate attributes or add them dynamically. But you still need to consider them in the conversion process. The event used for this, sales_quote_product_add_after, is triggered later only.

Source: The Magento buyRequest Object – A Reference

So an observer might look like this:

public function addCustomOption(Varien_Event_Observer $observer)
{
    $transport = $observer->getTransport();
    if (this_item_should_be_free()) { // implement your condition here
        $transport->options['Free Gifts'] = 'Spend $50 and get gift product worth $9.99';
    }
}

You cannot set a price for this dynamically added custom option, but you can change the price of the quote item using a second observer for catalog_product_get_final_price like this:

public function adjustFinalPrice($observer) {

    $product = $observer->getProduct();
    // Set price to "0" if custom option "Free Gift" has been set
    if ($product->getCustomOption('Free Gift')) {
        $product->setFinalPrice(0);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top