cancel
Showing results for 
Search instead for 
Did you mean: 

custom attribute address

SOLVED

custom attribute address

hello,

 

following this:

https://codeblog.experius.nl/magento-2-add-extra-billing-or-shipping-field-to-the-checkout/

I am trying to add a custom attribute to customer address. Inside:

/app/code/Hoop/Taxcode/Plugin/Magento/Quote/Model

I have a file named ShippingAddressManagement.php, with this code:

<?php
namespace Hoop\Taxcode\Plugin\Magento\Quote\Model;

class ShippingAddressManagement {
    protected $logger;
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }
    public function beforeAssign(
        \Magento\Quote\Model\ShippingAddressManagement $subject,
        $cartId,
        \Magento\Quote\Api\Data\AddressInterface $address
    ) {
        $extAttributes = $address->getExtensionAttributes();
        if (!empty($extAttributes)) {
            try {
                $address->setTaxcode($address->getTaxcode());
            } catch (\Exception $e) {
                $this->logger->critical($e->getMessage());
            }
        }
    }
}

but when I try to compile I get this:

[InvalidArgumentException]
  Plugin class Hoop_Taxcode\Plugin\Magento\Quote\Model\ShippingAddressManagement doesn't exist

what am I doing wrong?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: custom attribute address

Just replace your plugin path with below, 

Hoop_Taxcode with  Hoop\Taxcode

 

type="Hoop_Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement" replace withtype="Hoop\Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement"

And shippingAddressmanagement with below way,

 

type="Hoop_Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement" 
replace with
type="Hoop\Taxcode\Plugin\Magento\Quote\Model\ShippingAddressManagement"

 

Remove var/generation folder and clear cache.

Thanks. 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

7 REPLIES 7

Re: custom attribute address

@agiorgini,

 

Can you show your di.xml file code?

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: custom attribute address

Hi,

this is my etc/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\Quote\Model\BillingAddressManagement">
        <plugin disabled="false" name="Hoop_Taxcode_Plugin_Magento_Quote_Model_BillingAddressManagement" sortOrder="10" type="Hoop_Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement"/>
    </type>
    <type name="Magento\Quote\Model\ShippingAddressManagement">
        <plugin disabled="false" name="Hoop_Taxcode_Plugin_Magento_Quote_Model_ShippingAddressManagement" sortOrder="10" type="Hoop_Taxcode\Plugin\Magento\Quote\Model\ShippingAddressManagement"/>
    </type>
</config>

and this is my etc/frontend/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\Block\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="hoop_extra_checkout_address_fields_layoutprocessor" xsi:type="object">Hoop\Taxcode\Block\Checkout\LayoutProcessor</item>
            </argument>
        </arguments>
    </type>
</config>

thanks!

Re: custom attribute address

Just replace your plugin path with below, 

Hoop_Taxcode with  Hoop\Taxcode

 

type="Hoop_Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement" replace withtype="Hoop\Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement"

And shippingAddressmanagement with below way,

 

type="Hoop_Taxcode\Plugin\Magento\Quote\Model\BillingAddressManagement" 
replace with
type="Hoop\Taxcode\Plugin\Magento\Quote\Model\ShippingAddressManagement"

 

Remove var/generation folder and clear cache.

Thanks. 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: custom attribute address

Hey Rakesh,

that worekd, thanks. But now when I try to order, I get this exception:

Message: Property "Taxcode" does not have corresponding setter in class "Magento\Quote\Api\Data\AddressExtensionInterface". in /var/www/coronelli/vendor/magento/framework/Webapi/ErrorProcessor.php:195

and in fact, inside var/generation/Magento/Quote/Api/Data/AddressExtension.php and var/generation/Magento/Quote/Api/Data/AddressExtensionInterface.php I expected to have getters and setters, instead I see for example:

<?php
namespace Magento\Quote\Api\Data;

/**
 * Extension class for @see \Magento\Quote\Api\Data\AddressInterface
 */
class AddressExtension extends \Magento\Framework\Api\AbstractSimpleObject implements \Magento\Quote\Api\Data\AddressExtensionInterface
{
}

what am I missing?

thanks a lot

Re: custom attribute address

Why you have faced this type of error i dont know. You can create new post with all data which error you have faced.

Based on that any user will give reply. In this post topic is different so better to ask new question for your query.

You can close this question by approve as answer.
Thanks.
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: custom attribute address

Hi @agiorgini , Ini extension_attributes.xml, it should be Magento\Quote\Api\Data\AddressInterface

Re: custom attribute address

could you share your complete implementation? please. im trying to follow that tutorial, but i have multiple errors Smiley Sad

 

thanks in advance