Magento 2 : We need to save product two time After generating Custom Option programmatically?Product custom option duplication on save within loopCreating Custom Option on Product SaveCreate custom option programatically in magento 1.7.0.1Forcing a certain custom option value when customer adding product to cartMagento add custom options to cartHow to change product name in quote item dynamicallyHow to add custom options programmatically for configurable product in Magento 2Magento 2 custom option not showing created programmaticallyI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom Options

Entering the UK as a British citizen who is a Canadian permanent resident

How to distinguish PICTURE OF ME and PICTURE OF MINE in Chinese?

What kind of SATA connector is this?

What's tha name for when you write multiple voices on same staff? And are there any cons?

return tuple of uncopyable objects

Why are solar panels kept tilted?

Why was Thor doubtful about his worthiness to Mjolnir?

Are rated cable ampacities per conductor or per circuit?

Determine the slope and write the Cartesian equation of the line.

Quote from Leibniz

How do employ ' ("prime") in math mode at the correct depth?

Is there anything special about -1 (0xFFFFFFFF) regarding ADC?

High Memory Utilization in Juniper ACX2100

What is the largest number of identical satellites launched together?

Why do I get two different answers when solving for arclength?

Non-deterministic Finite Automata | Sipser Example 1.16

correct spelling of "carruffel" (fuzz, hustle, all that jazz)

What makes "quality" analog AV cables better than cheap cables?

Why do the lights go out when someone enters the dining room on this ship?

How can dragons propel their breath attacks to a long distance

How can a layman easily get the consensus view of what academia *thinks* about a subject?

Tikz draw contour without some edges, and fill

Anabelian geometry ~ higher category theory

How i can place a block anywhere in Store



Magento 2 : We need to save product two time After generating Custom Option programmatically?


Product custom option duplication on save within loopCreating Custom Option on Product SaveCreate custom option programatically in magento 1.7.0.1Forcing a certain custom option value when customer adding product to cartMagento add custom options to cartHow to change product name in quote item dynamicallyHow to add custom options programmatically for configurable product in Magento 2Magento 2 custom option not showing created programmaticallyI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom Options






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








4















I have work on Custom Module.
In Our Module I had use



<event name="catalog_product_save_after">
<observer name="add_custom_option" instance="NamespaceModulenameObserverAddCustomOption" />
</event>


In observer i Had Generate Product Custom Option Programmatically



 $product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
//$product->save();



i have use this code for generate Product custom Option.




//$product->save();




i had comment this code because it provide me Error




No Such Entity




After saving Product.



Product Options are generated Successfully in Product But Not Display in Front-end



I had Run All Commands



  • setup:upgrade

  • setup:static-content:deploy -f

  • indexer:reindex

  • cache:flush

But Custom Option Not Appears in Front-end



But When i have save product second time after saving product the Options are Appears in Front-end Without need Run any Commands.




I Need Solution to save Product One time and Get Product Option Front End




Is This Possible ?










share|improve this question






















  • I have also the same problems

    – Hardik Makwana
    May 9 at 5:19

















4















I have work on Custom Module.
In Our Module I had use



<event name="catalog_product_save_after">
<observer name="add_custom_option" instance="NamespaceModulenameObserverAddCustomOption" />
</event>


In observer i Had Generate Product Custom Option Programmatically



 $product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
//$product->save();



i have use this code for generate Product custom Option.




//$product->save();




i had comment this code because it provide me Error




No Such Entity




After saving Product.



Product Options are generated Successfully in Product But Not Display in Front-end



I had Run All Commands



  • setup:upgrade

  • setup:static-content:deploy -f

  • indexer:reindex

  • cache:flush

But Custom Option Not Appears in Front-end



But When i have save product second time after saving product the Options are Appears in Front-end Without need Run any Commands.




I Need Solution to save Product One time and Get Product Option Front End




Is This Possible ?










share|improve this question






















  • I have also the same problems

    – Hardik Makwana
    May 9 at 5:19













4












4








4








I have work on Custom Module.
In Our Module I had use



<event name="catalog_product_save_after">
<observer name="add_custom_option" instance="NamespaceModulenameObserverAddCustomOption" />
</event>


In observer i Had Generate Product Custom Option Programmatically



 $product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
//$product->save();



i have use this code for generate Product custom Option.




//$product->save();




i had comment this code because it provide me Error




No Such Entity




After saving Product.



Product Options are generated Successfully in Product But Not Display in Front-end



I had Run All Commands



  • setup:upgrade

  • setup:static-content:deploy -f

  • indexer:reindex

  • cache:flush

