Magento 2 - Save Extension Attribute afterSave() returns voidHow do the extension attributes work in Magento 2?Magento 2.1.3 : How can I add extension attributes to a product attribute?Adding extension attributes to Order API EndpointHow to save Customer Extension attribute in Magento 2/2.1Magento 2 extension attributesShipping method returns zero amountAdd configure product in Cart using Magento 2 API facing an issueMagento2 - New customer address attribute won't save to databaseShould MagentoEavModelAttributeRepository save 'used_in_forms' attribute value?Getting error on attributeResource save

Do I need to be legally qualified to install a Hive smart thermostat?

If a creature is blocking and it has vigilance does it still tap?

Is it possible to spoof an IP address to an exact number?

How to travel between two stationary worlds in the least amount of time? (time dilation)

Is there any connection between "Whispers of the heart" and "The cat returns"?

Upload csv into QGIS

Why would a propellor have blades of different lengths?

Can you use a reaction to affect initiative rolls?

What is a "tittering order"?

Magento 2: I am not aware about magneto optimization. Can you please share the steps for this?

Can you move between the attacks of a Twinned Booming Blade?

How can solar sailed ships be protected from space debris?

Should I warn my boss I might take sick leave

Should I cheat if the majority does it?

what is the meaning of "stock" dilution on the Massive Dev Chart Website?

Isn't "Dave's protocol" good if only the database, and not the code, is leaked?

What instances can be solved today by modern solvers (pure LP)?

When should we use dependency injection (C#)

Who are the police in Hong Kong?

Which high-degree derivatives play an essential role?

Finding integer database columns that may have their data type changed to reduce size

Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?

Story about two rival crews terraforming a planet

Has there ever been a cold war other than between the U.S. and the U.S.S.R.?



Magento 2 - Save Extension Attribute afterSave() returns void


How do the extension attributes work in Magento 2?Magento 2.1.3 : How can I add extension attributes to a product attribute?Adding extension attributes to Order API EndpointHow to save Customer Extension attribute in Magento 2/2.1Magento 2 extension attributesShipping method returns zero amountAdd configure product in Cart using Magento 2 API facing an issueMagento2 - New customer address attribute won't save to databaseShould MagentoEavModelAttributeRepository save 'used_in_forms' attribute value?Getting error on attributeResource save






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I've been looking into extension attributes from various articles and tutorials:



https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/extension_attributes/adding-attributes.html



Easy to explain, but the one thing that came up for me is adding an extension attribute to the CartInterface model. Basically, if I want to add a plugin for the save() repository model it does not return anything (void). How would I go about retrieving the recently saved Quote/Cart going by the above two methods. Right now I just have a method that puts data into the extension attribute, but the data is not persistent elsewhere.



 $quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);

$this->quoteRepository->save($quote);









share|improve this question






















  • Where the new data are saved in DB? It have its own model (new table) or the data are saved in the same table "quote"? After I know this, I will can help you better. Here interactiv4.com/e-commerce-en/extension-attributes-codehacks-i4/… you can see a explication about this.

    – raumatbel
    Jun 24 '18 at 16:58












  • @raumatbel This will be saved in the quote table.

    – Jeremy Trpka
    Jun 25 '18 at 11:34

















0















I've been looking into extension attributes from various articles and tutorials:



https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/extension_attributes/adding-attributes.html



Easy to explain, but the one thing that came up for me is adding an extension attribute to the CartInterface model. Basically, if I want to add a plugin for the save() repository model it does not return anything (void). How would I go about retrieving the recently saved Quote/Cart going by the above two methods. Right now I just have a method that puts data into the extension attribute, but the data is not persistent elsewhere.



 $quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);

$this->quoteRepository->save($quote);









share|improve this question






















  • Where the new data are saved in DB? It have its own model (new table) or the data are saved in the same table "quote"? After I know this, I will can help you better. Here interactiv4.com/e-commerce-en/extension-attributes-codehacks-i4/… you can see a explication about this.

    – raumatbel
    Jun 24 '18 at 16:58












  • @raumatbel This will be saved in the quote table.

    – Jeremy Trpka
    Jun 25 '18 at 11:34













0












0








0








I've been looking into extension attributes from various articles and tutorials:



https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/extension_attributes/adding-attributes.html



