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 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

    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>
  • 5 years-11 months

    How will my javascript function executed after the previous ajax call complete ?

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

    You can specify a function identifier in a custom property in your .ajax() call and use the value of that in your ajaxComplete() callback.

    For example, get any ajax property while making your .ajax() call:
    function foo() {
        $.ajax({
            url: 'my-page.php',
        });
    }
    
    And then, use any of ajax property to ajaxComplete() like this:
    $('#my-unique-id').ajaxComplete(function(e, xhr, settings) { // run after all ajax complete 
        if (settings.url == 'my-page.php') { // write condition according to your requirement
            // execute you code
        } else {
            // execute you code
        }
    });
     
  • 5 years-11 months

    How to Remove Items from the WordPress Toolbar new from wp admin area

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

    According to me if you want to remove all NEW link in wp admin area please follow this code

    add_action( 'admin_bar_menu', 'remove_wp_nodes', 999 );
    
    function remove_wp_nodes() 
    {
        global $wp_admin_bar;   
        $wp_admin_bar->remove_node('new-content');
    }