1

I’m supposed to move to the wishlist, all the items that i've got in the cart but straight after the order is complete. Any ideas?

Thanks!

Here is my code:

in /app/etc/modules/Eleva_ObserverFromCartToWishlist.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Eleva_ObserverFromCartToWishlist>
            <active>true</active>
            <codePool>local</codePool>
        </Eleva_ObserverFromCartToWishlist>
    </modules>
</config>

in /app/code/local/Eleva/ObserverFromCartToWishlist/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Eleva_ObserverFromCartToWishlist>
            <version>0.1.0</version>
        </Eleva_ObserverFromCartToWishlist>
    </modules>
    <global>
        <models>
            <eleva_observerfromcarttowishlist>
                <class>Eleva_ObserverFromCartToWishlist_Model</class>
            </eleva_observerfromcarttowishlist>
        </models>
        <events>
            <sales_order_place_after>
                <observers>
                    <Eleva_ObserverFromCartToWishlist_Model_Observer>
                        <type>singleton</type>
                        <class>Eleva_ObserverFromCartToWishlist_Model_Observer</class>
                        <method>addfromcarttoWishlistEvent</method>
                    </Eleva_ObserverFromCartToWishlist_Model_Observer>
                </observers>
            </sales_order_place_after>
        </events>
    </global>
</config>

in /app/code/local/Eleva/ObserverFromCartToWishlist/Model/observer.php

<?php

class Eleva_ObserverFromCartToWishlist_Model_Observer {

    public function addfromcarttoWishlistEvent(Varien_Event_Observer $observer) {
        $order = $observer->getOrder();
        $items = $order->getAllVisibleItems();

        //assuming the customer check out as a register user
        $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($order->getCustomerId(), true);

        foreach($items as $item){
           $product = Mage::getModel('catalog/product')->load($item->getProductId());
           //Todo - add the options
           $buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product
           $result = $wishlist->addNewItem($product, $buyRequest);
        } 

        $wishlist->save();

        $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
        fwrite($myfile, "test");
        fclose($myfile);

        Mage::log($order);
    }
}
  • Please update your question with the code you added as an answer, since it doesn't work – Renon Stewart Dec 10 '14 at 14:29
  • Also what type of product did you purchase, simple product with or without option, configureable or bundle? Did you check to make sure your observer was be executed? – Renon Stewart Dec 10 '14 at 14:31
  • I did purchase a simple product without any option. – Luca Ricci Dec 11 '14 at 11:59
  • Did you check to make sure your observer was be executed? – Renon Stewart Dec 11 '14 at 12:21
  • Yeah i'm sure that my observer was be executed. Where am i wrong? I updated the code above. – Luca Ricci Dec 11 '14 at 13:56
2

Take a look at How do I add to wishlist programatically? and magento sales_order_place_after observer

In your observer

<?php
class Feed_Sales_Model_Order_Observer
{

    public function addToWishlist($observer)
    {
        $order = $observer->getOrder();
        $items = $order->getAllVisibleItems();

        //assuming the customer check out as a register user
        $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($order->getCustomerId(), true);

        foreach($items as $item){
           $product = Mage::getModel('catalog/product')->load($item->getProductId());
           //Todo - add the options
           $buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product
           $result = $wishlist->addNewItem($product, $buyRequest);
        } 

        $wishlist->save();         
    }
}
?>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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