Easy to explain, but the one thing that came up for me is adding an extension attribute to the CartInterface model. Basically, if I want to add a plugin for the save() repository model it does not return anything (void). How would I go about retrieving the recently saved Quote/Cart going by the above two methods. Right now I just have a method that puts data into the extension attribute, but the data is not persistent elsewhere.



 $quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);

$this->quoteRepository->save($quote);









share|improve this question














I've been looking into extension attributes from various articles and tutorials:



https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/extension_attributes/adding-attributes.html



Easy to explain, but the one thing that came up for me is adding an extension attribute to the CartInterface model. Basically, if I want to add a plugin for the save() repository model it does not return anything (void). How would I go about retrieving the recently saved Quote/Cart going by the above two methods. Right now I just have a method that puts data into the extension attribute, but the data is not persistent elsewhere.



 $quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);

$this->quoteRepository->save($quote);






magento2 extension-attributes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 22 '18 at 12:44









Jeremy TrpkaJeremy Trpka

4612 bronze badges




4612 bronze badges












  • Where the new data are saved in DB? It have its own model (new table) or the data are saved in the same table "quote"? After I know this, I will can help you better. Here interactiv4.com/e-commerce-en/extension-attributes-codehacks-i4/… you can see a explication about this.

    – raumatbel
    Jun 24 '18 at 16:58












  • @raumatbel This will be saved in the quote table.

    – Jeremy Trpka
    Jun 25 '18 at 11:34

















  • Where the new data are saved in DB? It have its own model (new table) or the data are saved in the same table "quote"? After I know this, I will can help you better. Here interactiv4.com/e-commerce-en/extension-attributes-codehacks-i4/… you can see a explication about this.

    – raumatbel
    Jun 24 '18 at 16:58












  • @raumatbel This will be saved in the quote table.

    – Jeremy Trpka
    Jun 25 '18 at 11:34
















Where the new data are saved in DB? It have its own model (new table) or the data are saved in the same table "quote"? After I know this, I will can help you better. Here interactiv4.com/e-commerce-en/extension-attributes-codehacks-i4/… you can see a explication about this.

– raumatbel
Jun 24 '18 at 16:58






Where the new data are saved in DB? It have its own model (new table) or the data are saved in the same table "quote"? After I know this, I will can help you better. Here interactiv4.com/e-commerce-en/extension-attributes-codehacks-i4/… you can see a explication about this.

– raumatbel
Jun 24 '18 at 16:58














@raumatbel This will be saved in the quote table.

– Jeremy Trpka
Jun 25 '18 at 11:34





@raumatbel This will be saved in the quote table.

– Jeremy Trpka
Jun 25 '18 at 11:34










1 Answer
1






active

oldest

votes


















0














First time, you must create a file extension_attributes.xml




VendorNamespaceetcextension_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="MagentoQuoteApiDataCartInterface">
<attribute code="from_app" type="string"/>
</extension_attributes>




Then, you have to change the behavior of the get(), getList() and save() function of the repository.




VendorNamespaceetcextension_attributes.xml




<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="vendor_namespace_plugin_cart_repository" type="VendorNamespacePluginCartRepositoryPlugin"/>
</type>




Having the field in the table of DB quote, you can directly save the data by the magic method. If you do this, in the plugin of below, you do not need the function beforSave().



$quote->setFromApp(true);
$this->quoteRepository->save($quote);


You can do on this way also, in which, the function beforeSave() of the plugin of below is mandatory.



$quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);
$quote->setExtensionAttributes($quoteExtension);

$this->quoteRepository->save($quote);



VendorNamespacePluginCartRepositoryPlugin




<?php
namespace VendorNamespacePlugin;

use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiDataCartExtension;
use MagentoQuoteApiDataCartExtensionFactory;
use MagentoQuoteApiDataCartInterface;
use MagentoQuoteApiDataCartSearchResultsInterface;

/**
* Class CartRepositoryPlugin
*/
class CartRepositoryPlugin

/**
* @var CartExtensionFactory
*/
private $extensionFactory;

/**
* CartRepositoryPlugin constructor.
*
* @param CartExtensionFactory $orderExtensionFactory
*/
public function __construct(
CartExtensionFactory $orderExtensionFactory
)
$this->extensionFactory = $orderExtensionFactory;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $resultEntity
* @return CartInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
CartRepositoryInterface $subject,
CartInterface $resultEntity
)
/** @var CartExtension $extensionAttributes */
$extensionAttributes = $resultEntity->getExtensionAttributes() ?: $this->extensionFactory->create();

$extensionAttributes->setFromApp($resultEntity->getData('from_app'));

