6

I'm trying to add an extension attribute to \Magento\Quote\Api\Data\TotalsInterface. My understanting is, that magento should generate getter/setter for the new attribute, but that is not happening. (i tried deleting var/generation var/cache multiple times)

extension_attributes.xml

<?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\TotalsInterface">        
        <attribute code="custom_attribute" type="string"/>
    </extension_attributes>
</config>

Is my understanding correct, or is this just not possible?

The interface is instantiated using factory in Magento\Quote\Model\Cart\CartTotalRepository. Could that have an effect?

Any help would be greatly appreciated.

1 Answer 1

5

The getters and setters are not added to the TotalsInterface itself.

Instead they are generated in Magento\Quote\Api\Data\TotalsExtensionInterface (which you should find in var/generation) and you use it like this (for custom_attribute):

$totals->getExtensionAttributes()->getCustomAttribute();
5
  • That is exactly what i would expect to happen. However TotalsExtensionInterface.php is never generated.
    – Langley
    Jun 3, 2016 at 13:48
  • Not at all, not even empty? Are you running in developer mode? Are other classes generated? Jun 3, 2016 at 13:58
  • I'm running developer mode. The file is not there but others are (TotalsInterfaceFactory, TotalSegmentExtension etc). getExtensionAttributes() returns null so i assume the attribute is not registered? I have no idea why that is.
    – Langley
    Jun 3, 2016 at 14:01
  • Weird, it should be there. I don't fully understand when these are generated, maybe magento.stackexchange.com/questions/93366/… is helpful in that case Jun 3, 2016 at 14:03
  • 1
    Ok thanks for your time, the information provided is correct. It is working now, but i have no idea what changed. What i did: 1, Reinstalled magento and update 2.0.5 to 2.0.7 2, ran setup:di:compile - again my extension interface is not generated 3, reformatted my extension_attributes.xml (with ide and also removed the line break) 4, ran setup:di:compile - the interface gets generated T_T Number of generated files in /Magento/Quote/Api/Data is much greater than before. (not sure if that's caused by version update)
    – Langley
    Jun 3, 2016 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.