Skip to content

Instantly share code, notes, and snippets.

View kartikparmar's full-sized avatar

Kartik Parmar kartikparmar

  • Mumbai
View GitHub Profile
@kartikparmar
kartikparmar / functions.php
Last active August 30, 2019 18:37
Multiplying the rate if the any product in the cart falls under the specified categories.
<?php
add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 );
function overwrite_shipping_cost($rates, $package ) {
global $woocommerce;
if ( get_field( 'multiplier', 'option' ) ) :
$multiplier = get_field( 'multiplier', 'option' );
else :
$multiplier = 1;
@kartikparmar
kartikparmar / functions.php
Created August 30, 2019 08:10
Automatically adding the gift product to cart when specified criteria match
<?php
/*
* Automatically adding gift product to the cart
* Click on Add to Cart of Product A(under category cricket bat) will automatically add Gift A to cart
* Click on Add to Cart of Product B(under category cricket bat) will automatically add Gift A to cart
* Click on Add to Cart of Gift A will not automatically add Gift A to cart
*/
function aaptc_add_product_to_cart( $item_key, $product_id ) {
@kartikparmar
kartikparmar / functions.php
Last active August 30, 2019 07:32
Automatically adding multiple products to the cart based on the multiple categories.
<?php
/*
* Automatically adding multiple products to the cart based on the multiple categories.
*/
function aaptc_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = array( 17, 18 ); // Category ids Hoodies & T-Shirts
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' ); // Getting assigned categories of product which is being added to cart
@kartikparmar
kartikparmar / quantity-input.php
Created August 22, 2019 14:12
Customizing the validation message when custom tries to add to cart with more than allowed quantity.
<?php
/**
* Product quantity inputs
*
* This template can be overridden by copying it to yourtheme/woocommerce/global/quantity-input.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
@kartikparmar
kartikparmar / functions.php
Created June 27, 2019 11:50
Return all category ids of WooCommerce
<?php
function wcget_category_ids(){
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
@kartikparmar
kartikparmar / functions.php
Last active April 8, 2020 00:48
Removing free product when cart doesn't contains product of particular category.
<?php
/**
* Removing free product from cart when cart doensn't contains product of perticular category.
*/
function remove_product_from_cart() {
// Run only in the Cart or Checkout Page
if ( is_cart() || is_checkout() ) {
@kartikparmar
kartikparmar / functions.php
Created January 30, 2019 08:47
Remove WooCommerce Product of specific category from shop page
<?php
function exclude_product_of_specific_category( $q ) {
if ( is_shop() ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
@kartikparmar
kartikparmar / functions.php
Created December 1, 2018 07:25
Automatically add product to cart when cart total reaches to certain amount. Once customer removes the free product from cart then do not add again.
<?php
/*
* Automatically adding the product to the cart when cart total amount reach to $500.
* Also, when customer removes the free product from cart then do not add free product again to cart automatically.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$free_product_id = 1514; // Product Id of the free product which will get added to cart
$list_of_removed_product = array(); // In this we will add all the removed product ids
@kartikparmar
kartikparmar / functions.php
Last active November 19, 2018 09:19
Automatiacally adding free product to cart when cart total reaches to certain amount and number of product is equal to a particular number
<?php
/*
* Automatically adding the product to the cart when cart total amount reach to $500 and number of produc added in cart is 3 or more.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 500;
$number_of_product = 3;
$number_of_product_in_cart = WC()->cart->get_cart_contents_count(); // Number of products in cart.
@kartikparmar
kartikparmar / functions.php
Created November 6, 2018 05:30
Automatically adding the product to the cart for only specific user roles.
<?php
/*
* Automatically adding the product to the cart for only specific user roles.
*/
function aaptc_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = 123; // cricket bat category id
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
$user = wp_get_current_user(); // This will fetch current user's role
$allowed_roles = array( 'booking_agent', 'perfecth' ); // These are the list of allowed user role.