$resultEntity->setExtensionAttributes($extensionAttributes);

return $resultEntity;


/**
* @param CartRepositoryInterface $subject
* @param CartSearchResultsInterface $resultCart
* @return CartSearchResultsInterface
*/
public function afterGetList(
CartRepositoryInterface $subject,
CartSearchResultsInterface $resultCart
)
/** @var CartInterface $order */
foreach ($resultCart->getItems() as $order)
$this->afterGet($subject, $order);


return $resultCart;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return array
*/
public function beforeSave(
CartRepositoryInterface $subject,
CartInterface $quote
)
$extensionAttributes = $quote->getExtensionAttributes() ?: $this->extensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
$quote->setFromApp($extensionAttributes->getFromApp());


return [$quote];




How you can look, the function beforeSave() use the magic method setFromApp() to save the data, instead of this, you can use the magic method directly to save the quote how I explained before.



If the data were in another table of DB with its own model, the function of the plugin will be afterSave() and it will seem similar to this.



/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return CartInterface
*/
public function afterSave(
CartRepositoryInterface $subject,
CartInterface $result
)
$extensionAttributes = $result->getExtensionAttributes() ?: $this->storeExtensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
/** @var CustomEntity $customEntity */
$customEntity = $this->customEntityFactory->create();
$customEntity->setQuoteId($result->getId());
$customEntity->setFromApp($result->getFromApp());
$this->customEntityRepository->save($customEntity);


return $result;






share|improve this answer

























  • Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

    – Jeremy Trpka
    Jun 25 '18 at 20:06











  • The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

    – raumatbel
    Jun 25 '18 at 20:45











  • Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

    – Jeremy Trpka
    Jun 26 '18 at 12:01













Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f231036%2fmagento-2-save-extension-attribute-aftersave-returns-void%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First time, you must create a file extension_attributes.xml




VendorNamespaceetcextension_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="MagentoQuoteApiDataCartInterface">
<attribute code="from_app" type="string"/>
</extension_attributes>




Then, you have to change the behavior of the get(), getList() and save() function of the repository.




VendorNamespaceetcextension_attributes.xml




<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="vendor_namespace_plugin_cart_repository" type="VendorNamespacePluginCartRepositoryPlugin"/>
</type>




Having the field in the table of DB quote, you can directly save the data by the magic method. If you do this, in the plugin of below, you do not need the function beforSave().



$quote->setFromApp(true);
$this->quoteRepository->save($quote);


You can do on this way also, in which, the function beforeSave() of the plugin of below is mandatory.



$quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);
$quote->setExtensionAttributes($quoteExtension);

$this->quoteRepository->save($quote);



VendorNamespacePluginCartRepositoryPlugin




<?php
namespace VendorNamespacePlugin;

use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiDataCartExtension;
use MagentoQuoteApiDataCartExtensionFactory;
use MagentoQuoteApiDataCartInterface;
use MagentoQuoteApiDataCartSearchResultsInterface;

/**
* Class CartRepositoryPlugin
*/
class CartRepositoryPlugin

/**
* @var CartExtensionFactory
*/
private $extensionFactory;

/**
* CartRepositoryPlugin constructor.
*
* @param CartExtensionFactory $orderExtensionFactory
*/
public function __construct(
CartExtensionFactory $orderExtensionFactory
)
$this->extensionFactory = $orderExtensionFactory;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $resultEntity
* @return CartInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
CartRepositoryInterface $subject,
CartInterface $resultEntity
)
/** @var CartExtension $extensionAttributes */
$extensionAttributes = $resultEntity->getExtensionAttributes() ?: $this->extensionFactory->create();

$extensionAttributes->setFromApp($resultEntity->getData('from_app'));

$resultEntity->setExtensionAttributes($extensionAttributes);

return $resultEntity;


/**
* @param CartRepositoryInterface $subject
* @param CartSearchResultsInterface $resultCart
* @return CartSearchResultsInterface
*/
public function afterGetList(
CartRepositoryInterface $subject,
CartSearchResultsInterface $resultCart
)
/** @var CartInterface $order */
foreach ($resultCart->getItems() as $order)
$this->afterGet($subject, $order);


return $resultCart;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return array
*/
public function beforeSave(
CartRepositoryInterface $subject,
CartInterface $quote
)
$extensionAttributes = $quote->getExtensionAttributes() ?: $this->extensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
$quote->setFromApp($extensionAttributes->getFromApp());


return [$quote];




How you can look, the function beforeSave() use the magic method setFromApp() to save the data, instead of this, you can use the magic method directly to save the quote how I explained before.



If the data were in another table of DB with its own model, the function of the plugin will be afterSave() and it will seem similar to this.



/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return CartInterface
*/
public function afterSave(
CartRepositoryInterface $subject,
CartInterface $result
)
$extensionAttributes = $result->getExtensionAttributes() ?: $this->storeExtensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
/** @var CustomEntity $customEntity */
$customEntity = $this->customEntityFactory->create();
$customEntity->setQuoteId($result->getId());
$customEntity->setFromApp($result->getFromApp());
$this->customEntityRepository->save($customEntity);


return $result;






share|improve this answer

























  • Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

    – Jeremy Trpka
    Jun 25 '18 at 20:06











  • The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

    – raumatbel
    Jun 25 '18 at 20:45











  • Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

    – Jeremy Trpka
    Jun 26 '18 at 12:01















0














First time, you must create a file extension_attributes.xml




VendorNamespaceetcextension_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="MagentoQuoteApiDataCartInterface">
<attribute code="from_app" type="string"/>
</extension_attributes>




Then, you have to change the behavior of the get(), getList() and save() function of the repository.




VendorNamespaceetcextension_attributes.xml




<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="vendor_namespace_plugin_cart_repository" type="VendorNamespacePluginCartRepositoryPlugin"/>
</type>




Having the field in the table of DB quote, you can directly save the data by the magic method. If you do this, in the plugin of below, you do not need the function beforSave().



$quote->setFromApp(true);
$this->quoteRepository->save($quote);


You can do on this way also, in which, the function beforeSave() of the plugin of below is mandatory.



$quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);
$quote->setExtensionAttributes($quoteExtension);

$this->quoteRepository->save($quote);



VendorNamespacePluginCartRepositoryPlugin




<?php
namespace VendorNamespacePlugin;

use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiDataCartExtension;
use MagentoQuoteApiDataCartExtensionFactory;
use MagentoQuoteApiDataCartInterface;
use MagentoQuoteApiDataCartSearchResultsInterface;

/**
* Class CartRepositoryPlugin
*/
class CartRepositoryPlugin

/**
* @var CartExtensionFactory
*/
private $extensionFactory;

/**
* CartRepositoryPlugin constructor.
*
* @param CartExtensionFactory $orderExtensionFactory
*/
public function __construct(
CartExtensionFactory $orderExtensionFactory
)
$this->extensionFactory = $orderExtensionFactory;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $resultEntity
* @return CartInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
CartRepositoryInterface $subject,
CartInterface $resultEntity
)
/** @var CartExtension $extensionAttributes */
$extensionAttributes = $resultEntity->getExtensionAttributes() ?: $this->extensionFactory->create();

$extensionAttributes->setFromApp($resultEntity->getData('from_app'));

$resultEntity->setExtensionAttributes($extensionAttributes);

return $resultEntity;


/**
* @param CartRepositoryInterface $subject
* @param CartSearchResultsInterface $resultCart
* @return CartSearchResultsInterface
*/
public function afterGetList(
CartRepositoryInterface $subject,
CartSearchResultsInterface $resultCart
)
/** @var CartInterface $order */
foreach ($resultCart->getItems() as $order)
$this->afterGet($subject, $order);


return $resultCart;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return array
*/
public function beforeSave(
CartRepositoryInterface $subject,
CartInterface $quote
)
$extensionAttributes = $quote->getExtensionAttributes() ?: $this->extensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
$quote->setFromApp($extensionAttributes->getFromApp());


return [$quote];




How you can look, the function beforeSave() use the magic method setFromApp() to save the data, instead of this, you can use the magic method directly to save the quote how I explained before.



If the data were in another table of DB with its own model, the function of the plugin will be afterSave() and it will seem similar to this.



/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return CartInterface
*/
public function afterSave(
CartRepositoryInterface $subject,
CartInterface $result
)
$extensionAttributes = $result->getExtensionAttributes() ?: $this->storeExtensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
/** @var CustomEntity $customEntity */
$customEntity = $this->customEntityFactory->create();
$customEntity->setQuoteId($result->getId());
$customEntity->setFromApp($result->getFromApp());
$this->customEntityRepository->save($customEntity);


return $result;