But Custom Option Not Appears in Front-end



But When i have save product second time after saving product the Options are Appears in Front-end Without need Run any Commands.




I Need Solution to save Product One time and Get Product Option Front End




Is This Possible ?










share|improve this question














I have work on Custom Module.
In Our Module I had use



<event name="catalog_product_save_after">
<observer name="add_custom_option" instance="NamespaceModulenameObserverAddCustomOption" />
</event>


In observer i Had Generate Product Custom Option Programmatically



 $product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
//$product->save();



i have use this code for generate Product custom Option.




//$product->save();




i had comment this code because it provide me Error




No Such Entity




After saving Product.



Product Options are generated Successfully in Product But Not Display in Front-end



I had Run All Commands



  • setup:upgrade

  • setup:static-content:deploy -f

  • indexer:reindex

  • cache:flush

But Custom Option Not Appears in Front-end



But When i have save product second time after saving product the Options are Appears in Front-end Without need Run any Commands.




I Need Solution to save Product One time and Get Product Option Front End




Is This Possible ?







magento2 event-observer custom-options catalog-product






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 9 at 5:15









Bhavesh MagentoBhavesh Magento

1169




1169












  • I have also the same problems

    – Hardik Makwana
    May 9 at 5:19

















  • I have also the same problems

    – Hardik Makwana
    May 9 at 5:19
















I have also the same problems

– Hardik Makwana
May 9 at 5:19





I have also the same problems

– Hardik Makwana
May 9 at 5:19










2 Answers
2






active

oldest

votes


















1














You can try using a Product Repository MagentoCatalogApiProductCustomOptionRepositoryInterface



Like this:



$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogApiProductCustomOptionRepositoryInterface')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
$product->save();






share|improve this answer

























  • it is not working and read My Question $product->save() provide me Error No Such Entity

    – Bhavesh Magento
    May 9 at 5:50











  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

    – magefms
    May 9 at 5:56












  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

    – Bhavesh Magento
    May 9 at 7:03











  • which Magento version you are using?

    – magefms
    May 9 at 7:30











  • i have use magento 2.3.1

    – Bhavesh Magento
    May 9 at 7:37


















1














Save Product Using ProductRepositoryInterface :-



$option = $this->_objectManager->create(MagentoCatalogModelProductOption::class)
->setProductId($_product->getId())
->setStoreId($_product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
$productRepo = $this->_objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$productRepo->save($product);





share|improve this answer























  • try this.......

    – Rk Rathod
    May 9 at 6:19











  • it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

    – Bhavesh Magento
    May 9 at 6:49











  • Page Not Found in your screenshot

    – Rk Rathod
    May 9 at 6:56











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%2f273928%2fmagento-2-we-need-to-save-product-two-time-after-generating-custom-option-prog%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can try using a Product Repository MagentoCatalogApiProductCustomOptionRepositoryInterface



Like this:



$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogApiProductCustomOptionRepositoryInterface')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
$product->save();






share|improve this answer

























  • it is not working and read My Question $product->save() provide me Error No Such Entity

    – Bhavesh Magento
    May 9 at 5:50











  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

    – magefms
    May 9 at 5:56












  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

    – Bhavesh Magento
    May 9 at 7:03











  • which Magento version you are using?

    – magefms
    May 9 at 7:30











  • i have use magento 2.3.1

    – Bhavesh Magento
    May 9 at 7:37















1














You can try using a Product Repository MagentoCatalogApiProductCustomOptionRepositoryInterface



Like this:



$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogApiProductCustomOptionRepositoryInterface')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
$product->save();






share|improve this answer

























  • it is not working and read My Question $product->save() provide me Error No Such Entity

    – Bhavesh Magento
    May 9 at 5:50











  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

    – magefms
    May 9 at 5:56












  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

    – Bhavesh Magento
    May 9 at 7:03











  • which Magento version you are using?

    – magefms
    May 9 at 7:30











  • i have use magento 2.3.1

    – Bhavesh Magento
    May 9 at 7:37













1












1








1







You can try using a Product Repository MagentoCatalogApiProductCustomOptionRepositoryInterface



Like this:



$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogApiProductCustomOptionRepositoryInterface')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
$product->save();






share|improve this answer















You can try using a Product Repository MagentoCatalogApiProductCustomOptionRepositoryInterface



Like this:



$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption)
$isHasOptions = $product->getTypeInstance()->hasOptions($product);

$option = MagentoFrameworkAppObjectManager::getInstance()
->create('MagentoCatalogApiProductCustomOptionRepositoryInterface')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);

