Product custom option delete while edit product programmatically
I have added a custom option in product from the back-end.
After that when I tried to update the product programmatically
but Facing an issue: When I edited the product all the custom options of product has been deleted.
It removes all options while editing a product. I have checked all entries in the database that's also get deleted.
magento2 product custom-options programmatically
add a comment |
I have added a custom option in product from the back-end.
After that when I tried to update the product programmatically
but Facing an issue: When I edited the product all the custom options of product has been deleted.
It removes all options while editing a product. I have checked all entries in the database that's also get deleted.
magento2 product custom-options programmatically
add a comment |
I have added a custom option in product from the back-end.
After that when I tried to update the product programmatically
but Facing an issue: When I edited the product all the custom options of product has been deleted.
It removes all options while editing a product. I have checked all entries in the database that's also get deleted.
magento2 product custom-options programmatically
I have added a custom option in product from the back-end.
After that when I tried to update the product programmatically
but Facing an issue: When I edited the product all the custom options of product has been deleted.
It removes all options while editing a product. I have checked all entries in the database that's also get deleted.
magento2 product custom-options programmatically
magento2 product custom-options programmatically
edited Jul 3 '18 at 9:12
ABHISHEK TRIPATHI
1,6781626
1,6781626
asked Jul 3 '18 at 7:29
Dharmendra JadavDharmendra Jadav
1,593318
1,593318
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Finally I found the solution of this.
Not great solution but it's working fine for me.
I have first getting all options of the product before save.
protected function getProductOptions($productObj)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
$options = ;
if($customOptions->count() > 0) {
foreach($customOptions as $customOption) {
$optionsValue = ;
$customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
if($customOptionValues->count() > 0) {
foreach($customOptionValues as $customOptionValue) {
$optionsValue = array(
'record_id' => $customOptionValue->getRecordId(),
'title' => $customOptionValue->getTitle(),
'price' => $customOptionValue->getPrice(),
'price_type' => $customOptionValue->getPriceType(),
'sort_order' => $customOptionValue->getSortOrder(),
'sku' => $customOptionValue->getSku(),
'is_delete' => 0,
);
}
}
$sku = $customOption->getSku();
$title = $customOption->getTitle();
$type = $customOption->getType();
$price = $customOption->getPrice();
$price_type = $customOption->getPriceType();
$record_id = $customOption->getRecordId();
$options = array(
'sort_order' => $customOption->getSortOrder(),
'title' => $title,
'price_type' => $price_type,
'price' => $price,
'type' => $type,
'values' => $optionsValue,
'is_require' => 1,
);
}
}
return $options;
}
After that I have added that all options after save the product.
protected function addProductCustomOptions($product, $productOption)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($productOption as $arrayOption) {
$option = $objectManager->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
}
Now I am getting my all product options same as before.
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
add a comment |
i am getting this issue in my magento 1 project , can you please suggest any solution ?
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f232122%2fproduct-custom-option-delete-while-edit-product-programmatically%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
Finally I found the solution of this.
Not great solution but it's working fine for me.
I have first getting all options of the product before save.
protected function getProductOptions($productObj)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
$options = ;
if($customOptions->count() > 0) {
foreach($customOptions as $customOption) {
$optionsValue = ;
$customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
if($customOptionValues->count() > 0) {
foreach($customOptionValues as $customOptionValue) {
$optionsValue = array(
'record_id' => $customOptionValue->getRecordId(),
'title' => $customOptionValue->getTitle(),
'price' => $customOptionValue->getPrice(),
'price_type' => $customOptionValue->getPriceType(),
'sort_order' => $customOptionValue->getSortOrder(),
'sku' => $customOptionValue->getSku(),
'is_delete' => 0,
);
}
}
$sku = $customOption->getSku();
$title = $customOption->getTitle();
$type = $customOption->getType();
$price = $customOption->getPrice();
$price_type = $customOption->getPriceType();
$record_id = $customOption->getRecordId();
$options = array(
'sort_order' => $customOption->getSortOrder(),
'title' => $title,
'price_type' => $price_type,
'price' => $price,
'type' => $type,
'values' => $optionsValue,
'is_require' => 1,
);
}
}
return $options;
}
After that I have added that all options after save the product.
protected function addProductCustomOptions($product, $productOption)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($productOption as $arrayOption) {
$option = $objectManager->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
}
Now I am getting my all product options same as before.
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
add a comment |
Finally I found the solution of this.
Not great solution but it's working fine for me.
I have first getting all options of the product before save.
protected function getProductOptions($productObj)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
$options = ;
if($customOptions->count() > 0) {
foreach($customOptions as $customOption) {
$optionsValue = ;
$customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
if($customOptionValues->count() > 0) {
foreach($customOptionValues as $customOptionValue) {
$optionsValue = array(
'record_id' => $customOptionValue->getRecordId(),
'title' => $customOptionValue->getTitle(),
'price' => $customOptionValue->getPrice(),
'price_type' => $customOptionValue->getPriceType(),
'sort_order' => $customOptionValue->getSortOrder(),
'sku' => $customOptionValue->getSku(),
'is_delete' => 0,
);
}
}
$sku = $customOption->getSku();
$title = $customOption->getTitle();
$type = $customOption->getType();
$price = $customOption->getPrice();
$price_type = $customOption->getPriceType();
$record_id = $customOption->getRecordId();
$options = array(
'sort_order' => $customOption->getSortOrder(),
'title' => $title,
'price_type' => $price_type,
'price' => $price,
'type' => $type,
'values' => $optionsValue,
'is_require' => 1,
);
}
}
return $options;
}
After that I have added that all options after save the product.
protected function addProductCustomOptions($product, $productOption)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($productOption as $arrayOption) {
$option = $objectManager->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
}
Now I am getting my all product options same as before.
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
add a comment |
Finally I found the solution of this.
Not great solution but it's working fine for me.
I have first getting all options of the product before save.
protected function getProductOptions($productObj)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
$options = ;
if($customOptions->count() > 0) {
foreach($customOptions as $customOption) {
$optionsValue = ;
$customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
if($customOptionValues->count() > 0) {
foreach($customOptionValues as $customOptionValue) {
$optionsValue = array(
'record_id' => $customOptionValue->getRecordId(),
'title' => $customOptionValue->getTitle(),
'price' => $customOptionValue->getPrice(),
'price_type' => $customOptionValue->getPriceType(),
'sort_order' => $customOptionValue->getSortOrder(),
'sku' => $customOptionValue->getSku(),
'is_delete' => 0,
);
}
}
$sku = $customOption->getSku();
$title = $customOption->getTitle();
$type = $customOption->getType();
$price = $customOption->getPrice();
$price_type = $customOption->getPriceType();
$record_id = $customOption->getRecordId();
$options = array(
'sort_order' => $customOption->getSortOrder(),
'title' => $title,
'price_type' => $price_type,
'price' => $price,
'type' => $type,
'values' => $optionsValue,
'is_require' => 1,
);
}
}
return $options;
}
After that I have added that all options after save the product.
protected function addProductCustomOptions($product, $productOption)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($productOption as $arrayOption) {
$option = $objectManager->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
}
Now I am getting my all product options same as before.
Finally I found the solution of this.
Not great solution but it's working fine for me.
I have first getting all options of the product before save.
protected function getProductOptions($productObj)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
$options = ;
if($customOptions->count() > 0) {
foreach($customOptions as $customOption) {
$optionsValue = ;
$customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
if($customOptionValues->count() > 0) {
foreach($customOptionValues as $customOptionValue) {
$optionsValue = array(
'record_id' => $customOptionValue->getRecordId(),
'title' => $customOptionValue->getTitle(),
'price' => $customOptionValue->getPrice(),
'price_type' => $customOptionValue->getPriceType(),
'sort_order' => $customOptionValue->getSortOrder(),
'sku' => $customOptionValue->getSku(),
'is_delete' => 0,
);
}
}
$sku = $customOption->getSku();
$title = $customOption->getTitle();
$type = $customOption->getType();
$price = $customOption->getPrice();
$price_type = $customOption->getPriceType();
$record_id = $customOption->getRecordId();
$options = array(
'sort_order' => $customOption->getSortOrder(),
'title' => $title,
'price_type' => $price_type,
'price' => $price,
'type' => $type,
'values' => $optionsValue,
'is_require' => 1,
);
}
}
return $options;
}
After that I have added that all options after save the product.
protected function addProductCustomOptions($product, $productOption)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($productOption as $arrayOption) {
$option = $objectManager->create('MagentoCatalogModelProductOption')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
}
Now I am getting my all product options same as before.
answered Sep 7 '18 at 10:29
Dharmendra JadavDharmendra Jadav
1,593318
1,593318
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
add a comment |
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options.
– Indian
Nov 17 '18 at 10:03
add a comment |
i am getting this issue in my magento 1 project , can you please suggest any solution ?
add a comment |
i am getting this issue in my magento 1 project , can you please suggest any solution ?
add a comment |
i am getting this issue in my magento 1 project , can you please suggest any solution ?
i am getting this issue in my magento 1 project , can you please suggest any solution ?
answered 11 mins ago
megimegi
674
674
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f232122%2fproduct-custom-option-delete-while-edit-product-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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