Domanda

I am absolutely new in magento. I have made new input called 'name' under email input in

app/design/frontend/NewVendor/NewTheme/Magento_Newsletter/templates/subscribe.phtml

<div class="block newsletter">
<div class="title"><strong><?= $block->escapeHtml(__('Newsletter')) ?></strong></div>
<div class="content">
    <form class="form subscribe"
        novalidate
        action="<?= $block->escapeUrl($block->getFormActionUrl()) ?>"
        method="post"
        data-mage-init='{"validation": {"errorClass": "mage-error"}}'
        id="newsletter-validate-detail">
        <div class="field newsletter">
            <label class="label" for="newsletter"><span><?= $block->escapeHtml(__('Sign Up for Our Newsletter:')) ?></span></label>
            <div class="control">
                <input name="email" type="email" id="newsletter"
                       placeholder="<?= $block->escapeHtml(__('Enter your email address')) ?>"
                       data-mage-init='{"mage/trim-input":{}}'
                       data-validate="{required:true, 'validate-email':true}"/>
                <input name="name" placeholder="Name"/>
            </div>

        </div>
        <div class="actions">
            <button class="action subscribe primary" title="<?= $block->escapeHtmlAttr(__('Subscribe')) ?>" type="submit">
                <span><?= $block->escapeHtml(__('Subscribe')) ?></span>
            </button>
        </div>
    </form>
</div>

I have made a new column in newsletter_subscriber table in

app/code/Mag/Newsletter/Setup/UpgradeSchema.php

class UpgradeSchema implements UpgradeSchemaInterface

{

public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    if (version_compare($context->getVersion(), '0.0.2', '<')) {
        $setup->getConnection()->addColumn(
            $setup->getTable('newsletter_subscriber'),
            'name',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 50,
                'nullable' => false,
                'default' => '',
                'comment' => 'Name'
            ]
        );
    }
    $setup->endSetup();
}

}

And this is my controller in

app/code/Mag/Newsletter/Controller/Subscriber/NewAction.php

<?php
namespace Magebit\Newsletter\Controller\Subscriber;

class NewAction extends 
\Magento\Newsletter\Controller\Subscriber\NewAction
{
    public function execute() {
        $name = $this->getRequest()->getPost();
        var_dump($name);exit;
    }
}

For now controller var_dump's input value.

What I want to achieve is to save input value into "name" column in 'newsletter_subscriber' table.

Can't get it right. What should I do next?

È stato utile?

Soluzione

If you want to implement the logic in your controller, you have to copy the execute() method from the original, inherited controller and load the subscriber object after it was created, add your attribute and save the object. For this you have to add the following lines right after the subscribe() method has been called:

$status = (int) $this->_subscriberFactory->create()->subscribe($email);
//your code starts here...
$newSubscriber = $this->_subscriberFactory->create()->loadByEmail($email);
$newSubscriber->setName($name);
$newSubscriber->save();

I hope that helps. Anyway there are other possibilities to do this, for example creating a new method subscribe($email, $name) in the subscriber object and call that from your controller. With that approach you don't need to load the subscriber in the controller after creation, but you have to overwrite the subscriber model.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top