:::: MENU ::::

PrestaShop get current category information

Today we talk about how PrestaShop get current category information. You can use this code in hook related to the category page or controller and any other class. In this example, it will be shown the category name and category description, and other information. So let’s start with our example.

$categoryId = (int)Tools::getValue('id_category'); // get current category id
$categoryData = new Category ($categoryId,Context::getContext()->language->id); // get category object with id
$name    = $categoryData->name; // get category name
$description    = $categoryData->description; // get category description
Author: Qaisar Satti
Category: Prestashop
Last Modified: October 12, 2020


PrestaShop get current product information

Today we talk about how PrestaShop get current product information. You can use this code in hook related to the product page or controller and any other class. In this example, it will be shown the product name and product link, and other information. So let’s start with our example.

$productId = (int)Tools::getValue('id_product'); // get current product id
$productData = new Product($productId); // get product object with id
$name    = $productData->getProductName(); // get product name
$link    = $productData->getLink(); // get product link
Author: Qaisar Satti
Category: Prestashop
Last Modified: October 12, 2020


Prestashop session and cookies

Today we talk about how to use Prestashop session and cookies. The first thing you must know Prestashop did not use the session it is using the cookie for storing the data So let talk about the cookies. In this tutorial, we focus on set cookies, get cookies, and unset the cookies. So let’s start it.

You can get the cookie variable by following.

in the Controller file.

 $cookieObject = $this->context->cookie;

Other files

 $cookieObject = Context::getContext()->cookie;

Set Cookies

     $context->cookie->__set('simplaedata', 'abc');

To store an array in cookies you need to serialize data. an example is below.

     $array = array('a','b','c');
     $context->cookie->__set('simplaedata', serialize($array));

Get Cookies

    $context->cookie->simplaedata;

UnSet Cookies

   $context->cookie->__unset('simplaedata');
Author: Qaisar Satti
Category: Prestashop
Last Modified: October 12, 2020


WooCommerce get current user information

Today we talk WooCommerce get current user information. In this tutorial, we will get the following information of the current user, user name, user email, user id, and user group. So let’s start with our example.

 $user = wp_get_current_user(); // get current user
 $id = $user->ID; // get user id
 $name = $user->user_name; // get user name
 $email = $user->email; //get user email
 $role = $user->roles[0]; // get user role
Author: Qaisar Satti
Category: WooCommerce
Last Modified: September 30, 2020


WooCommerce remove tax for user

Today we talk about how in WooCommerce remove tax for user. We will remove the tax on basis of the user id or user groups. Now, let’s start with our example.

add_filter( 'wc_tax_enabled',  'custom_enable_tax' , 1, 2 );
function custom_enable_tax(){
   $user = wp_get_current_user(); //get current user
   if( '20' == $user->ID ){
      return false;//remove tax for current user on basis of id
   }
   if( 'customer' == $user->roles[0] ){
     return false; //remove tax for current user on basis of user group
   }
  return true,
}
Author: Qaisar Satti
Category: WooCommerce
Last Modified: September 30, 2020


WooCommere remove tax for product

Today we talk about WooCommerce remove tax for product. Sometime in the scenario, you have to remove the tax bases on some condition. In the below example, I will explain how can you achieve this functionality.

add_filter( 'woocommerce_product_is_taxable' , 'product_product_custom_tax_status' , 10 , 2);
function product_product_custom_tax_status( $product_status , $product ){
    //return true to enable Tax and false to disable tax
    if( '20' == $product->get_id() ){
        return false;
    }else {
       return true;
    }
}
Author: Qaisar Satti
Category: WooCommerce
Last Modified: September 30, 2020


WooCommerce apply discount programmatically

Today we talk about how WooCommerce apply discount programmatically. Sometimes you need to add the discount bases on some condition. Follow is the snippets how can you achieve the functionality.

Simple Product

To apply the discount on simple products use the following code.

add_filter( 'woocommerce_product_get_price' , 'products_custom_price' , 5, 2 );
function products_custom_price( $price, $product ){
      $discount = 10;
      $product_id = $product->get_id();
      if( $product_id == '20' ){
          return $price - $discount;
      }
}

Variable Product

To apply the discount on variable products use the following code.

add_filter( 'woocommerce_product_variation_get_price' , 'variation_custom_price' , 99, 2 );
function variation_custom_price( $price, $variation ){
        //Apply Discount by matching the parent Product
       $product = wc_get_product($variation->get_parent_id());
       if( '20' == $product->get_id() ){
             return $price - $discount;
       }
       //Apply Discount by matching the Product Variation
       if( '20' == $variation->get_id() ){
             return $price - $discount;
       }
}
Author: Qaisar Satti
Category: WooCommerce
Last Modified: September 29, 2020


Pages:1234567...25