Adding custom options to all products of a certain type on save Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?file option not working in custom options for product import from csvObserver failing to save custom optionsAdd product to cart programmatically with file type custom optionsadding products with custom options to order in adminMagento2: Adding the same custom attribute to all products of a certain Type?how many custom options a product can haveAdd new drop-down custom option typeHow do I check If custom options exists for product?How to edit the custom option price type programmatically?Magento 2 - How to show amount "save $100 considering custom options also on detail page?

Output the ŋarâþ crîþ alphabet song without using (m)any letters

Is the Standard Deduction better than Itemized when both are the same amount?

How to motivate offshore teams and trust them to deliver?

What are the motives behind Cersei's orders given to Bronn?

How widely used is the term Treppenwitz? Is it something that most Germans know?

Gastric acid as a weapon

How can players work together to take actions that are otherwise impossible?

Single word antonym of "flightless"

Should I discuss the type of campaign with my players?

I am not a queen, who am I?

Did Xerox really develop the first LAN?

How discoverable are IPv6 addresses and AAAA names by potential attackers?

How does a Death Domain cleric's Touch of Death feature work with Touch-range spells delivered by familiars?

Can a non-EU citizen traveling with me come with me through the EU passport line?

How can I fade player when goes inside or outside of the area?

Bonus calculation: Am I making a mountain out of a molehill?

What LEGO pieces have "real-world" functionality?

Were Kohanim forbidden from serving in King David's army?

Antler Helmet: Can it work?

How do I mention the quality of my school without bragging

What makes black pepper strong or mild?

Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?

Is there a concise way to say "all of the X, one of each"?

Is there a documented rationale why the House Ways and Means chairman can demand tax info?



Adding custom options to all products of a certain type on save



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?file option not working in custom options for product import from csvObserver failing to save custom optionsAdd product to cart programmatically with file type custom optionsadding products with custom options to order in adminMagento2: Adding the same custom attribute to all products of a certain Type?how many custom options a product can haveAdd new drop-down custom option typeHow do I check If custom options exists for product?How to edit the custom option price type programmatically?Magento 2 - How to show amount "save $100 considering custom options also on detail page?



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








2















I have created some custom product types that extend from simple products. These product types will all have the same custom options added, so I'd like to just set those options on the product when saved as well as removing any other options that might have been added accidentally.



In my product type model, I have added the following save() method:



//My_Module_Model_Product_Type_Customtype extends Mage_Catalog_Model_Product_Type_Simple

public function save($product = null)

$option = array(
'title' => 'My Options',
'type' => 'field',
'is_require' => 0,
'price' => 5,
'price_type' => 'fixed',
'sku' => 'mysku'
);

$product->setCanSaveCustomOptions(true);
$optionInstance = $product->getOptionInstance();
$optionInstance->unsetOptions();
$optionInstance->addOption($option);
$product->setHasOptions(1);
$optionInstance->setProduct($product);

return $this;



When I load an existing product of my type that does not have custom options assigned, the option I am adding inside save() can not be seen after the product saves; however, if I add a custom option from the admin form and then save, my option added in save() does get added. It seems the option will only get saved if the product already has a custom option assigned through the admin



I am guessing I am missing something basic, but can't seem to find what it is.




EDIT



I took a look at the catalog_product_option table, and the option I add in save() is listed here, but it doesn't appear on the product's custom options tab unless I add an additional option manually from admin.




EDIT 2



It seems that $product->setHasOptions() isn't working. When I look at catalog_product_entity, that value is 0. Manually changing it to 1 resolves the issue.



How am I supposed to save custom product type data while in the save() method?










share|improve this question
















bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • This is a common problem. A way around is to wrap your rourine in a registry flag. So action if flag is not set. Set flag when actioned. This will block the recursion.

    – ProxiBlue
    Sep 18 '16 at 0:19

















2















I have created some custom product types that extend from simple products. These product types will all have the same custom options added, so I'd like to just set those options on the product when saved as well as removing any other options that might have been added accidentally.



In my product type model, I have added the following save() method:



//My_Module_Model_Product_Type_Customtype extends Mage_Catalog_Model_Product_Type_Simple

public function save($product = null)

$option = array(
'title' => 'My Options',
'type' => 'field',
'is_require' => 0,
'price' => 5,
'price_type' => 'fixed',
'sku' => 'mysku'
);

$product->setCanSaveCustomOptions(true);
$optionInstance = $product->getOptionInstance();
$optionInstance->unsetOptions();
$optionInstance->addOption($option);
$product->setHasOptions(1);
$optionInstance->setProduct($product);

return $this;



When I load an existing product of my type that does not have custom options assigned, the option I am adding inside save() can not be seen after the product saves; however, if I add a custom option from the admin form and then save, my option added in save() does get added. It seems the option will only get saved if the product already has a custom option assigned through the admin



I am guessing I am missing something basic, but can't seem to find what it is.




EDIT



I took a look at the catalog_product_option table, and the option I add in save() is listed here, but it doesn't appear on the product's custom options tab unless I add an additional option manually from admin.




EDIT 2



It seems that $product->setHasOptions() isn't working. When I look at catalog_product_entity, that value is 0. Manually changing it to 1 resolves the issue.



How am I supposed to save custom product type data while in the save() method?










share|improve this question
















bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • This is a common problem. A way around is to wrap your rourine in a registry flag. So action if flag is not set. Set flag when actioned. This will block the recursion.

    – ProxiBlue
    Sep 18 '16 at 0:19













2












2








2








I have created some custom product types that extend from simple products. These product types will all have the same custom options added, so I'd like to just set those options on the product when saved as well as removing any other options that might have been added accidentally.



In my product type model, I have added the following save() method:



//My_Module_Model_Product_Type_Customtype extends Mage_Catalog_Model_Product_Type_Simple

public function save($product = null)

$option = array(
'title' => 'My Options',
'type' => 'field',
'is_require' => 0,
'price' => 5,
'price_type' => 'fixed',
'sku' => 'mysku'
);

$product->setCanSaveCustomOptions(true);
$optionInstance = $product->getOptionInstance();
$optionInstance->unsetOptions();
$optionInstance->addOption($option);
$product->setHasOptions(1);
$optionInstance->setProduct($product);

return $this;



When I load an existing product of my type that does not have custom options assigned, the option I am adding inside save() can not be seen after the product saves; however, if I add a custom option from the admin form and then save, my option added in save() does get added. It seems the option will only get saved if the product already has a custom option assigned through the admin



I am guessing I am missing something basic, but can't seem to find what it is.




EDIT



I took a look at the catalog_product_option table, and the option I add in save() is listed here, but it doesn't appear on the product's custom options tab unless I add an additional option manually from admin.




EDIT 2



It seems that $product->setHasOptions() isn't working. When I look at catalog_product_entity, that value is 0. Manually changing it to 1 resolves the issue.



How am I supposed to save custom product type data while in the save() method?










share|improve this question
















I have created some custom product types that extend from simple products. These product types will all have the same custom options added, so I'd like to just set those options on the product when saved as well as removing any other options that might have been added accidentally.



In my product type model, I have added the following save() method:



//My_Module_Model_Product_Type_Customtype extends Mage_Catalog_Model_Product_Type_Simple

public function save($product = null)

$option = array(
'title' => 'My Options',
'type' => 'field',
'is_require' => 0,
'price' => 5,
'price_type' => 'fixed',
'sku' => 'mysku'
);

$product->setCanSaveCustomOptions(true);
$optionInstance = $product->getOptionInstance();
$optionInstance->unsetOptions();
$optionInstance->addOption($option);
$product->setHasOptions(1);
$optionInstance->setProduct($product);

return $this;



When I load an existing product of my type that does not have custom options assigned, the option I am adding inside save() can not be seen after the product saves; however, if I add a custom option from the admin form and then save, my option added in save() does get added. It seems the option will only get saved if the product already has a custom option assigned through the admin



I am guessing I am missing something basic, but can't seem to find what it is.




EDIT



I took a look at the catalog_product_option table, and the option I add in save() is listed here, but it doesn't appear on the product's custom options tab unless I add an additional option manually from admin.




EDIT 2



It seems that $product->setHasOptions() isn't working. When I look at catalog_product_entity, that value is 0. Manually changing it to 1 resolves the issue.



How am I supposed to save custom product type data while in the save() method?







magento-1 custom-options






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 17 '16 at 21:00









Siarhey Uchukhlebau

10.1k93058




10.1k93058










asked May 28 '15 at 17:52









pspahnpspahn

3,70822457




3,70822457





bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • This is a common problem. A way around is to wrap your rourine in a registry flag. So action if flag is not set. Set flag when actioned. This will block the recursion.

    – ProxiBlue
    Sep 18 '16 at 0:19

















  • This is a common problem. A way around is to wrap your rourine in a registry flag. So action if flag is not set. Set flag when actioned. This will block the recursion.

    – ProxiBlue
    Sep 18 '16 at 0:19
















