0

Iam using Magento 2.3.5-p2. I add custom field in the cart and checkout page which is successfully showing there.

In app\code\Company\Module\etc\di.xml

<preference for="Magento\Quote\Api\Data\AddressInterface" type="Company\Module\Api\Quote\Data\AddressInterface"/>

In app\code\Company\Module\Api\Quote\Data\AddressInterface.php

<?php
namespace Company\Module\Api\Quote\Data;

use Magento\Quote\Api\Data\AddressInterface as MagentoDataAddressInterface;

interface AddressInterface extends MagentoDataAddressInterface
{
    const KEY_SUBDISTRICT = 'subdistrict_id';

    public function getSubdistrictId();

    public function setSubdistrictId($subdistrictId);
}

But while getting shipping estimate, it was error

Uncaught Error: Cannot instantiate interface Company\Module\Api\Quote\Data\AddressInterface in C:\wamp64\www\magento2-2\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php:50

I already

  • Delete /generated/code
  • Delete /static/frontend
  • php bin\magento cache:clean

1 Answer 1

0

You cannot override any interface in such way.

If you have to use extension attribute for this case.

extension_attributes.xml code look like:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
        <attribute code="subdistrict_id" type="int" />
    </extension_attributes>
</config>

Using extension attribute you can update and get data for your custom field.See https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html

1
  • If I remove the preference from di.xml and using extension_attributes.xml above, I got error Property "SubdistrictId" does not have accessor method "getSubdistrictId" in class "Magento\Quote\Api\Data\AddressInterface".
    – Wakanina
    Commented Jan 9, 2021 at 8:46

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.