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.