1

I have a promotional popup on home page, asking to subscribe a newsletter. Also I want to save gender and Interested Categories of that User. How I can save these additional Information to 'newsletter_subscriber' table.

I have referred this link: Magento 2 : How to add additional fields to newsletter

So Fields are added to table and admin grid, but it doesn't save gender and category data to table.

Can anyone help me? Thanks in advance.

please check SS. enter image description here

enter image description here

Adding Some code here,

   public function aroundSubscribe($subject, \Closure $proceed, $email) {
    $result = $proceed($email);
    if ($this->request->isPost() && $this->request->getPost('gender')) {
        print_r($this->request->getPost('gender'));
        exit;
        $gender = $this->request->getPost('gender');
        $category = $this->request->getPost('category');

        $subject->setGender($gender);
        $subject->setInterestedCategory($category);


        try {
            $subject->save();
        }catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    return $result;
}
16
  • any Exception ??? Commented Jan 7, 2020 at 13:56
  • Can you please add entire code of you file? Are you getting values when you try to print "gender"? Commented Jan 7, 2020 at 15:20
  • @AbhishekPanchal no it doesn't give any exception or error. this is the main logic to save via plugin. let me know which code you want to check? Commented Jan 8, 2020 at 4:30
  • try to print your values in debug log and see what you are getting in debug.log file. Commented Jan 8, 2020 at 4:32
  • ok.. will check and let you know Commented Jan 8, 2020 at 4:36

1 Answer 1

0

finally, I found the answer to add additional data to news letter.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Newsletter\Model\Subscriber">
    <plugin name="vendor_module_newsletter_model_subscriber" type="vendor\module\Plugin\Newsletter\Subscriber" sortOrder="1"/>
</type>

and a plugin file

public function aroundSubscribe(\Magento\Newsletter\Model\Subscriber $subject, \Closure $proceed, $email) {

    if ($this->request->isPost() && $this->request->getPost('gender')) {
        $gender = $this->request->getPost('gender');
        $category = $this->request->getPost('category');

        $subject->setGender($gender);
        $subject->setInterestedCategory($category);

        $result = $proceed($email);

        try {
            $subject->save();

        }catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    return $result;
}

mistake was $result = $proceed($email); line has to write after saving additional data.

hope will work !!

1
  • 1
    You should write $result = $proceed($email); line code outside if condition. If condition will not true then, it will stop execution and return error. Not sure. But, as i know it will create issue. Commented Jan 10, 2020 at 5:47

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.