Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Created January 15, 2019 05:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kartikparmar/197cd898a0ee50f42cafe065d4405d99 to your computer and use it in GitHub Desktop.
Save kartikparmar/197cd898a0ee50f42cafe065d4405d99 to your computer and use it in GitHub Desktop.
Minimum & Maximum Quantity for Variation
<?php
add_action( 'woocommerce_product_after_variable_attributes', 'wc_variation_settings_fields', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'wc_save_variation_settings_fields', 10, 2 );
add_filter( "woocommerce_available_variation", "woocommerce_available_variation_callback", 10, 3 );
/**
* Adding Min and Max field in variations
*
* @since 1.0
*/
function wc_variation_settings_fields( $loop, $variation_data, $variation ) {
wp_nonce_field('wc_minmax', 'wc_variation_edit');
woocommerce_wp_text_input(
array(
'id' => 'wc_min_quantity_var[' . $variation->ID . ']',
'label' => __( 'Minimum Quantity Var', 'min-max' ),
'value' => get_post_meta( $variation->ID, '_wc_min_qty_product', true ),
'custom_attributes' => array('min' => '1'),
'wrapper_class' => 'wc_min_max_clear'
)
);
woocommerce_wp_text_input(
array(
'id' => 'wc_max_quantity_var[' . $variation->ID . ']',
'label' => __( 'Maximum Quantity Var', 'min-max' ),
'value' => get_post_meta( $variation->ID, '_wc_max_qty_product', true ),
'custom_attributes' => array('min' => '1')
)
);
}
/**
* Saving Min and Max value for variations
*
* @since 1.0
*/
function wc_save_variation_settings_fields( $post_id ) {
if ( empty($_REQUEST['wc_variation_edit']) || ! wp_verify_nonce( $_REQUEST['wc_variation_edit'], 'wc_minmax') ) {
return;
}
if ( isset( $_POST['wc_min_quantity_var'][ $post_id ] ) ) {
update_post_meta( $post_id, '_wc_min_qty_product', $_POST['wc_min_quantity_var'][ $post_id ] );
}
if ( isset( $_POST['wc_max_quantity_var'][ $post_id ] ) ) {
update_post_meta( $post_id, '_wc_max_qty_product', $_POST['wc_max_quantity_var'][ $post_id ] );
}
}
/**
* Applying Min and Max value on front end.
*
* @since 1.0
*/
function woocommerce_available_variation_callback( $variation_array, $this_something, $variation ) {
$parent_id = $variation->get_parent_id();
$variation_id = $variation->get_id();
$pro_min = get_post_meta( $parent_id, "_wc_min_qty_product", true );
$pro_max = get_post_meta( $parent_id, "_wc_max_qty_product", true );
if ( $pro_min == "" ){ // if parent min is not set then check for variation min
$min = wc_get_product_min_limit( $variation_id );
if ( $min != "" ){
$variation_array['min_qty'] = $min;
}
}else{
$variation_array['min_qty'] = $pro_min;
}
if ( $pro_max == "" ){ // if parent max is not set then check for variation max
$max = wc_get_product_max_limit( $variation_id );
if ( $max != "" ){
$variation_array['max_qty'] = $max;
}
} else {
$variation_array['max_qty'] = $pro_max;
}
return $variation_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment