How to access to configurable product stock qty via js?












2















Someone knows how to get qty for each simple product of configurable product via javascript in product page?



It seems that spConfig retrieved with



#product_addtocart_form": {
"configurable": {
"spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>
}
}


hasn't data about qty.



I have to retrieve it with custom code or do you have some hints on how to find qty information?










share|improve this question





























    2















    Someone knows how to get qty for each simple product of configurable product via javascript in product page?



    It seems that spConfig retrieved with



    #product_addtocart_form": {
    "configurable": {
    "spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>
    }
    }


    hasn't data about qty.



    I have to retrieve it with custom code or do you have some hints on how to find qty information?










    share|improve this question



























      2












      2








      2


      1






      Someone knows how to get qty for each simple product of configurable product via javascript in product page?



      It seems that spConfig retrieved with



      #product_addtocart_form": {
      "configurable": {
      "spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>
      }
      }


      hasn't data about qty.



      I have to retrieve it with custom code or do you have some hints on how to find qty information?










      share|improve this question
















      Someone knows how to get qty for each simple product of configurable product via javascript in product page?



      It seems that spConfig retrieved with



      #product_addtocart_form": {
      "configurable": {
      "spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>
      }
      }


      hasn't data about qty.



      I have to retrieve it with custom code or do you have some hints on how to find qty information?







      magento2 configurable-product






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 6 '16 at 10:31







      LucScu

















      asked Jun 6 '16 at 10:24









      LucScuLucScu

      1,256929




      1,256929






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Thx to @Amit Bera for hints, here my answer.



          Override MagentoConfigurableProductModelConfigurableAttributeData and extends getAttributeOptionsData() adding qty field:



          /**
          * @var \MagentoCatalogInventoryApiStockStateInterface
          */
          protected $_stockState;



          /**
          *
          */
          public function __construct(
          MagentoCatalogInventoryApiStockStateInterface $stockState
          ) {

          $this->_stockState = $stockState;

          }



          /**
          * @param Attribute $attribute
          * @param array $config
          * @return array
          */
          protected function getAttributeOptionsData($attribute, $config) {

          $attributeOptionsData = ;
          foreach ($attribute->getOptions() as $attributeOption) {

          $optionId = $attributeOption['value_index'];

          $attributeOptionsData = [
          'id' => $optionId,
          'label' => $attributeOption['label'],
          'products' => isset($config[$attribute->getAttributeId()][$optionId])
          ? $config[$attribute->getAttributeId()][$optionId]
          : ,
          'qty' => isset($config[$attribute->getAttributeId()][$optionId][0])
          ? $this->_stockState->getStockQty($config[$attribute->getAttributeId()][$optionId][0])
          : '',
          ];
          }

          return $attributeOptionsData;

          }


          Now in appcodexxxyyyviewfrontendtemplatesproductviewtypeoptionsconfigurable.phtml you could retrieve simple product qty with simple js:



          var config = <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>;
          .....
          // loop options config.attributes[attribute_id].options
          // for each simple product to obtain:
          // Object {id: "231", label: "M", products: ["2089"], qty: 2}





          share|improve this answer































            0














            In this case,you need to rewrite class MagentoConfigurableProductHelperData;



            then at getOptions() function at



             $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');

            $options['qty'][$productId]=$StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());


            after $images = $this->getGalleryImages($product);






            share|improve this answer


























            • thx for the hint, have i to instantiate $StockState right?

              – LucScu
              Jun 6 '16 at 12:13






            • 1





              yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

              – Amit Bera
              Jun 6 '16 at 12:17











            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%2f119508%2fhow-to-access-to-configurable-product-stock-qty-via-js%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














            Thx to @Amit Bera for hints, here my answer.



            Override MagentoConfigurableProductModelConfigurableAttributeData and extends getAttributeOptionsData() adding qty field:



            /**
            * @var \MagentoCatalogInventoryApiStockStateInterface
            */
            protected $_stockState;



            /**
            *
            */
            public function __construct(
            MagentoCatalogInventoryApiStockStateInterface $stockState
            ) {

            $this->_stockState = $stockState;

            }



            /**
            * @param Attribute $attribute
            * @param array $config
            * @return array
            */
            protected function getAttributeOptionsData($attribute, $config) {

            $attributeOptionsData = ;
            foreach ($attribute->getOptions() as $attributeOption) {

            $optionId = $attributeOption['value_index'];

            $attributeOptionsData = [
            'id' => $optionId,
            'label' => $attributeOption['label'],
            'products' => isset($config[$attribute->getAttributeId()][$optionId])
            ? $config[$attribute->getAttributeId()][$optionId]
            : ,
            'qty' => isset($config[$attribute->getAttributeId()][$optionId][0])
            ? $this->_stockState->getStockQty($config[$attribute->getAttributeId()][$optionId][0])
            : '',
            ];
            }

            return $attributeOptionsData;

            }


            Now in appcodexxxyyyviewfrontendtemplatesproductviewtypeoptionsconfigurable.phtml you could retrieve simple product qty with simple js:



            var config = <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>;
            .....
            // loop options config.attributes[attribute_id].options
            // for each simple product to obtain:
            // Object {id: "231", label: "M", products: ["2089"], qty: 2}





            share|improve this answer




























              1














              Thx to @Amit Bera for hints, here my answer.



              Override MagentoConfigurableProductModelConfigurableAttributeData and extends getAttributeOptionsData() adding qty field:



              /**
              * @var \MagentoCatalogInventoryApiStockStateInterface
              */
              protected $_stockState;



              /**
              *
              */
              public function __construct(
              MagentoCatalogInventoryApiStockStateInterface $stockState
              ) {

              $this->_stockState = $stockState;

              }



              /**
              * @param Attribute $attribute
              * @param array $config
              * @return array
              */
              protected function getAttributeOptionsData($attribute, $config) {

              $attributeOptionsData = ;
              foreach ($attribute->getOptions() as $attributeOption) {

              $optionId = $attributeOption['value_index'];

              $attributeOptionsData = [
              'id' => $optionId,
              'label' => $attributeOption['label'],
              'products' => isset($config[$attribute->getAttributeId()][$optionId])
              ? $config[$attribute->getAttributeId()][$optionId]
              : ,
              'qty' => isset($config[$attribute->getAttributeId()][$optionId][0])
              ? $this->_stockState->getStockQty($config[$attribute->getAttributeId()][$optionId][0])
              : '',
              ];
              }

              return $attributeOptionsData;

              }


              Now in appcodexxxyyyviewfrontendtemplatesproductviewtypeoptionsconfigurable.phtml you could retrieve simple product qty with simple js:



              var config = <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>;
              .....
              // loop options config.attributes[attribute_id].options
              // for each simple product to obtain:
              // Object {id: "231", label: "M", products: ["2089"], qty: 2}





              share|improve this answer


























                1












                1








                1







                Thx to @Amit Bera for hints, here my answer.



                Override MagentoConfigurableProductModelConfigurableAttributeData and extends getAttributeOptionsData() adding qty field:



                /**
                * @var \MagentoCatalogInventoryApiStockStateInterface
                */
                protected $_stockState;



                /**
                *
                */
                public function __construct(
                MagentoCatalogInventoryApiStockStateInterface $stockState
                ) {

                $this->_stockState = $stockState;

                }



                /**
                * @param Attribute $attribute
                * @param array $config
                * @return array
                */
                protected function getAttributeOptionsData($attribute, $config) {

                $attributeOptionsData = ;
                foreach ($attribute->getOptions() as $attributeOption) {

                $optionId = $attributeOption['value_index'];

                $attributeOptionsData = [
                'id' => $optionId,
                'label' => $attributeOption['label'],
                'products' => isset($config[$attribute->getAttributeId()][$optionId])
                ? $config[$attribute->getAttributeId()][$optionId]
                : ,
                'qty' => isset($config[$attribute->getAttributeId()][$optionId][0])
                ? $this->_stockState->getStockQty($config[$attribute->getAttributeId()][$optionId][0])
                : '',
                ];
                }

                return $attributeOptionsData;

                }


                Now in appcodexxxyyyviewfrontendtemplatesproductviewtypeoptionsconfigurable.phtml you could retrieve simple product qty with simple js:



                var config = <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>;
                .....
                // loop options config.attributes[attribute_id].options
                // for each simple product to obtain:
                // Object {id: "231", label: "M", products: ["2089"], qty: 2}





                share|improve this answer













                Thx to @Amit Bera for hints, here my answer.



                Override MagentoConfigurableProductModelConfigurableAttributeData and extends getAttributeOptionsData() adding qty field:



                /**
                * @var \MagentoCatalogInventoryApiStockStateInterface
                */
                protected $_stockState;



                /**
                *
                */
                public function __construct(
                MagentoCatalogInventoryApiStockStateInterface $stockState
                ) {

                $this->_stockState = $stockState;

                }



                /**
                * @param Attribute $attribute
                * @param array $config
                * @return array
                */
                protected function getAttributeOptionsData($attribute, $config) {

                $attributeOptionsData = ;
                foreach ($attribute->getOptions() as $attributeOption) {

                $optionId = $attributeOption['value_index'];

                $attributeOptionsData = [
                'id' => $optionId,
                'label' => $attributeOption['label'],
                'products' => isset($config[$attribute->getAttributeId()][$optionId])
                ? $config[$attribute->getAttributeId()][$optionId]
                : ,
                'qty' => isset($config[$attribute->getAttributeId()][$optionId][0])
                ? $this->_stockState->getStockQty($config[$attribute->getAttributeId()][$optionId][0])
                : '',
                ];
                }

                return $attributeOptionsData;

                }


                Now in appcodexxxyyyviewfrontendtemplatesproductviewtypeoptionsconfigurable.phtml you could retrieve simple product qty with simple js:



                var config = <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>;
                .....
                // loop options config.attributes[attribute_id].options
                // for each simple product to obtain:
                // Object {id: "231", label: "M", products: ["2089"], qty: 2}






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 6 '16 at 15:30









                LucScuLucScu

                1,256929




                1,256929

























                    0














                    In this case,you need to rewrite class MagentoConfigurableProductHelperData;



                    then at getOptions() function at



                     $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                    $StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');

                    $options['qty'][$productId]=$StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());


                    after $images = $this->getGalleryImages($product);






                    share|improve this answer


























                    • thx for the hint, have i to instantiate $StockState right?

                      – LucScu
                      Jun 6 '16 at 12:13






                    • 1





                      yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

                      – Amit Bera
                      Jun 6 '16 at 12:17
















                    0














                    In this case,you need to rewrite class MagentoConfigurableProductHelperData;



                    then at getOptions() function at



                     $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                    $StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');

                    $options['qty'][$productId]=$StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());


                    after $images = $this->getGalleryImages($product);






                    share|improve this answer


























                    • thx for the hint, have i to instantiate $StockState right?

                      – LucScu
                      Jun 6 '16 at 12:13






                    • 1





                      yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

                      – Amit Bera
                      Jun 6 '16 at 12:17














                    0












                    0








                    0







                    In this case,you need to rewrite class MagentoConfigurableProductHelperData;



                    then at getOptions() function at



                     $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                    $StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');

                    $options['qty'][$productId]=$StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());


                    after $images = $this->getGalleryImages($product);






                    share|improve this answer















                    In this case,you need to rewrite class MagentoConfigurableProductHelperData;



                    then at getOptions() function at



                     $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                    $StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');

                    $options['qty'][$productId]=$StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());


                    after $images = $this->getGalleryImages($product);







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 35 mins ago









                    Teja Bhagavan Kollepara

                    2,98641847




                    2,98641847










                    answered Jun 6 '16 at 10:45









                    Amit BeraAmit Bera

                    58.9k1475174




                    58.9k1475174













                    • thx for the hint, have i to instantiate $StockState right?

                      – LucScu
                      Jun 6 '16 at 12:13






                    • 1





                      yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

                      – Amit Bera
                      Jun 6 '16 at 12:17



















                    • thx for the hint, have i to instantiate $StockState right?

                      – LucScu
                      Jun 6 '16 at 12:13






                    • 1





                      yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

                      – Amit Bera
                      Jun 6 '16 at 12:17

















                    thx for the hint, have i to instantiate $StockState right?

                    – LucScu
                    Jun 6 '16 at 12:13





                    thx for the hint, have i to instantiate $StockState right?

                    – LucScu
                    Jun 6 '16 at 12:13




                    1




                    1





                    yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

                    – Amit Bera
                    Jun 6 '16 at 12:17





                    yes, i have call StockStateInterface inter face by using magento.stackexchange.com/questions/97943/…

                    – Amit Bera
                    Jun 6 '16 at 12:17


















                    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%2f119508%2fhow-to-access-to-configurable-product-stock-qty-via-js%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