2

I have issue in Override the Newsletter Queue Model to sent Newsletter Based on Specific Customer group. But, It throw

PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Newsletter\Model\Queue::__construct() must be an instance of Magento\Framework\Registry, array given, called in /var/www/public/app/code/vendor/Module/Model/Queue.php on line 26 and defined in /var/www/public/vendor/magento/module-newsletter/Model/Queue.php:125

what i did :

create di.xml:

add below code:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> 
        <preference for="Magento\Newsletter\Model\Queue" type="vendor\Module\Model\Queue" />
    </config>

Create Vendor/Module/Model/Queue:

<?php

namespace vendor\Module\Model;

use Magento\Framework\App\TemplateTypesInterface;



class Queue extends \Magento\Newsletter\Model\Queue
{

    protected $_customergroup;
    protected $_customergroupdata;


    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Vendor\Module\Block\Testing $customergroup,
        \Magento\Customer\Model\ResourceModel\Group\Collection $customergroupdata,
        array $data = []
    )
    {
        parent::__construct(
            $context,
            $data
        );
        $this->_customergroup = $customergroup;
        $this->_customergroupdata = $customergroupdata;
    }

    public function sendPerSubscriber($count = 20)
    {
        if ($this->getQueueStatus() != self::STATUS_SENDING &&
            ($this->getQueueStatus() != self::STATUS_NEVER &&
                $this->getQueueStartAt())
        ) {
            return $this;
        }

        if (!$this->_subscribersCollection->getQueueJoinedFlag()) {
            $this->_subscribersCollection->useQueue($this);
        }

        if ($this->_subscribersCollection->getSize() == 0) {
            $this->_finishQueue();
            return $this;
        }

        $collection = $this->_subscribersCollection->useOnlyUnsent()->showCustomerInfo()->setPageSize(
            $count
        )->setCurPage(
            1
        )->load();

        $this->_transportBuilder->setTemplateData(
            [
                'template_subject' => $this->getNewsletterSubject(),
                'template_text' => $this->getNewsletterText(),
                'template_styles' => $this->getNewsletterStyles(),
                'template_filter' => $this->_templateFilter,
                'template_type' => self::TYPE_HTML,
            ]
        );

        /** @var \Magento\Newsletter\Model\Subscriber $item */
        foreach ($collection->getItems() as $item) {

            //MY customized code:

            //  var_dump($this->_customergroup->getCustomersbyGroup(4));

            $groupOptions = $this->_customergroupdata->toOptionArray();

            $groups = array();
            //$groupid=array();

            foreach ($groupOptions as $group) {
                array_push($groups, $group["label"]);

            }
            foreach ($groups as $groupname) {
                if ($groupname = "Daily customers") {

                    $groupid = $groupOptions["value"];

                    $transport = $this->_transportBuilder->setTemplateOptions(
                        ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $item->getStoreId()]
                    )->setTemplateVars(
                        ['subscriber' => $item]
                    )->setFrom(
                        ['name' => $this->getNewsletterSenderName(), 'email' => $this->getNewsletterSenderEmail()]
                    )->addTo(
                    //$item->getSubscriberEmail(),
                    //$item->getSubscriberFullName()
                        $this->_customergroup->getCustomersbyGroup($groupid)
                    )->getTransport();

                    try {
                        $transport->sendMessage();
                    } catch (\Magento\Framework\Exception\MailException $e) {
                        /** @var \Magento\Newsletter\Model\Problem $problem */
                        $problem = $this->_problemFactory->create();
                        $problem->addSubscriberData($item);
                        $problem->addQueueData($this);
                        $problem->addErrorData($e);
                        $problem->save();
                    }
                    $item->received($this);
                }

                if (count($collection->getItems()) < $count - 1 || count($collection->getItems()) == 0) {
                    $this->_finishQueue();
                }
                return parent::sendPerSubscriber();
            }
        }

    }
}

And I add One Block in contractor for dependency injection \Vendor\Module\Block\Testing $customergroup i am not write this path to di.xml. I am directly inserted in this code.

is there any probelm?

Edit:

PHP Fatal error: Uncaught Error: Cannot instantiate abstract class Magento\Framework\Model\ResourceModel\AbstractResource in /var/www/public/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:73 Suggest Me What I miss in my code, how to Solve this problem.

2

It's caused by the fact that your model constructor does not match your original model constructor.

So this is the constructor from Magento\Newsletter\Model\Queue :

public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Newsletter\Model\Template\Filter $templateFilter,
    \Magento\Framework\Stdlib\DateTime\DateTime $date,
    \Magento\Newsletter\Model\TemplateFactory $templateFactory,
    \Magento\Newsletter\Model\ProblemFactory $problemFactory,
    \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
    \Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
) {
    parent::__construct(
        $context,
        $registry,
        $resource,
        $resourceCollection,
        $data
    );
    $this->_templateFilter = $templateFilter;
    $this->_date = $date;
    $this->_templateFactory = $templateFactory;
    $this->_problemFactory = $problemFactory;
    $this->_subscribersCollection = $subscriberCollectionFactory->create();
    $this->_transportBuilder = $transportBuilder;
}

To fix your constructor it needs to be:

public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Newsletter\Model\Template\Filter $templateFilter,
    \Magento\Framework\Stdlib\DateTime\DateTime $date,
    \Magento\Newsletter\Model\TemplateFactory $templateFactory,
    \Magento\Newsletter\Model\ProblemFactory $problemFactory,
    \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
    \Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    \Vendor\Module\Block\Testing $customergroup,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customergroupdata,
    array $data = []
)
{
    $this->_customergroup = $customergroup;
    $this->_customergroupdata = $customergroupdata;
    parent::__construct(
        $context,
        $registry,
        $templateFilter,
        $date,
        $templateFactory,
        $problemFactory,
        $subscriberCollectionFactory,
        $transportBuilder,
        $resource,
        $resourceCollection,
        $data
    );
}
  • I add your suggest then throw error, check my edit question. – Rajkumar .E Oct 27 '16 at 10:32
  • @Rajkumar.E which Magento version are you running ? – Raphael at Digital Pianism Oct 27 '16 at 10:33
  • I am using Magento 2.0.8 – Rajkumar .E Oct 27 '16 at 10:35
  • 1
    you should write those function in a helper if those functions are also used in a model, not in a block – Raphael at Digital Pianism Oct 27 '16 at 10:52
  • 1
    Try moving the dependencies that you need above the two abstract classes in the constructor. – Paul Oct 27 '16 at 11:01
1

As Per @Paul Suggestion, I move my dependencies above Abstatct class

This is My Dependencies:

\Vendor\Module\Block\Testing $customergroup,

\Magento\Customer\Model\ResourceModel\Group\Collection $customergroupdata,

Move to Above Abstack class:

  \vendort\module\Block\Testing $customergroup,
   \Magento\Customer\Model\ResourceModel\Group\Collection $customergroupdata,
  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,

And Also Order the Paramenter:

      parent::__construct(
                $context,
                $registry,
                $templateFilter,
                $date,
                $templateFactory,
                $problemFactory,
                $subscriberCollectionFactory,
                $transportBuilder,
                $resource,
                $resourceCollection,
                $data
            );

My Final Code:

 public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Newsletter\Model\Template\Filter $templateFilter,
    \Magento\Framework\Stdlib\DateTime\DateTime $date,
    \Magento\Newsletter\Model\TemplateFactory $templateFactory,
    \Magento\Newsletter\Model\ProblemFactory $problemFactory,
    \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
    \Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
    \vendor\Module\Block\Testing $customergroup,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customergroupdata,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
)
{
    $this->_templateFilter = $templateFilter;
    $this->_date = $date;
    $this->_templateFactory = $templateFactory;
    $this->_problemFactory = $problemFactory;
    $this->_subscribersCollection = $subscriberCollectionFactory->create();
    $this->_transportBuilder = $transportBuilder;
    $this->_customergroup =$customergroup;
    $this->_customergroupdata=$customergroupdata;
    parent::__construct(
        $context,
        $registry,
        $templateFilter,
        $date,
        $templateFactory,
        $problemFactory,
        $subscriberCollectionFactory,
        $transportBuilder,
        $resource,
        $resourceCollection,
        $data
    );
}

Its Works for Me, I think magento2 Need Some Order wise Objects paramenter.

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.