0

I am using a free extension to add the subscribe checkbox on checkout, the field is visible there but when I submitting shipping information the extension attribute is not included in the post. di.xml

<?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\Checkout\Model\ShippingInformationManagement">
        <plugin name="save-newsletter-subscribe-in-quote" 
            type="Ulmod\CheckoutNewsletterSubscription\Model\Plugin\Checkout\ShippingInformationManagement" sortOrder="10"/>
    </type>
    <type name="Magento\Checkout\Api\ShippingInformationManagementInterface">
        <plugin name="save-newsletter-subscribe-in-quote" 
            type="Ulmod\CheckoutNewsletterSubscription\Model\Plugin\Checkout\ShippingInformationManagement" sortOrder="20"/>
    </type>
    <type name="Ulmod\CheckoutNewsletterSubscription\Observer\OnSalesModelServiceQuoteSubmitSuccess">
        <arguments>
            <argument name="subscriber" xsi:type="object">UlmodCheckoutNewsletterSubscriptionModelSubscriber</argument>
        </arguments>
    </type>
</config>

ShippingInformationManagement.php

<?php
/**
 * Copyright © Ulmod. All rights reserved.
 * See LICENSE.txt for license details.
 */ 
 
namespace Ulmod\CheckoutNewsletterSubscription\Model\Plugin\Checkout;

use Magento\Quote\Model\QuoteRepository;
use Magento\Checkout\Model\ShippingInformationManagement as ShippingManagement;
use Ulmod\CheckoutNewsletterSubscription\Block\CheckoutSubscription;
use Ulmod\CheckoutNewsletterSubscription\Model\Config\Source\CheckoutSubscribe;
use Magento\Checkout\Api\Data\ShippingInformationInterface;

class ShippingInformationManagement
{
    /**
     * @var CheckoutSubscription
     */
    protected $checkoutSubscription;

    /**
     * @var QuoteRepository
     */
    protected $_quoteRepository;

    /**
     * @param QuoteRepository $quoteRepository
     * @param CheckoutSubscription $checkoutSubscription
     */
    public function __construct(
        QuoteRepository $quoteRepository,
        CheckoutSubscription $checkoutSubscription
    ) {
        $this->_quoteRepository = $quoteRepository;
        $this->checkoutSubscription = $checkoutSubscription;
    }

    /**
     * @param ShippingManagement $subject
     * @param $cartId
     * @param ShippingInformationInterface $addressInformation
     */
    public function beforeSaveAddressInformation(
        ShippingManagement $subject,
        $cartId,
        ShippingInformationInterface $addressInformation
    ) {
        $extensionEnabled = $this->checkoutSubscription->getConfigModule('enabled');        
        if ($extensionEnabled) {
            $newsletterSubscribe = 0;

            if (in_array(
                $this->checkoutSubscription->getConfigModule('checkout_subscribe'),
                [CheckoutSubscribe::FORCE_INVISIBLE, CheckoutSubscribe::FORCE]
            )) {
                $newsletterSubscribe = 1;
            } elseif (($extAttributes = $addressInformation->getExtensionAttributes())
                && $extAttributes->getNewsletterSubscribe()
            ) {
                $newsletterSubscribe = 1;
            }

            $quote = $this->_quoteRepository->getActive($cartId);
            
            $quote->setNewsletterSubscribe($newsletterSubscribe);
        }
    }
}

I have checked this code

$addressInformation->getExtensionAttributes());    
$extAttributes->getNewsletterSubscribe();

It's returning nothing. Any solution to resolve this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.