Product custom option delete while edit product programmatically












0















I have added a custom option in product from the back-end.





enter image description here



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.





enter image description here



It removes all options while editing a product. I have checked all entries in the database that's also get deleted.










share|improve this question





























    0















    I have added a custom option in product from the back-end.





    enter image description here



    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.





    enter image description here



    It removes all options while editing a product. I have checked all entries in the database that's also get deleted.










    share|improve this question



























      0












      0








      0








      I have added a custom option in product from the back-end.





      enter image description here



      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.





      enter image description here



      It removes all options while editing a product. I have checked all entries in the database that's also get deleted.










      share|improve this question
















      I have added a custom option in product from the back-end.





      enter image description here



      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.





      enter image description here



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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.






          share|improve this answer
























          • 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



















          0














          i am getting this issue in my magento 1 project , can you please suggest any solution ?






          share|improve this answer























            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%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









            1














            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.






            share|improve this answer
























            • 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
















            1














            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.






            share|improve this answer
























            • 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














            1












            1








            1







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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













            0














            i am getting this issue in my magento 1 project , can you please suggest any solution ?






            share|improve this answer




























              0














              i am getting this issue in my magento 1 project , can you please suggest any solution ?






              share|improve this answer


























                0












                0








                0







                i am getting this issue in my magento 1 project , can you please suggest any solution ?






                share|improve this answer













                i am getting this issue in my magento 1 project , can you please suggest any solution ?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 11 mins ago









                megimegi

                674




                674






























                    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%2f232122%2fproduct-custom-option-delete-while-edit-product-programmatically%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

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto