vikas
Vikas Pandey@vikas

I am the best

  • 5 years-11 months

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

    • Category : php
    • Location : Chandigarh, India
    • Posted On : Sep 09, 2018
    • 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 : Aug 08, 2018
    • 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 : Jul 26, 2018
    • 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 : May 29, 2018
    • 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 : May 29, 2018
    • 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();
    }
  • 5 years-11 months

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

    • Category : magento
    • Location : Chandigarh, India
    • Posted On : Apr 04, 2018
    • 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 will my javascript function executed after the previous ajax call complete ?

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On : Mar 15, 2018
    • 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 : Mar 13, 2018
    • 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');
    }
     
  • 5 years-11 months

    woocommerce backend add items in new or edit orders

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

    If you need to change item product prices in Woocommerce Backend Order. Please use the following hook or code

    add_action( 'woocommerce_ajax_add_order_item_meta', 'VP_set_item_quantity', 99, 3 );
    function VP_set_item_quantity( $item_id, $item, $order ) {
        foreach ( $order->get_items() as $order_item_id => $order_item_data ) {
            if ( $order_item_id == $item_id ) {
    
    
                $order_item_data->set_subtotal('20.22');
                $order_item_data->set_total(20.22);
                $order->apply_changes();
                $order->save();
    				
    		}
        }
    }
     
  • 5 years-11 months

    How to change user role after wordpress registration

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

    This action hook allows you to access data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.

    add_action( 'user_register', 'eddpremium_register_fields_save');
    add_action( 'profile_update', 'eddpremium_register_fields_save');
    
    function eddpremium_register_fields_save($user_id) {
    	$u = new WP_User($user_id);
    
    	// Remove role
    	$u->remove_role( 'subscriber' );
    
    	// Add role
    	$u->add_role( 'author' );
    }