Skip to main content

Magento 2 Custom product option edit using JS

Custom product option edit using JS

 <?php  
 $objectManager  = \Magento\Framework\App\ObjectManager::getInstance();  
 $cart      = $objectManager->get('\Magento\Checkout\Model\Cart');  
 //This doesn't work  
 $itemsCollection = $cart->getQuote()->getItemsCollection();  
 //This doesn't work too  
 $itemsVisible = $cart->getQuote()->getAllVisibleItems();  
 $items    = $cart->getQuote()->getAllItems();  
 $prosuctid  = $_product->getId();  
 foreach ($items as $item) {  
   $options  = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());  
   $optionsid = $item->getProductId();  
   if ($prosuctid == $optionsid) {  
     $customOptions = $options['options']; // error here, line 44  
     if (!empty($customOptions)) {  
       foreach ($customOptions as $option) {  
         $optionId  = $option['option_id'];  
         $optionValue = $option['option_value'];  
   waitForElement("#selection_option_<?= $optionValue ?>", function(){  
   waitForElement("#option-<?= $optionId ?>", function(){  
   var datatype = $("#option-<?= $optionId ?>").attr('data-type');  
   $('#option-<?= $optionId ?>').trigger('click');  
   $('#selection_option_<?= $optionValue ?>').trigger('click');  
   $('.select_<?= $optionId ?> .selection_controls .apply_btn').trigger('click');  
   });  
   });  
   waitForElement("#option-<?= $optionId ?>", function(){  
   var datatype = $("#option-<?= $optionId ?>").attr('data-type');  
   console.log(datatype)  
   if(datatype == "checkbox"){  
   setTimeout(function() {  
   $('#option-<?= $optionId ?>').trigger('click');  
   $('#option-<?= $optionId ?>').trigger('click');  
   console.log( $('#option-<?= $optionId ?>')[0]);  
   }, 1000);  
   }  
   });  
       }  
     }  
   }  
 }  
 ?>  

Comments

Popular posts from this blog

Magento 2 How to get product collection using controller

Getting product collection in Magento 2 Step 1: Declare in Vendor_ModuleName Block Step 2: Display product collection in phtml file .. Step 1: Declare in Vendor_ModuleName Block You will use a block class of the module Vendor_ModuleName, then possibly inject the object of `\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory` in the constructor of the module’s block class.     app/code/Vendor/ModuleName/Block/ProductCollection.php add code this code  <?php namespace Vendor\ModuleName\Block; class ProductCollection extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, array $data = [] ) { $this->_productCollectionFact...

Magento 2 get category collection using specific controllers and category level using object manager

Magento 2 get category collection using object manager <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry') ->registry('current_category'); //get current category $requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface'); //echo $routeName = $requestInterface->getRouteName(); //echo $moduleName = $requestInterface->getModuleName(); $controllerName = $requestInterface->getControllerName(); //echo $actionName = $requestInterface->getActionName(); $parcatId = $category->getId(); // current Category ID $parcategory = $objectManager->create('Magento\Catalog\Model\Category') ->load($parcatId); $parent_category = $parcategory->getParentCategory(); $subcatId = $parent_category->getId(); $subcategory = $objectManager->create('Magento\Catalog\Model\Category...

Magento 2 add single product using custom script or programmatically

Magento 2 add single product programmatically <?php //increase the max execution time @ini_set('max_execution_time', -1); //memory_limit @ini_set('memory_limit', -1); error_reporting(E_ALL); ini_set('display_errors', '1'); use \Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $bootstrapManager = $bootstrap->getObjectManager(); $state = $bootstrapManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager $product = $objectManager->create('\Magento\Catalog\Model\Product'); $sku = 'sku'; $name = 'Sample Product'; $product->setSku($sku); // Set your sku here $product->setName($name); // Name of Product $product->setAttributeSetId(4); // Attribu...