Skip to content

Instantly share code, notes, and snippets.

Avatar

Kartik Parmar kartikparmar

  • Mumbai
View GitHub Profile
@kartikparmar
kartikparmar / functions.php
Last active Mar 15, 2021
Changing Min/Max quantity using filters in WooCommerce
View functions.php
<?php
/*
* Changing the minimum quantity to 2 for all the WooCommerce products
*/
function woocommerce_quantity_input_min_callback( $min, $product ) {
$min = 2;
return $min;
}
View cmnq_min_max_quantity.php
<?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 Nov 3, 2017
Saving the value when product is published/updated
View cmnq_min_max_quantity.php
<?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 Nov 17, 2018
Setting minimum and maximum for quantity input args
View cmnq_min_max_quantity.php
<?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 Jan 18, 2019
Validation on Add To Cart button click action
View cmnq_min_max_quantity.php
<?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 Jul 23, 2021
Validation on the Update Cart button on Cart page
View cmnq_min_max_quantity.php
<? 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 Oct 18, 2021
Adding product to cart on website visit
View aaptc_add_product_to_cart.php
<?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 Nov 5, 2017
Adding product to cart on Add to Cart click
View aaptc_add_product_to_cart.php
<?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 Nov 6, 2017
Adding product to cart based on cart total
View aaptc_add_product_to_cart.php
<?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 Nov 30, 2017
Adding dashboard widget
View functions.php
<?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.