I'm new to Magento 2 and to this forum. I'm trying to add a newsletter subscription on the customer menu, just after the core one.
For that, i created a module "ACME_Newsletter"
I added a custom field to the customer_entity table via a db_schema :
<table name="customer_entity"> <column xsi:type="boolean" name="acme_nl_subscription" nullable="false" default="false" comment="Acme Newsletter Subscription" />
I created a layout that completes the existing one "customer_newsletter"
<body>
<referenceBlock name="customer_newsletter">
<container name="customer.form.newsletter.extra">
<block
class="ACME\Newsletter\Block\Subscription"
name="customer_form_newsletter_extra" as="acme_nl_subscription"
template="ACME_Newsletter::customer/form/subscription.phtml"
/>
</container>
</referenceBlock>
</body>
Here is my template :
<?php
/** @var \ACME\Newsletter\Block\Subscription $block */
?>
<?= $block->getChildHtml('customer.form.newsletter.extra') ?>
<div class="field choice">
<input type="checkbox" name="acme_nl_subscription" id="acme_nl_subscription" value="1"
title="<?= $block->escapeHtmlAttr(__('ACME\'s Newsletter Subscription')) ?>"
<?php if ($block->getCustomerSession()->getCustomer()->getAcmeNlSubscription()) : ?> checked="checked"<?php endif; ?>
class="checkbox">
<label for="acme_nl_subscription"
class="label"><span><?= $block->escapeHtml(__('ACME\'s Newsletter Subscription')) ?></span></label>
</div>
And my block :
<?php
namespace ACME\Newsletter\Block;
use Magento\Customer\Block\Newsletter;
class Subscription extends Newsletter
{
public function getCustomerSession()
{
return $this->customerSession;
}
}
With all that I manage to retrieve the value stored in database.
Now I try to modify that value and to store it in db, within an after plugin on the Magento\Newsletter\Controller\Manage\Save::execute() method.
I can't find the right way to write my method to persist this data. For the moment, my plugin looks like this :
<?php
namespace ACME\Newsletter\Plugin;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Newsletter\Controller\Manage\Save;
use Magento\Framework\App\RequestInterface;
use Magento\Customer\Model\ResourceModel\CustomerRepository as CustomerRepository;
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
use Magento\Customer\Model\Customer as CustomerModel;
class AfterSavePlugin
{
protected $customerSession;
protected $request;
protected $customerRepository;
protected $customerRepositoryInterface;
protected $customerResource;
protected $customerModel;
public function __construct(
Session $customerSession,
RequestInterface $request,
CustomerRepositoryInterface $customerRepositoryInterface,
CustomerRepository $customerRepository,
CustomerResource $customerResource,
CustomerModel $customerModel
)
{
$this->customerSession = $customerSession;
$this->request = $request;
$this->customerRepository = $customerRepository;
$this->customerRepositoryInterface = $customerRepositoryInterface;
$this->customerResource = $customerResource;
$this->customerModel = $customerModel;
}
public function afterExecute(Save $subject, $result)
{
$acmeNlSubscription = $this->request->getParam('acme_nl_subscription') ? (bool)$this->request->getParam('acme_nl_subscription') : 0;
$sessionCustomer = $this->customerSession->getCustomer();
$customerData = $sessionCustomer->getData();
$repositoryCustomer = $this->customerRepository->getById($sessionCustomer->getId());
// $modelCustomer->loadByEmail($sessionCustomer->getEmail());
$modelCustomer = $this->customerModel;
$modelCustomer->setData($customerData);
$resourceCustomer = $this->customerResource->loadByEmail($modelCustomer, $modelCustomer->getEmail());
$modelCustomer->save();
return $result;
}
}
Could a kind soul help me to take a step in my understanding of Adobe Commerce 2 (Magento 2) please?
What am I doing wrong? All comments are welcome, thank you!