:::: MENU ::::

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


WooCommerce remove price html

Today we talk about how WooCommerce remove product price HTML.It will remove the price from all the listing pages, product pages, and all other pages too. So let’s start with our example.

add_filter( 'woocommerce_get_price_html' ,  'remove_woocommerce_price_html' , 10, 2 );
function remove_woocommerce_price_html( $price_html ){
          $price_html = "";
          return $price_html;
}
Author: Qaisar Satti
Category: WooCommerce
Last Modified: September 29, 2020


WooCommerce remove add to cart

Today we talk about WooCommerce remove to add to cart to different places. Like listing page, product page. So let’s start with an example.

Lisitng Page

You have to remove the following action to do the trick.

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');

Product Page

You have to remove the following action to do the trick.

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');

For the simple product use the following code.

remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );

Remove add to cart for variable products without removing the variation selectors
Note: It will remove only the add to cart button and the Quantity box.

remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
Author: Qaisar Satti
Category: WooCommerce
Last Modified: September 29, 2020


Magento 2 Get Store Specific Product Name and Description

Today we’ll talk about how we can get store specific products name and descriptions. Like for example, my shop has six store views: DE, AT, IT, ES, FR, and GB. And each store view uses the same set of the product with appropriately translated but I would for example want to view a product in a specific view like DE.

We can do that just by following the instructions below:

protected $productFactory;  
public function __construct(\Magento\Catalog\Model\ProductFactory $productFactory) {      
 $this->productFactory = $productFactory;            
}

now you can use this like,

$product= $this->_productFactory->create()->setStoreId($storeId)->load($pid);

Where $storeIdis your store Id and
$pidis your product Id.

$productName=$product->getName();

Don’t forget to do

di:compile

by

php bin/magento setup:di:compile

and clearcache

php bin/magento c:f

Product Repository

protected $productRepository;
public function __construct(             \Magento\Catalog\Api\ProductRepositoryInterface $productRepository    )
{        
$this->productRepository = $productRepository;    
}

Now, you can use below

$product = $this->productRepository->getById($pid,false,$storeId);

$productName=$product->getName();

You can see It’s arguments invendor\magento\module-catalog\Api\ProductRepositoryInterface.phppublic function getById($productId, $editMode = false, $storeId = null, $forceReload =

Author: Qaisar Satti
Category: Magento 2
Last Modified: September 29, 2020


Pages:1234567...24