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 Hide the WordPress Update Notification in dashboard

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

    To disable this update notification, there is no switch in the settings menu or anywhere else in the admin dashboard. To prevent WordPress from showing these information snippets on top of your WordPress dashboard, you shall need to add a PHP code snippet to your functions.php file.

    add_filter('pre_site_transient_update_core','__return_null');
    add_filter('pre_site_transient_update_plugins','__return_null');
    add_filter('pre_site_transient_update_themes','__return_null');
     
  • 5 years-11 months

    How to create add to cart with AJAX on single and variable products programmatically

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

    default WooCommerce if the user clicks on the add to cart button, that item is added to the cart. if you want to add custom add to cart button please remove default "add to cart" button

    // custom template
    <button type="submit" class="custom_cart_button">add to cart</button>
    <script type="text/javascript">
    jQuery(document).ready(function() {
    var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        jQuery('.custom_cart_button').submit(function(e) {
            jQuery.ajax({
                url: ajaxurl,
                type: "post",
                data: {				
    		action : 'post_product_variations19'
    	    },
                success: function(data) {   
                	console.log(data);
                }
            });
    	return false;
        });
    });
    </script>
    
    
    // function.php
    add_action( 'wp_ajax_nopriv_post_product_variations19', 'post_product_variations_func19' );
    add_action( 'wp_ajax_post_product_variations19', 'post_product_variations_func19' );
    function post_product_variations_func19() {
    
        global $woocommerce;
    
        $product_id = 364; // your product id
    
        $found = false;
    
        //check if product already in cart
        if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
            foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id ) {
                	//$calc_qty = $values['quantity'] < 1 ? 1 : $values['quantity']+1;
                	//$woocommerce->cart->set_quantity( $cart_item_key, $calc_qty, true );
                    $found = true;
                }
            }
            // if product not found, add it
            //if ( ! $found )
                $woocommerce->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            $woocommerce->cart->add_to_cart( $product_id );
        }
    	
    wp_die();
    }
  • 2 years-11 months

    How to change format of Phone number in input text as you type?

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

    I'm currently displaying phone numbers like 18003336523. However, I need the number to be formatted in a more human-readable form, for example: 1-800-333-6523.

    <script>
    
    jQuery('.wpcf7-tel').keydown(function (e) {
      var key = e.charCode || e.keyCode || 0;
      $phone = jQuery(this);
    
      // Auto-format- do not expose the mask as the user begins to type
      if (key !== 8 && key !== 9) {
          
    console.log($phone.val().length);
       if ($phone.val().length === 1) {
        $phone.val($phone.val() + '-');
       }
       if ($phone.val().length === 5) {
        $phone.val($phone.val() + '-');
       }   
       if ($phone.val().length === 9) {
        $phone.val($phone.val() + '-');
       }
       if ($phone.val().length === 15) {
        return false;
       }
      }
    
      // Allow numeric (and tab, backspace, delete) keys only
      return (key == 8 || 
        key == 9 ||
        key == 46 ||
        (key >= 48 && key <= 57) ||
        (key >= 96 && key <= 105)); 
     })
     
     .bind('focus click', function () {
      $phone = jQuery(this);
      
      if ($phone.val().length === 0) {
       //$phone.val('(');
      }
      else {
       var val = $phone.val();
       $phone.val('').val(val); // Ensure cursor remains at the end
      }
     })
     
     .blur(function () {
      $phone = jQuery(this);
      
      if ($phone.val() === '(') {
       $phone.val('');
      }
     });
    </script>
     
  • 2 years-11 months

    How to include simple pagination in a Wordpress Custom Post Type Query

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Mohit

    For a WordPress blog, pagination alludes to showing a predetermined number of posts per page. Pagination permits clients the capacity to explore substantially less demanding and more profound into the chronicles. In this post, I will demonstrate that my past instructional exercise about including Pagination without a module in WordPress, can likewise be utilized on Custom Post Types with no alterations. I will likewise demonstrate the means I took to test my past instructional exercise's pagination code and demonstrate that it is as yet substantial for custom post writes.

    <?php
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;          
    $args = array( 'paged'=> $paged,'post_type' => 'product','orderby' => 'post_date','order'=> 'ASC','posts_per_page' => 10,'post_status'=>'publish');        
    
    $query = new WP_Query($args);
    if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post();
        $id=get_the_ID();
    ?> 
          
    <a href="<?php the_permalink() ?>" class="post-title">
        <h2><?php the_title();?></h2>
    </a>
    <div class="post-content"> <?php echo '<p>'.substr(get_the_content(),0,190).'...</p>'; ?> </div>
    
    <?php
    endwhile; endif;
    ?>
     <?php
     echo '<div class="blogPager">';
     echo '<ul class="pagination"> ';
     if( $paged-1 >= 1){
        echo ' <li class="'.($i == $paged ? 'active ' : '').'page button"><a   href="'.get_pagenum_link(($paged-1 > 0 ? $paged-1 : 1)).'"  > Previous</a></li>';
     }
    
    if($query->max_num_pages > 10) {
        for($i=1;$i<=$query->max_num_pages;$i++){
            echo '<li class="'.($i == $paged ? 'active ' : '').'page button"><a  href="'.
           get_pagenum_link($i).'">'.$i.'</a></li>';
    
        }
    }
    if( $query->max_num_pages > $paged  ) {
        echo '<li class="'.($i == $paged ? 'active ' : '').'page button"> <a   href="'.get_pagenum_link(($paged+1 <= $query->max_num_pages ? $paged+1 :
                $query->max_num_pages)).'">Next </a></li>';
    }
    echo '</ul> ';
    echo '</div> ';
    wp_reset_postdata();
    ?>
     
  • 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-7 months

    Load more content with jQuery

    • Category : jquery
    • Location : Unknown
    • Posted On :
    • Posted by : Sunil1521543962

    Load more content with jQuery

    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    
    
    <a href="#" id="loadMore">Load More</a>
    
    <p class="totop"> 
        <a href="#top">Back to top</a> 
    </p>


     

    1 Answers

    View More

    sunil1521543962 image

    Name: sunil1521543962

    Experiance: 5 years-7 months

    answered
    Mar 28, 2018 at 06:39

    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
    
    
    <a href="#" id="loadMore">Load More</a>
    
    <p class="totop"> 
        <a href="#top">Back to top</a> 
    </p>
    
    <style>
    body {
        background-color: #f6f6f6;
        width: 400px;
        margin: 20px auto;
        font: normal 13px/100% sans-serif;
        color: #444;
    }
    div {
        display:none;
        padding: 10px;
        border-width: 0 1px 1px 0;
        border-style: solid;
        border-color: #fff;
        box-shadow: 0 1px 1px #ccc;
        margin-bottom: 5px;
        background-color: #f1f1f1;
    }
    .totop {
        position: fixed;
        bottom: 10px;
        right: 20px;
    }
    .totop a {
        display: none;
    }
    a, a:visited {
        color: #33739E;
        text-decoration: none;
        display: block;
        margin: 10px 0;
    }
    a:hover {
        text-decoration: none;
    }
    #loadMore {
        padding: 10px;
        text-align: center;
        background-color: #33739E;
        color: #fff;
        border-width: 0 1px 1px 0;
        border-style: solid;
        border-color: #fff;
        box-shadow: 0 1px 1px #ccc;
        transition: all 600ms ease-in-out;
        -webkit-transition: all 600ms ease-in-out;
        -moz-transition: all 600ms ease-in-out;
        -o-transition: all 600ms ease-in-out;
    }
    #loadMore:hover {
        background-color: #fff;
        color: #33739E;
    }
    </style>
    <script>
    /*
    	Load more content with jQuery - May 21, 2013
    	(c) 2013 @ElmahdiMahmoud
    */   
    
    $(function () {
        $("div").slice(0, 4).show();
        $("#loadMore").on('click', function (e) {
            e.preventDefault();
            $("div:hidden").slice(0, 4).slideDown();
            if ($("div:hidden").length == 0) {
                $("#load").fadeOut('slow');
            }
            $('html,body').animate({
                scrollTop: $(this).offset().top
            }, 1500);
        });
    });
    
    $('a[href=#top]').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 600);
        return false;
    });
    
    $(window).scroll(function () {
        if ($(this).scrollTop() > 50) {
            $('.totop a').fadeIn();
        } else {
            $('.totop a').fadeOut();
        }
    });
    </script>
     
  • 5 years-7 months

    How To Easily Add Product Variants To Your Clickfunnels 2-Step Order Form?

    • Category : php
    • Location : Unknown
    • Posted On :
    • Posted by : Sunil1521543962

    How To Easily Add Product Variants To Your Clickfunnels 2-Step Order Form?

    <script>
    var option_name = 'product_color';
    var options = ['Red', 'Blue', 'Green'];
    var label = 'Pick A Color';
    
    $('.o2step_step1').prepend('<hr style="margin-top: 3px;margin-bottom: 15px;">');
    select = $('<select type="custom_type" data-custom-type="'+option_name+'" name="custom_type" class="elS1Fullname elInput elInput100 elAlign_left elInputSmall elInputStyl0 elInputBG1 elInputBR5 elInputI0 elInputIBlack elInputIRight required1 elInputStyle1 elInputSmall elInputBG2 garlic-auto-save" data-type="extra" style="margin-top: 0px !important;margin-bottom: 10px !important; border-color: rgb(74, 137, 32); border-width: 3px;"></select>');
    for(option in options) {
       select.append('<option>'+options[option]+'</option>');
    }
    $('.o2step_step1').prepend(select);
    $('.o2step_step1').prepend('<label>'+label+'</label>');
    </script>
     

    1 Answers

    View More

    sunil1521543962 image

    Name: sunil1521543962

    Experiance: 5 years-7 months

    answered
    Mar 23, 2018 at 02:31

    In this short how-to tutorial I'm going to discuss a little trick you can implement on your order form(s) to add variants support to them, (eg. product Color, Shape, Size...etc), without having to add multiple products as long as the different variants have the same price.

    So let's get started:

    1. On your Funnel Step under Settings > Tracking Code :


       

      <script>
      var option_name = 'product_color';
      var options = ['Red', 'Blue', 'Green'];
      var label = 'Pick A Color';
      
      $('.o2step_step1').prepend('<hr style="margin-top: 3px;margin-bottom: 15px;">');
      select = $('<select type="custom_type" data-custom-type="' option_name '" name="custom_type" class="elS1Fullname elInput elInput100 elAlign_left elInputSmall elInputStyl0 elInputBG1 elInputBR5 elInputI0 elInputIBlack elInputIRight required1 elInputStyle1 elInputSmall elInputBG2 garlic-auto-save" data-type="extra" style="margin-top: 0px !important;margin-bottom: 10px !important; border-color: rgb(74, 137, 32); border-width: 3px;"></select>');
      for(option in options) {
         select.append('<option>' options[option] '</option>');
      }
      $('.o2step_step1').prepend(select);
      $('.o2step_step1').prepend('<label>' label '</label>');
      </script>