Top Questions:

  • 5 years-11 months

    How to check if customer is logged in or not in magento

    • Category : php
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    Customer session does not work except some pages

    Method 1.  To check if customer is logged: (best way)Create helper file like Data.phphow to create custom helper or module https://www.dorcode.com/blog/52/how-to-create-and-use-helpers-in-magento-2


    <?php
    namespace Magento\Mytheme\Helper;
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        protected $_customerSession;
        protected $_customerSessionn;
    
        public function __construct(\Magento\Framework\App\Helper\Context $context,
                \Magento\Framework\App\Request\Http $request,
                \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
                \Magento\Customer\Model\SessionFactory $customerSession,
                \Magento\Customer\Model\Session $customerSessionn
            ) 
        {
            $this->request = $request;
            $this->customerRepository = $customerRepository;
            $this->_customerSession = $customerSession;
            $this->_customerSessionn = $customerSessionn;
            parent::__construct($context);
        }
        public function getCustomerName(){
            $customer = $this->_customerSession->create();
            return $customer->getCustomer()->getName();
        }
        public function isLoggedIn()
        {
            return $this->_customerSessionn->isLoggedIn();
        }
    }
    ?>
    
    Call to your template :
    
    <?php
    $myhelper = $this->helper('Magento\Mytheme\Helper\Data');
    $cname=$myhelper->getCustomerName();
    $isLoggedIn=$myhelper->isLoggedIn();
    
    if ($isLoggedIn):
    	   echo 'login';
    else : 
    	  echo 'not login';
    endif;
    ?>
  • 5 years-11 months

    How to limit characters for textarea including count in jquery

    • Category : java
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    Here are two scenarios where the keyup event will not get fired: The user drags text into the textarea. The user copy-paste text in the textarea with a right click (contextual menu).

    Use the HTML5 input event instead for a more robust solution:

    // jquery
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    
    // html
    <div class="control">
    	<textarea spellcheck="true" maxlength="200" name="question" id="question" class="input-textarea required-entry" cols="5" rows="8" data-validate="{required:true}" aria-required="true"></textarea>
      <div class="limittext"><span class="charnum">200 Characters left</span></div>
    </div>
    
    <script type="text/javascript">
    jQuery(document).ready(function() {
      // edit area start
      var maxLength = 200;
      var textarea_id = "question";
      // edit area end
    
      var html='<span style="color:red;">'+ maxLength + ' characters allowed, excess characters trimmed</span>';
      jQuery("#"+textarea_id).on('input',function(){
          var length = this.value.length;
          if (length >= maxLength) {
            this.value = this.value.substring(0, maxLength);
            jQuery(this).next(".limittext").html(html);
          } else {
            var count=maxLength-length;
            jQuery(this).next(".limittext").html('<span class="charnum">' +count+' Characters left</span>');
          }
      });
    });
    </script>
  • 5 years-11 months

    How to check if customer is logged in or not in magento 2?

    • Category : magento
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    Customer session does not work except some pages

    Method 1.  To check if customer is logged: (best way)
    Create helper file like Data.php

    how to create custom helper or module https://www.dorcode.com/blog/52/how-to-create-and-use-helpers-in-magento-2
     
    <?php
    namespace Magento\Mytheme\Helper;
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        protected $_customerSession;
        protected $_customerSessionn;
    
        public function __construct(\Magento\Framework\App\Helper\Context $context,
                \Magento\Framework\App\Request\Http $request,
                \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
                \Magento\Customer\Model\SessionFactory $customerSession,
                \Magento\Customer\Model\Session $customerSessionn
            ) 
        {
            $this->request = $request;
            $this->customerRepository = $customerRepository;
            $this->_customerSession = $customerSession;
            $this->_customerSessionn = $customerSessionn;
            parent::__construct($context);
        }
        public function getCustomerName(){
            $customer = $this->_customerSession->create();
            return $customer->getCustomer()->getName();
        }
        public function isLoggedIn()
        {
            return $this->_customerSessionn->isLoggedIn();
        }
    }
    ?>
    
    Call to your template :
    
    <?php
    $myhelper = $this->helper('Magento\Mytheme\Helper\Data');
    $cname=$myhelper->getCustomerName();
    $isLoggedIn=$myhelper->isLoggedIn();
    
    if ($isLoggedIn):
    	   echo 'login';
    else : 
    	  echo 'not login';
    endif;
    ?>
    Method 2. To check the customer is logged or not on any page of the website:
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    
    $customerSession = $objectManager->get('Magento\Customer\Model\Session');
    
    if($customerSession->isLoggedIn()) {
    
       // customer login action
    
    }
     
  • 5 years-11 months

    How to enable developer mode or enable all error messages in magento 2

    • Category : magento
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    When you’re getting a blank page in Magento 2.x or only error number like Error log record number: (any number), you can enable error reporting in Magento.

    // enable error messages in command line
    php bin/magento deploy:mode:set developer
    
    // enable error messages in file
    Add following code in index.php file :-
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // disable error messages in command line
    php bin/magento maintenance:disable
    Goto app/etc/ and open env.php
    and change current application mode by MAGE_MODE' => 'default' to MAGE_MODE' => 'developer'

    Then,please flush the cache

    you can give a try by renaming local.xml.sample to local.xml under pub/errors
  • 5 years-11 months

    How do I sorting MySQL result by ages from the birthday date

    • Category : php
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    MySQL sorting by age if your birthday like this 1974-05-15, 1974-05-28 (YEAR,MONTH,DATE)

    <?php
    $table_name="YOUR_TABLE_NAME";
    $column_name="birthdate";
    $age_from=15;
    $age_to=45;
    
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    // Perform queries
    
    $sql="SELECT * FROM $table_name WHERE TIMESTAMPDIFF(YEAR, $column_name, CURDATE()) BETWEEN $age_from AND $age_to";
    $result=mysqli_query($con,$sql);
    
    
    // Associative array
    $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
    printf ("%s (%s)\n",$row["id"],$row["name"]);
    
    // Free result set
    mysqli_free_result($result);
    
    mysqli_close($con);
    ?> 
  • 3 years-4 months

    Split time interval in 15 min slot

    • Category : php
    • Location : Chandigarh
    • Posted On :
    • Posted by : Guley

    split time interval in 15 min slot using php

    $array_of_time = array ();
    $start_time    = strtotime ("2013-08-28 17:45:00");
    $end_time      = strtotime ("2013-08-28 22:15:00");
    
    $fifteen_mins  = 15 * 60;
    
    while ($start_time <= $end_time)
    {
       $array_of_time[] = date ("Y-m-d H:i:s", $start_time);
       $start_time  = $fifteen_mins;
    }
    
    print_r ($array_of_time);
     
  • 5 years-11 months

    Warning: Cannot modify header information - headers already sent by ERROR

    • Category : php
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    fix “Headers already sent” error in PHP

    The very quick fix for your problem will be to add

    ob_start();
    as the very first thing in your script if you only need it in this one script. If you need it in all your scripts add it as the very first thing in your header.php file.
  • 5 years-11 months

    How to add Magento Canonical Meta Tag

    • Category : magento
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    Magento SEO - How to add Magento Canonical Meta Tag

    Step 1: Login your Magento Admin Panel

    Step 2: Go to  System > Configuration

    Step 3: In the left menu, choose Catalog >> Search Engine Optimizations
    • In the "Use Canonical Link Meta Tag for Categories" choose "Yes"
    • In the "Use Canonical Link Meta Tag for Products" choose "No"

    Step 4:  Save the changes
     
  • 5 years-11 months

    Magento. Adding page links to the navigation bar

    • Category : magento
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    This tutorial shows how to add page links to Magento navigation bar. In order to add the page links to the navigation bar, you should perform the following steps:

    Step 1: Adding the Subcategory
     
    1. Go to Catalog -> Manage Categories in your Magento Admin.

    2. Select ‘Default Category’ on the left, then click the ‘Add Subcategory’ button:
    3. Fill in the fields:
    4. – Specify your link Name.
    5. – Set ‘Is Active’ to Yes.
    6. – In the URL Key field type the name of your page link in lowercase letters with hyphens instead of spaces (f.e.’About Us’ becomes about-us):
    7. – Scroll down to the bottom of the form and set Include to Navigation Menu to ‘yes’.
    8. – Click ‘Save Category’ button:
     

    Step 2: Creating a Redirect
     

    1. Navigate to Catalog -> URL Rewrite Management.
    2. In the search box of the Request Path column, type in the URL key for the subcategory, and click Search.
    3. When the subcategory is located, click ‘Edit’ to open the record:
    4. NOTE: you will have records for each installed language. You can open the record for each language in the new browser tab.
    5. Open the notepad on your PC and save the following info: the ID path of your URL Rewrite and the Request path:
    6. Now we should delete the URL Rewrite for each language – click the Delete button at the top right.

    Step 3: Adding a new URL rewrite
     
    1. In Catalog -> URL Rewrite Management click Add URL Rewrite button at the top right.

    2. In the Create URL Rewrite field select ‘Custom’.

    3. Fill in the URL Rewrite Information form:

      – Add the ID path for the item which you have noted and saved in the notepad on step 2.2.

      – Add the Request Path for the item which you have noted and saved in the notepad on step 2.2.

      – To fill in the Target Path field, open any navigation menu on your site and copy-paste its url into the field, f.e: http://yourdomain.com/index.php/contacts/ (where ‘contacts’ is the Request Path for the Contact Us navigation page). Replace the Request Path with the new item Request Path which you have noted and saved in the notepad on step 2.2 of this tutorial.

    4. Set Redirect to ‘Permanent 301′.

    5. Click ‘save’ button at the top right:
       

    Step 4: Creating a page
     
    1. Go to CMS -> Pages.

    2. Click ‘Add New Page’.

    3. Specify the page title, the URL key for the page, select All Store Views:

    4. Switch to the Content Tab on the left. Put the html for your page content here.

    5. Switch to the Design Tab on the left. In the layout field choose the desired Page Layout:

    6. Click ‘Save Page’

    Step 5: Clearing your Magento Cache

    This is the end of the tutorial. You’ve learned how to add page links to Magento navigation bar
  • 5 years-11 months

    .htaccess Tricks To Speed Up Your Website

    • Category : htaccess
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    Speed-up your website with these simple .htaccess tricks

    # Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
    RewriteBase /
    
    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
     <filesMatch ".(ico|jpe?g|png|gif|swf)$">
       Header set Cache-Control "public"
     </filesMatch>
     <filesMatch ".(css)$">
       Header set Cache-Control "public"
     </filesMatch>
     <filesMatch ".(js)$">
       Header set Cache-Control "private"
     </filesMatch>
     <filesMatch ".(x?html?|php)$">
       Header set Cache-Control "private, must-revalidate"
     </filesMatch>
    </ifModule>
    # END Cache-Control Headers
    
    <ifmodule mod_expires.c>
    <Filesmatch "\.(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$">
        ExpiresActive on
        ExpiresDefault A2592000
    </Filesmatch>
    </ifmodule>