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 March 15, 2021 11:54
Changing Min/Max quantity using filters in WooCommerce
<?php
/*
* Changing the minimum quantity to 2 for all the WooCommerce products
*/
function woocommerce_quantity_input_min_callback( $min, $product ) {
$min = 2;
return $min;
}
<?php
function wc_qty_add_product_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_wc_min_qty_product',
'label' => __( 'Minimum Quantity', 'woocommerce-max-quantity' ),
'placeholder' => '',
'desc_tip' => 'true',
@kartikparmar
kartikparmar / cmnq_min_max_quantity.php
Last active November 3, 2017 07:13
Saving the value when product is published/updated
<?php
/*
* This function will save the value set to Minimum Quantity and Maximum Quantity options
* into _wc_min_qty_product and _wc_max_qty_product meta keys respectively
*/
function wc_qty_save_product_field( $post_id ) {
$val_min = trim( get_post_meta( $post_id, '_wc_min_qty_product', true ) );
$new_min = sanitize_text_field( $_POST['_wc_min_qty_product'] );
@kartikparmar
kartikparmar / cmnq_min_max_quantity.php
Last active November 17, 2018 14:36
Setting minimum and maximum for quantity input args
<?php
/*
* Setting minimum and maximum for quantity input args.
*/
function wc_qty_input_args( $args, $product ) {
$product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
$product_min = wc_get_product_min_limit( $product_id );
@kartikparmar
kartikparmar / cmnq_min_max_quantity.php
Last active January 18, 2019 09:18
Validation on Add To Cart button click action
<?php
/*
* Validating the quantity on add to cart action with the quantity of the same product available in the cart.
*/
function wc_qty_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
$product_min = wc_get_product_min_limit( $product_id );
$product_max = wc_get_product_max_limit( $product_id );
@kartikparmar
kartikparmar / cmnq_min_max_quantity.php
Last active July 23, 2021 09:29
Validation on the Update Cart button on Cart page
<? php
/*
* Get the total quantity of the product available in the cart.
*/
function wc_qty_get_cart_qty( $product_id , $cart_item_key = '' ) {
global $woocommerce;
$running_qty = 0; // iniializing quantity to 0
// search the cart for the product in and calculate quantity.
foreach($woocommerce->cart->get_cart() as $other_cart_item_keys => $values ) {
@kartikparmar
kartikparmar / aaptc_add_product_to_cart.php
Last active October 18, 2021 09:05
Adding product to cart on website visit
<?php
/*
* Automatically adding the product to the cart.
*/
function aaptc_add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 12986; // Product Id of the free product which will get added to cart
$found = false;
@kartikparmar
kartikparmar / aaptc_add_product_to_cart.php
Created November 5, 2017 18:31
Adding product to cart on Add to Cart click
<?php
/*
* Automatically adding the product to the cart.
*/
function aaptc_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = 123; // cricket bat category id
@kartikparmar
kartikparmar / aaptc_add_product_to_cart.php
Created November 6, 2017 06:26
Adding product to cart based on cart total
<?php
/*
* Automatically adding the product to the cart when cart total amount reach to $500.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 500;
@kartikparmar
kartikparmar / functions.php
Created November 30, 2017 07:31
Adding dashboard widget
<?php
/**
* Add a widget to the dashboard.
*
* This function is hooked into the 'wp_dashboard_setup' action below.
*/
function wc_orders_dashboard_widgets() {
wp_add_dashboard_widget(
'wc_order_widget_id', // Widget slug.