This is a common problem. A way around is to wrap your rourine in a registry flag. So action if flag is not set. Set flag when actioned. This will block the recursion.

– ProxiBlue
Sep 18 '16 at 0:19





This is a common problem. A way around is to wrap your rourine in a registry flag. So action if flag is not set. Set flag when actioned. This will block the recursion.

– ProxiBlue
Sep 18 '16 at 0:19










1 Answer
1






active

oldest

votes


















0














I was working on something similar today - I would try to get all current options, push your new option(s) onto that array and do this:



Mage::getSingleton('catalog/product_option')->unsetOptions();
$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true)
->setHasOptions(true);
$product->save();


The first line is explained here (not sure if you'd need it actually): https://stackoverflow.com/questions/4006260/magento-accumulating-custom-options-in-script



Edit: By the way, this code is working perfectly for me, however I'm using it in a script in development






share|improve this answer

























  • $product->save() calls itself as I am currently working inside save(). This would just cause recursion.

    – pspahn
    May 28 '15 at 18:18











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%2f69359%2fadding-custom-options-to-all-products-of-a-certain-type-on-save%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














I was working on something similar today - I would try to get all current options, push your new option(s) onto that array and do this:



Mage::getSingleton('catalog/product_option')->unsetOptions();
$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true)
->setHasOptions(true);
$product->save();


The first line is explained here (not sure if you'd need it actually): https://stackoverflow.com/questions/4006260/magento-accumulating-custom-options-in-script



Edit: By the way, this code is working perfectly for me, however I'm using it in a script in development






share|improve this answer

























  • $product->save() calls itself as I am currently working inside save(). This would just cause recursion.

    – pspahn
    May 28 '15 at 18:18















0














I was working on something similar today - I would try to get all current options, push your new option(s) onto that array and do this:



Mage::getSingleton('catalog/product_option')->unsetOptions();
$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true)
->setHasOptions(true);
$product->save();


The first line is explained here (not sure if you'd need it actually): https://stackoverflow.com/questions/4006260/magento-accumulating-custom-options-in-script



Edit: By the way, this code is working perfectly for me, however I'm using it in a script in development






share|improve this answer

























  • $product->save() calls itself as I am currently working inside save(). This would just cause recursion.

    – pspahn
    May 28 '15 at 18:18













0












0








0







I was working on something similar today - I would try to get all current options, push your new option(s) onto that array and do this:



Mage::getSingleton('catalog/product_option')->unsetOptions();
$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true)
->setHasOptions(true);
$product->save();


The first line is explained here (not sure if you'd need it actually): https://stackoverflow.com/questions/4006260/magento-accumulating-custom-options-in-script



Edit: By the way, this code is working perfectly for me, however I'm using it in a script in development






share|improve this answer















I was working on something similar today - I would try to get all current options, push your new option(s) onto that array and do this:



Mage::getSingleton('catalog/product_option')->unsetOptions();
$product->setProductOptions($options);
$product->setCanSaveCustomOptions(true)
->setHasOptions(true);
$product->save();


The first line is explained here (not sure if you'd need it actually): https://stackoverflow.com/questions/4006260/magento-accumulating-custom-options-in-script



Edit: By the way, this code is working perfectly for me, however I'm using it in a script in development







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 12:37









Community

1




1










answered May 28 '15 at 18:12









brianwalleshauserbrianwalleshauser

3101211




3101211












  • $product->save() calls itself as I am currently working inside save(). This would just cause recursion.

    – pspahn
    May 28 '15 at 18:18

















  • $product->save() calls itself as I am currently working inside save(). This would just cause recursion.

    – pspahn
    May 28 '15 at 18:18
















$product->save() calls itself as I am currently working inside save(). This would just cause recursion.

– pspahn
May 28 '15 at 18:18





$product->save() calls itself as I am currently working inside save(). This would just cause recursion.

– pspahn
May 28 '15 at 18:18

















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%2f69359%2fadding-custom-options-to-all-products-of-a-certain-type-on-save%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

Magento 2 duplicate PHPSESSID cookie when using session_start() in custom php scriptMagento 2: User cant logged in into to account page, no error showing!Magento duplicate on subdomainGrabbing storeview from cookie (after using language selector)How do I run php custom script on magento2Magento 2: Include PHP script in headerSession lock after using Cm_RedisSessionscript php to update stockMagento set cookie popupMagento 2 session id cookie - where to find it?How to import Configurable product from csv with custom attributes using php scriptMagento 2 run custom PHP script

Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2