• javascript

    limit Input text box charecters ,enter only 10 characters at max

    <script>
    var keycount =0;
    jQuery(document).on('keydown', 'input.product-custom-option', function(ev) {
        var c = ev.which;
       // console.log(c);
        if(c==8)
            {
            keycount--;
            }
        else{ 
            keycount++;
           // console.log(keycount);
        }
        if(keycount>10)
         {
    	    return false;
         }
    });
    </script>
    
    <input type="text" class="product-custom-option" />

    OPtion 2

    function ohmyfun()
    {
    var xdata='';
    var lee = document.getElementsByClassName("product-custom-option")[0].id;
    console.log(lee);
     document.getElementsByClassName("product-custom-option")[0].addEventListener("keydown", getleecount);
      
    }
    function getleecount()
    {
          var lee_id = document.getElementsByClassName("product-custom-option")[0].id;
        var xle= document.getElementById(lee_id).value;
        
        console.log(xle.length);
        if(xle.length==9){
        
        xdata = document.getElementById(lee_id).value;
      console.log(xdata);
         document.getElementById(lee_id).value=xdata;
             
        }
        else if(xle.length>9){
            
             console.log(xdata);
             document.getElementById(lee_id).value=xdata;
        }
    }
    </script>
  • javascript

    Get the attribute value in product page programatically

    in Magento_Catalog / template /product/ make a phtml file .

    write the below code

    $_product = $block->getProduct();
    $pi = $_product->getData('giftwrap'); // giftwrap is attribute name 
    if($pi==1){ echo "show image";}

    Magento_Catalog/layout /catalog_product_view.xml

      <block name="catalog.mygifrwrap"  template="Magento_Catalog::product/giftwrap.phtml" class="Magento\Catalog\Block\Product\View"/>

  • MAGENTO2

    Get the product count by attribute(swatches) in filter option in magento 2

     \Magento\Swatches\Block\LayeredNavigation\RenderLayered::getOptionViewData
    return [
        ‘label’ => $swatchOption->getLabel(),
        ‘link’ => $linkToOption,
        ‘custom_style’ => $customStyle,
        ‘count’ => $filterItem->getCount()
    ];
    Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml
    <?php foreach ($swatchData[‘options’] as $option => $label): ?>
        <?php if(isset($label[‘count’])) {echo $label[‘count’];} ?>

  • magento,  MAGENTO2

    Get product price and currency symbol in phtml page magento2 from product id

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    
    $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
    $b= 1234;
    $productd = $objectManager->get('Magento\Catalog\Model\Product')->load($b); 
    $regularPrice= $productd->getPrice();
    $specialPrice = $productd->getFinalPrice();
    <?php  echo $priceHelper->currency($specialPrice, true, false) ?> 
    // code for  currency with special price

  • magento,  MAGENTO2

    How to get product url from product id magento 2

     $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
     $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
    $b= 1232;  // the product id 
     $productd = $objectManager->get('Magento\Catalog\Model\Product')->load($b); 
     $sku = $productd->getSku();// getting Sku 
    $productData = $productRepository->get($sku);
    <a href="<?php echo $productData->getProductUrl(); ?>"> Show product </a>

  • magento,  MAGENTO2

    How to show product image from product id in phtml page magento 2

    The following code will show the product image from given product id

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $b=1232;  // product id 
    $productd = $objectManager->get('Magento\Catalog\Model\Product')->load($b);
    
    <div class="product-image"> 
        <img src="<?php echo $productd->getMediaConfig()->getMediaUrl($productd->getImage()); ?>" >
     </div> 

  • magento,  MAGENTO2

    How to get the SKU of product in phtml magento2 from given product Id.

    Through the help of Object Manager we can get SKU of the product if we have product id.

     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  
     $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
     $b;
     $productd = $objectManager->get('Magento\Catalog\Model\Product')->load($b);  // product id 
     $sku = $productd->getSku();