$this->customoptionOperation($option, 'save');
$product->addOption($option);
$product->save();







share|improve this answer














share|improve this answer



share|improve this answer








edited May 9 at 5:53

























answered May 9 at 5:42









magefmsmagefms

3,0502630




3,0502630












  • it is not working and read My Question $product->save() provide me Error No Such Entity

    – Bhavesh Magento
    May 9 at 5:50











  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

    – magefms
    May 9 at 5:56












  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

    – Bhavesh Magento
    May 9 at 7:03











  • which Magento version you are using?

    – magefms
    May 9 at 7:30











  • i have use magento 2.3.1

    – Bhavesh Magento
    May 9 at 7:37

















  • it is not working and read My Question $product->save() provide me Error No Such Entity

    – Bhavesh Magento
    May 9 at 5:50











  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

    – magefms
    May 9 at 5:56












  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

    – Bhavesh Magento
    May 9 at 7:03











  • which Magento version you are using?

    – magefms
    May 9 at 7:30











  • i have use magento 2.3.1

    – Bhavesh Magento
    May 9 at 7:37
















it is not working and read My Question $product->save() provide me Error No Such Entity

– Bhavesh Magento
May 9 at 5:50





it is not working and read My Question $product->save() provide me Error No Such Entity

– Bhavesh Magento
May 9 at 5:50













updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

– magefms
May 9 at 5:56






updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error

– magefms
May 9 at 5:56














No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

– Bhavesh Magento
May 9 at 7:03





No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ]

– Bhavesh Magento
May 9 at 7:03













which Magento version you are using?

– magefms
May 9 at 7:30





which Magento version you are using?

– magefms
May 9 at 7:30













i have use magento 2.3.1

– Bhavesh Magento
May 9 at 7:37





i have use magento 2.3.1

– Bhavesh Magento
May 9 at 7:37













1














Save Product Using ProductRepositoryInterface :-



$option = $this->_objectManager->create(MagentoCatalogModelProductOption::class)
->setProductId($_product->getId())
->setStoreId($_product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
$productRepo = $this->_objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$productRepo->save($product);





share|improve this answer























  • try this.......

    – Rk Rathod
    May 9 at 6:19











  • it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

    – Bhavesh Magento
    May 9 at 6:49











  • Page Not Found in your screenshot

    – Rk Rathod
    May 9 at 6:56















1














Save Product Using ProductRepositoryInterface :-



$option = $this->_objectManager->create(MagentoCatalogModelProductOption::class)
->setProductId($_product->getId())
->setStoreId($_product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
$productRepo = $this->_objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$productRepo->save($product);





share|improve this answer























  • try this.......

    – Rk Rathod
    May 9 at 6:19











  • it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

    – Bhavesh Magento
    May 9 at 6:49











  • Page Not Found in your screenshot

    – Rk Rathod
    May 9 at 6:56













1












1








1







Save Product Using ProductRepositoryInterface :-



$option = $this->_objectManager->create(MagentoCatalogModelProductOption::class)
->setProductId($_product->getId())
->setStoreId($_product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
$productRepo = $this->_objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$productRepo->save($product);





share|improve this answer













Save Product Using ProductRepositoryInterface :-



$option = $this->_objectManager->create(MagentoCatalogModelProductOption::class)
->setProductId($_product->getId())
->setStoreId($_product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
$productRepo = $this->_objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$productRepo->save($product);






share|improve this answer












share|improve this answer



share|improve this answer










answered May 9 at 5:57









Rk RathodRk Rathod

1,714214




1,714214












  • try this.......

    – Rk Rathod
    May 9 at 6:19











  • it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

    – Bhavesh Magento
    May 9 at 6:49











  • Page Not Found in your screenshot

    – Rk Rathod
    May 9 at 6:56

















  • try this.......

    – Rk Rathod
    May 9 at 6:19











  • it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

    – Bhavesh Magento
    May 9 at 6:49











  • Page Not Found in your screenshot

    – Rk Rathod
    May 9 at 6:56
















try this.......

– Rk Rathod
May 9 at 6:19





try this.......

– Rk Rathod
May 9 at 6:19













it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

– Bhavesh Magento
May 9 at 6:49





it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL]

– Bhavesh Magento
May 9 at 6:49













Page Not Found in your screenshot

– Rk Rathod
May 9 at 6:56





Page Not Found in your screenshot

– Rk Rathod
May 9 at 6:56

















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%2f273928%2fmagento-2-we-need-to-save-product-two-time-after-generating-custom-option-prog%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

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

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?