微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未捕获的错误:在 magento 2

如何解决未捕获的错误:在 magento 2

我尝试在结帐页面的运输和计费部分添加自定义字段(文本框)。 我已经创建了这样的扩展属性

<?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\Customer\Api\Data\AddressInterface">
        <attribute code="countrycode" type="string"/>
    </extension_attributes>
</config>

下面的代码是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="Checkoutattribute_Plugin_Magento_Quote_Model_BillingAddressManagement" sortOrder="10" type="vendor\extension\Plugin\Magento\Quote\Model\BillingAddressManagement"/>
    </type>
    <type name="Magento\Quote\Model\ShippingAddressManagement">
        <plugin disabled="false" name="Checkoutattribute_Plugin_Magento_Quote_Model_ShippingAddressManagement" sortOrder="10" type="vendor\extension\Plugin\Magento\Quote\Model\ShippingAddressManagement"/>
    </type>
</config>


<?PHP
    
    
 namespace vendor\extension\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->setCountrycode($extAttributes->getCountrycode());
            } catch (\Exception $e) {
                $this->logger->critical($e->getMessage());
            }

        }

    }
}

但它显示这样的错误

致命错误:未捕获的错误调用未定义的方法 Magento\Quote\Api\Data\AddressExtension::getCountrycode() 中 app/code/vendor/extension/Plugin/Magento/Quote/Model/ShippingAddressManagement.PHP:30

我已尝试调试此问题,但问题仍未解决,请帮助我找出问题。

来自这个例子的参考:https://magento.stackexchange.com/questions/194705/magento-2-add-extra-text-field-in-checkout-billing-address-and-save-it-in-orde

谢谢。

解决方法

问题是 extension_attribute.xml 文件

<extension_attributes for=""Magento\Customer\Api\Data\AddressInterface"">
            <attribute code=""countrycode"" type=""string""/>
        </extension_attributes>

上面的代码应该变成

<extension_attributes for=""Magento\Quote\Api\Data\AddressInterface"">
            <attribute code=""countrycode"" type=""string""/>
        </extension_attributes>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。