share|improve this answer

























  • Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

    – Jeremy Trpka
    Jun 25 '18 at 20:06











  • The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

    – raumatbel
    Jun 25 '18 at 20:45











  • Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

    – Jeremy Trpka
    Jun 26 '18 at 12:01













0












0








0







First time, you must create a file extension_attributes.xml




VendorNamespaceetcextension_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="MagentoQuoteApiDataCartInterface">
<attribute code="from_app" type="string"/>
</extension_attributes>




Then, you have to change the behavior of the get(), getList() and save() function of the repository.




VendorNamespaceetcextension_attributes.xml




<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="vendor_namespace_plugin_cart_repository" type="VendorNamespacePluginCartRepositoryPlugin"/>
</type>




Having the field in the table of DB quote, you can directly save the data by the magic method. If you do this, in the plugin of below, you do not need the function beforSave().



$quote->setFromApp(true);
$this->quoteRepository->save($quote);


You can do on this way also, in which, the function beforeSave() of the plugin of below is mandatory.



$quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);
$quote->setExtensionAttributes($quoteExtension);

$this->quoteRepository->save($quote);



VendorNamespacePluginCartRepositoryPlugin




<?php
namespace VendorNamespacePlugin;

use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiDataCartExtension;
use MagentoQuoteApiDataCartExtensionFactory;
use MagentoQuoteApiDataCartInterface;
use MagentoQuoteApiDataCartSearchResultsInterface;

/**
* Class CartRepositoryPlugin
*/
class CartRepositoryPlugin

/**
* @var CartExtensionFactory
*/
private $extensionFactory;

/**
* CartRepositoryPlugin constructor.
*
* @param CartExtensionFactory $orderExtensionFactory
*/
public function __construct(
CartExtensionFactory $orderExtensionFactory
)
$this->extensionFactory = $orderExtensionFactory;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $resultEntity
* @return CartInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
CartRepositoryInterface $subject,
CartInterface $resultEntity
)
/** @var CartExtension $extensionAttributes */
$extensionAttributes = $resultEntity->getExtensionAttributes() ?: $this->extensionFactory->create();

$extensionAttributes->setFromApp($resultEntity->getData('from_app'));

$resultEntity->setExtensionAttributes($extensionAttributes);

return $resultEntity;


/**
* @param CartRepositoryInterface $subject
* @param CartSearchResultsInterface $resultCart
* @return CartSearchResultsInterface
*/
public function afterGetList(
CartRepositoryInterface $subject,
CartSearchResultsInterface $resultCart
)
/** @var CartInterface $order */
foreach ($resultCart->getItems() as $order)
$this->afterGet($subject, $order);


return $resultCart;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return array
*/
public function beforeSave(
CartRepositoryInterface $subject,
CartInterface $quote
)
$extensionAttributes = $quote->getExtensionAttributes() ?: $this->extensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
$quote->setFromApp($extensionAttributes->getFromApp());


return [$quote];




How you can look, the function beforeSave() use the magic method setFromApp() to save the data, instead of this, you can use the magic method directly to save the quote how I explained before.



If the data were in another table of DB with its own model, the function of the plugin will be afterSave() and it will seem similar to this.



/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return CartInterface
*/
public function afterSave(
CartRepositoryInterface $subject,
CartInterface $result
)
$extensionAttributes = $result->getExtensionAttributes() ?: $this->storeExtensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
/** @var CustomEntity $customEntity */
$customEntity = $this->customEntityFactory->create();
$customEntity->setQuoteId($result->getId());
$customEntity->setFromApp($result->getFromApp());
$this->customEntityRepository->save($customEntity);


return $result;






share|improve this answer















First time, you must create a file extension_attributes.xml




VendorNamespaceetcextension_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="MagentoQuoteApiDataCartInterface">
<attribute code="from_app" type="string"/>
</extension_attributes>




Then, you have to change the behavior of the get(), getList() and save() function of the repository.




VendorNamespaceetcextension_attributes.xml




<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="vendor_namespace_plugin_cart_repository" type="VendorNamespacePluginCartRepositoryPlugin"/>
</type>




Having the field in the table of DB quote, you can directly save the data by the magic method. If you do this, in the plugin of below, you do not need the function beforSave().



$quote->setFromApp(true);
$this->quoteRepository->save($quote);


You can do on this way also, in which, the function beforeSave() of the plugin of below is mandatory.



$quote = $this->quoteRepository->getActive($cartId);

