Skip to main content
code edit
Source Link
sh4
  • 123
  • 4
                    $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
                    
                    foreach($products as $product) {
                        $product = Mage::getModel('catalog/product')->load($product->getId());
                        $buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product
                        $result = $wishlist->addNewItem($product, $buyRequest);
                    }

                    Mage::helper('wishlist')->calculate();

                    $message = "";
                    $emailModel = Mage::getModel('core/email_template');
                    $sharingCode = $wishlist->getSharingCode();
                    //this var returning empty
                    $wishlistBlock = Mage::getSingleton('core/layout')->createBlock('wishlist/share_email_items')->toHtml();

                    $emailModel->sendTransactional(
                        Mage::getStoreConfig('wishlist/email/email_template'),
                        Mage::getStoreConfig('wishlist/email/email_identity'),
                        $email,
                        null,
                        array(
                            'customer'       => $customer,
                            'salable'        => $wishlist->isSalable() ? 'yes' : '',
                            'items'          => $wishlistBlock,
                            'addAllLink'     => Mage::getUrl('*/shared/allcart', array('code' => $sharingCode)),
                            'viewOnSiteLink' => Mage::getUrl('*/shared/index', array('code' => $sharingCode)),
                            'message'        => $message
                        )
                    );

                    $wishlist->setShared(1);
                    $wishlist->save();

                    Mage::dispatchEvent('wishlist_share', array('wishlist' => $wishlist));
                    Mage::getSingleton('adminhtml/session')->addSuccess(
                        $this->__('Your Wishlist has been shared.')
                    );
                    $emailModel = Mage::getModel('core/email_template');
                    $sharingCode = $wishlist->getSharingCode();
                    //this var returning empty
                    $wishlistBlock = Mage::getSingleton('core/layout')->createBlock('wishlist/share_email_items')->toHtml();

                    $emailModel->sendTransactional(
                        Mage::getStoreConfig('wishlist/email/email_template'),
                        Mage::getStoreConfig('wishlist/email/email_identity'),
                        $email,
                        null,
                        array(
                            'customer'       => $customer,
                            'salable'        => $wishlist->isSalable() ? 'yes' : '',
                            'items'          => $wishlistBlock,
                            'addAllLink'     => Mage::getUrl('*/shared/allcart', array('code' => $sharingCode)),
                            'viewOnSiteLink' => Mage::getUrl('*/shared/index', array('code' => $sharingCode)),
                            'message'        => $message
                        )
                    );

                    $wishlist->setShared(1);
                    $wishlist->save();

                    Mage::dispatchEvent('wishlist_share', array('wishlist' => $wishlist));
                    Mage::getSingleton('adminhtml/session')->addSuccess(
                        $this->__('Your Wishlist has been shared.')
                    );
                    $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
                    
                    foreach($products as $product) {
                        $product = Mage::getModel('catalog/product')->load($product->getId());
                        $buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product
                        $result = $wishlist->addNewItem($product, $buyRequest);
                    }

                    Mage::helper('wishlist')->calculate();

                    $message = "";
                    $emailModel = Mage::getModel('core/email_template');
                    $sharingCode = $wishlist->getSharingCode();
                    //this var returning empty
                    $wishlistBlock = Mage::getSingleton('core/layout')->createBlock('wishlist/share_email_items')->toHtml();

                    $emailModel->sendTransactional(
                        Mage::getStoreConfig('wishlist/email/email_template'),
                        Mage::getStoreConfig('wishlist/email/email_identity'),
                        $email,
                        null,
                        array(
                            'customer'       => $customer,
                            'salable'        => $wishlist->isSalable() ? 'yes' : '',
                            'items'          => $wishlistBlock,
                            'addAllLink'     => Mage::getUrl('*/shared/allcart', array('code' => $sharingCode)),
                            'viewOnSiteLink' => Mage::getUrl('*/shared/index', array('code' => $sharingCode)),
                            'message'        => $message
                        )
                    );

                    $wishlist->setShared(1);
                    $wishlist->save();

                    Mage::dispatchEvent('wishlist_share', array('wishlist' => $wishlist));
                    Mage::getSingleton('adminhtml/session')->addSuccess(
                        $this->__('Your Wishlist has been shared.')
                    );
Source Link
sh4
  • 123
  • 4

Share programmatically created wishlist

I have a custom admin module where I want to programatically create and share a wishlist.

I managed to create it programatically and to send the email (it arrives ok), but the "$wishlistBlock" and therefore, the email is empty.

I'm not such advanced in Magento yet, do I have to rewrite the model/modelresource/block or something like to get it working in my custom module? Tried a lot of different things, none worked.

This is the offending piece of code:

                    $emailModel = Mage::getModel('core/email_template');
                    $sharingCode = $wishlist->getSharingCode();
                    //this var returning empty
                    $wishlistBlock = Mage::getSingleton('core/layout')->createBlock('wishlist/share_email_items')->toHtml();

                    $emailModel->sendTransactional(
                        Mage::getStoreConfig('wishlist/email/email_template'),
                        Mage::getStoreConfig('wishlist/email/email_identity'),
                        $email,
                        null,
                        array(
                            'customer'       => $customer,
                            'salable'        => $wishlist->isSalable() ? 'yes' : '',
                            'items'          => $wishlistBlock,
                            'addAllLink'     => Mage::getUrl('*/shared/allcart', array('code' => $sharingCode)),
                            'viewOnSiteLink' => Mage::getUrl('*/shared/index', array('code' => $sharingCode)),
                            'message'        => $message
                        )
                    );

                    $wishlist->setShared(1);
                    $wishlist->save();

                    Mage::dispatchEvent('wishlist_share', array('wishlist' => $wishlist));
                    Mage::getSingleton('adminhtml/session')->addSuccess(
                        $this->__('Your Wishlist has been shared.')
                    );

Thanks in advance.