1

I have created a custom module for wishlist, and I need to share email from wishlist which include multiple products in Magento2.2.5?

My Code:

<?php
namespace Emipro\GuestWishlist\Controller\Index;

use Magento\Framework\Mail\Template\TransportBuilder;


class Wishlistemailshare extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;

    protected $scopeConfig;

    protected $_registry;

    protected $request;


    public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory,\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Request\Http $request, TransportBuilder $transportBuilder)
    {

        $this->resultPageFactory = $resultPageFactory;
        $this->_scopeConfig = $scopeConfig;
        $this->request = $request;
        $this->transportBuilder = $transportBuilder;

        parent::__construct($context);
    }

    public function execute()
    {
        //GetProduct details start
       // print_r($_POST);
    //die('AAAAAAAAAAAAAAA');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        $messageManager = $objectManager->get('Magento\Framework\Message\ManagerInterface');
        $cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
        $getProductId = $cookieManager->getCookie('guestwishlistids');
        $product = explode(',', $getProductId);



        $array_product = array();
        $get_product_details = array();

        $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
        $curruncyFactory = $objectManager->create('Magento\Directory\Model\CurrencyFactory');
        $currencyCode = $storeManager->getStore()->getCurrentCurrencyCode();
        $currency = $curruncyFactory->create()->load($currencyCode);
        $currencySymbol = $currency->getCurrencySymbol();
        $cookieId = "";

        foreach ($product as $value) 
        {
            if (!empty($value)) {
        $display_button=1;
        $productId = $value;
        $productCollection = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

        // $imageBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
        // $productImage = $imageBlock->getImage($productCollection, 'category_page_list');

        $array_product['image'] = $productCollection->getImage();
        $array_product['product_name'] = $productCollection->getName();
        $array_product['sku_id'] = $productCollection->getSku();
        $array_product['get_urlkey'] = $productCollection->getUrlKey();
        $array_product['product_url']=$productCollection->getProductUrl();
        $array_product['price'] = $productCollection->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
        echo "<pre>";
        print_r($productCollection);

           }
           array_push($get_product_details,$array_product);
        }
        //die;
        //GetProduct details End

        $getPostVal = $this->getRequest()->getPost();

        $getFromName = isset($getPostVal["from_name"]) ? $getPostVal["from_name"] : '';
        $getFromEmail = isset($getPostVal["from_email"]) ? $getPostVal["from_email"] : '';
        $getToEmail = isset($getPostVal["email_sep"]) ? $getPostVal["email_sep"] : '';
        $messagee = isset($getPostVal["message"]) ? $getPostVal["message"] : '';

        $to_mail_Array = explode(',', $getToEmail);



        $html = '';

        $test_arr = array("test1", "test2", "test3");

        foreach($test_arr as $val) 
        {
            $html .= "<div class='item'>";
            $html .= "<p> Product Name: ".$val." </p>";
            $html .= "</div>";
        }

        $dfssdf = strip_tags($html);

        $requestData['product_content'] = $dfssdf;


        /* Email send to the shared users - Code Start */
        $requestData['product_details'] = $get_product_details;

        $requestData['mail_subj'] = "Amy Wishlist share";

        $requestData['hello_txt'] = "Hey";

        $requestData['mesg'] = "Take a look at my wishlist from Amy.";

        $requestData['message'] = $messagee;

        $postObject = new \Magento\Framework\DataObject();
        $postObject->setData($requestData);

        if(count($to_mail_Array) > 0)  
        {
            foreach($to_mail_Array as $val) 
            {
                if($val != '') 
                {
                    $transport = $this->transportBuilder
                        ->setTemplateIdentifier('wishlist_template')
                        ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
                        ->setTemplateVars(['data' => $postObject])
                        ->setFrom(['name' => $getFromName,'email' => $getFromEmail])
                        ->addTo($val)
                        ->getTransport();

                    $transport->sendMessage();
                }
            }
        }

        // $val = "ame@gmail.com";

        // $transport = $this->transportBuilder
        //     ->setTemplateIdentifier('wishlist_template')
        //     ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
        //     ->setTemplateVars(['data' => $postObject])
        //     ->setFrom(['name' => $getFromName,'email' => $getFromEmail])
        //     ->addTo($val)
        //     ->getTransport();

        // $transport->sendMessage();

        /* Email send to the shared users - Code Start */

        $this->_redirect('guestwishlist/index/view');
        return;

        // $resultPage = $this->resultPageFactory->create(); 
        // return  $resultPage;
    } 
}

I am just scratching my head from past 4days.

Any help would be appreciated.

Your Answer

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

Browse other questions tagged or ask your own question.