// Extension Attributes
$quoteExtension = $quote->getExtensionAttributes();
$quoteExtension->setFromApp(true);
$quote->setExtensionAttributes($quoteExtension);

$this->quoteRepository->save($quote);



VendorNamespacePluginCartRepositoryPlugin




<?php
namespace VendorNamespacePlugin;

use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiDataCartExtension;
use MagentoQuoteApiDataCartExtensionFactory;
use MagentoQuoteApiDataCartInterface;
use MagentoQuoteApiDataCartSearchResultsInterface;

/**
* Class CartRepositoryPlugin
*/
class CartRepositoryPlugin

/**
* @var CartExtensionFactory
*/
private $extensionFactory;

/**
* CartRepositoryPlugin constructor.
*
* @param CartExtensionFactory $orderExtensionFactory
*/
public function __construct(
CartExtensionFactory $orderExtensionFactory
)
$this->extensionFactory = $orderExtensionFactory;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $resultEntity
* @return CartInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
CartRepositoryInterface $subject,
CartInterface $resultEntity
)
/** @var CartExtension $extensionAttributes */
$extensionAttributes = $resultEntity->getExtensionAttributes() ?: $this->extensionFactory->create();

$extensionAttributes->setFromApp($resultEntity->getData('from_app'));

$resultEntity->setExtensionAttributes($extensionAttributes);

return $resultEntity;


/**
* @param CartRepositoryInterface $subject
* @param CartSearchResultsInterface $resultCart
* @return CartSearchResultsInterface
*/
public function afterGetList(
CartRepositoryInterface $subject,
CartSearchResultsInterface $resultCart
)
/** @var CartInterface $order */
foreach ($resultCart->getItems() as $order)
$this->afterGet($subject, $order);


return $resultCart;


/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return array
*/
public function beforeSave(
CartRepositoryInterface $subject,
CartInterface $quote
)
$extensionAttributes = $quote->getExtensionAttributes() ?: $this->extensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
$quote->setFromApp($extensionAttributes->getFromApp());


return [$quote];




How you can look, the function beforeSave() use the magic method setFromApp() to save the data, instead of this, you can use the magic method directly to save the quote how I explained before.



If the data were in another table of DB with its own model, the function of the plugin will be afterSave() and it will seem similar to this.



/**
* @param CartRepositoryInterface $subject
* @param CartInterface $result
* @return CartInterface
*/
public function afterSave(
CartRepositoryInterface $subject,
CartInterface $result
)
$extensionAttributes = $result->getExtensionAttributes() ?: $this->storeExtensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getFromApp() !== null)
/** @var CustomEntity $customEntity */
$customEntity = $this->customEntityFactory->create();
$customEntity->setQuoteId($result->getId());
$customEntity->setFromApp($result->getFromApp());
$this->customEntityRepository->save($customEntity);


return $result;







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 25 at 5:09









Amit Bera

61.7k16 gold badges84 silver badges183 bronze badges




61.7k16 gold badges84 silver badges183 bronze badges










answered Jun 25 '18 at 13:16









raumatbelraumatbel

7554 silver badges14 bronze badges




7554 silver badges14 bronze badges












  • Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

    – Jeremy Trpka
    Jun 25 '18 at 20:06











  • The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

    – raumatbel
    Jun 25 '18 at 20:45











  • Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

    – Jeremy Trpka
    Jun 26 '18 at 12:01

















  • Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

    – Jeremy Trpka
    Jun 25 '18 at 20:06











  • The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

    – raumatbel
    Jun 25 '18 at 20:45











  • Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

    – Jeremy Trpka
    Jun 26 '18 at 12:01
















Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

– Jeremy Trpka
Jun 25 '18 at 20:06





Awesome! I did not knew I could use magic functions if I had columns made in the appropriate db tables. A far better solution than using extension attributes as I was having issues (unrelated to the question) using them with this project.

– Jeremy Trpka
Jun 25 '18 at 20:06













The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

– raumatbel
Jun 25 '18 at 20:45





The correct way is use the extension attributes, but if you save the data in the table "quote", for saving you can use the magic method, but you need do the plugin to load the data with the function get() and getList().

– raumatbel
Jun 25 '18 at 20:45













Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

– Jeremy Trpka
Jun 26 '18 at 12:01





Yeah I get that, but in my current situation I can not use the get() and getList() for reasons unrelated to this question.

– Jeremy Trpka
Jun 26 '18 at 12:01

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f231036%2fmagento-2-save-extension-attribute-aftersave-returns-void%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form