1

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:

                    $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.')
                    );

Thanks in advance.

7
  • Did you add items to your wishlist ? Could try adding a Mage::log call under the wishlist/email/items.phtml template to ensure the block is getting rendered ? Commented Jan 14, 2016 at 12:05
  • Sorry, I just edited my post to add that code. It doesn't appear to being rendered. What I am missing? Thanks.
    – sh4
    Commented Jan 14, 2016 at 12:19
  • It looks that since the controller is in adminhtml it's trying to render the block inside the adminhtml folder when executing this line: "$wishlistBlock = Mage::getSingleton('core/layout')->createBlock('wishlist/share_email_items')->toHtml();" Throwing the following error: "CRIT (2): Not valid template file:adminhtml/base/default/template/wishlist/email/items.phtml" any advice to replace that line?
    – sh4
    Commented Jan 14, 2016 at 12:28
  • Try creating the block first. Then do $block->setData('area','frontend')->toHtml(); Also ensure the Mage_Wishlist output is not disabled in the backend Commented Jan 14, 2016 at 12:40
  • Cool Digital Pianism, now renders the template!! Thank you! The products are missing though, do I have to pass the items to the block in any way? $this->getWishlistItems() <--- empty.
    – sh4
    Commented Jan 14, 2016 at 12:59

1 Answer 1

1

Ok so as this is done from an admin controller the first thing to do is to change the area before rendering the block so the frontend template is getting used.

To do so:

$block = Mage::getSingleton('core/layout')->createBlock('wishlist/share_email_items');
$block->setData('area','frontend')->toHtml();

Second part, which is minor but for some reasons in sh4 code, the wishlist object was not registered, so we had to register it manually by adding the following line before rendering the block:

Mage::register('wishlist',$wishlist);
1
  • so this is who is spamming us! :)
    – ben.incani
    Commented Oct 17, 2017 at 4:55

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.