Plugin Directory

Changeset 2543347


Ignore:
Timestamp:
06/06/2021 06:03:13 PM (4 years ago)
Author:
wpashokg
Message:

Bug Fix: If quantity item manually changed in the product page / cart page and if the changed quantity voids the minimum / maximum order quantity rule / the quantity increment rule then it throws error which was not earlier.

Location:
woo-min-max-quantity-limit
Files:
3 edited
4 copied

Legend:

Unmodified
Added
Removed
  • woo-min-max-quantity-limit/tags/2.1.6/changelog.txt

    r2512073 r2543347  
    11*** WC Minimum & Maximum Quantity  Changelog ***
    2 2021.04.09 - version 2.1.4
     22021.06.06 - version 2.1.6
     3 * Bug Fix: If quantity item manually changed in the product page / cart page and if the changed quantity voids the minimum / maximum order quantity rule / the quantity increment rule then it throws error which was not earlier.
     42021.04.09 - version 2.1.5
    35 * Added feature to do increments based on the increment set by admin
    462021.01.01 - version 2.1.4
  • woo-min-max-quantity-limit/tags/2.1.6/readme.txt

    r2512073 r2543347  
    44Tags: woocommerce, minimum, maximum, quantity, minimum purchase, maximum purchase, purchase
    55Requires at least: 3.3.1
    6 Tested up to: 5.7
    7 Stable tag: 2.1.5
    8 WC tested up to: 5.1.0
     6Tested up to: 5.7.2
     7Stable tag: 2.1.6
     8WC tested up to: 5.3.0
    99License: GNU General Public License v3.0
    1010License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    3030 * Select a particular product from the product list for which you need to enable this feature.
    3131 * Enable this feature and add the minimum and maximum allowed quantity in the respective boxes
     32 * Setting up the increment, if the increment by is set other than one, the minimum order quantity should be the value of the increment set.
     33 * Setting custom error notice / messages, in the admin panel go to Settings --> WooCommerce Minimum and Maximum Quantity Limit, there you will find 2 text area, there you can set the custom error message which will override the default error message in the product and cart page
    3234 
    3335== Frequently Asked Questions ==
     
    4244
    4345== Changelog ==
     46= 2.1.6 =
     47Bug Fix: If quantity item manually changed in the product page / cart page and if the changed quantity voids the minimum / maximum order quantity rule / the quantity increment rule then it throws error which was not earlier.
    4448= 2.1.5 =
    4549Added feature to do increments based on the increment set by admin
  • woo-min-max-quantity-limit/tags/2.1.6/woo-min-max-quantity.php

    r2512073 r2543347  
    11<?php
    22/*
    3 Plugin Name: Woocommerce Minimum and Maximum Quantity
    4 Plugin URI:  http://ashokg.in/
    5 Description: Allow the site admin to enable the feature of minimum and maximum purchase of a particular product in each product.
    6 Version: 2.1.5
    7 Author: Ashok G
    8 Text Domain: wcmmax
    9 Author URI: https://ashokg.in
    10 WC tested up to: 5.1.0
    11 Copyright: © 2020 Ashok G.
    12 License: GNU General Public License v3.0
    13 License URI: http://www.gnu.org/licenses/gpl-3.0.html
    14 */
     3  Plugin Name: Woocommerce Minimum and Maximum Quantity
     4  Plugin URI:  http://ashokg.in/
     5  Description: Allow the site admin to enable the feature of minimum and maximum purchase of a particular product in each product.
     6  Version: 2.1.6
     7  Author: Ashok G
     8  Text Domain: wcmmax
     9  Author URI: https://ashokg.in
     10  WC tested up to: 5.3.0
     11  Copyright: © 2020 Ashok G.
     12  License: GNU General Public License v3.0
     13  License URI: http://www.gnu.org/licenses/gpl-3.0.html
     14 */
    1515
    1616
    1717add_action('add_meta_boxes', 'wc_mmax_meta_box_create');
    1818add_action('save_post', 'wc_mmax_save_meta_box');
    19 function wc_mmax_meta_box_create()
    20 {
     19
     20function wc_mmax_meta_box_create() {
    2121    add_meta_box('wc_mmax_enable', __('Min Max Quantity', 'wcmmax'), 'wc_mmax_meta_box', 'product', 'side');
    2222}
    2323
    24 function wc_mmax_meta_box($post)
    25 {
     24function wc_mmax_meta_box($post) {
    2625    wp_nonce_field('wc_mmax_cst_prd_nonce', 'wc_mmax_cst_prd_nonce');
    27    
     26
    2827    echo '<table>';
    2928    echo '<tr><td><label for="_wc_mmax_prd_opt_enable">' . __('Enable', 'wcmmax') . '</label>';
     
    4645    echo '<td><input type="number" id="_wc_mmax_increment" class="short" name="_wc_mmax_increment" value="' . $wc_mmax_increment . '" /></td>';
    4746    echo '</tr></table>';
    48    
    49 }
    50 
    51 function wc_mmax_save_meta_box($post_id)
    52 {
     47}
     48
     49function wc_mmax_save_meta_box($post_id) {
    5350    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    5451        return;
     
    5653        return;
    5754    update_post_meta($post_id, '_wc_mmax_prd_opt_enable', (int) $_POST['_wc_mmax_prd_opt_enable']);
    58     update_post_meta($post_id, '_wc_mmax_max',(int) $_POST['_wc_mmax_max']);
     55    update_post_meta($post_id, '_wc_mmax_max', (int) $_POST['_wc_mmax_max']);
    5956    update_post_meta($post_id, '_wc_mmax_min', (int) $_POST['_wc_mmax_min']);
    6057    update_post_meta($post_id, '_wc_mmax_increment', (int) $_POST['_wc_mmax_increment']);
    6158}
    6259
    63 
    64 
    65  
    66 /** Functions For Custom Option Page for Plugin **/
     60/** Functions For Custom Option Page for Plugin * */
    6761function _wcmmax_options_register_settings() {
    68    add_option( '_wcmmax_options_option_name', 'Settings.');
    69    register_setting( '_wcmmax_options_group', '_wcmmax_options_option_name', '_wcmmax_options_callback' );
    70 }
    71 add_action( 'admin_init', '_wcmmax_options_register_settings' );
     62    add_option('_wcmmax_options_option_name', '');
     63    add_option('_wcmmax_cart_error_message', '');
     64    register_setting('_wcmmax_options_group', '_wcmmax_options_option_name', '_wcmmax_options_callback');
     65    register_setting('_wcmmax_options_group', '_wcmmax_cart_error_message', '_wcmmax_options_callback');
     66}
     67
     68add_action('admin_init', '_wcmmax_options_register_settings');
    7269
    7370function _wcmmax_register_options_page() {
    74   add_options_page('WooCommerce Minimum & Maximum Quantity Limit Settings', 'WooCommerce Minimum & Maximum Quantity Limit', 'manage_options', '_wcmmax_', '_wcmmax_options_page');
    75 }
     71    add_options_page('WooCommerce Minimum & Maximum Quantity Limit Settings', 'WooCommerce Minimum & Maximum Quantity Limit', 'manage_options', '_wcmmax_', '_wcmmax_options_page');
     72}
     73
    7674add_action('admin_menu', '_wcmmax_register_options_page');
    7775
    78 function _wcmmax_options_page()
    79 {
    80 ?>
    81   <div>
    82  
    83   <h2>WooCommerce Minimum & Maximum Quantity Limit Settings</h2>
    84   <form method="post" action="options.php">
    85   <?php settings_fields( '_wcmmax_options_group' ); ?>
    86  
    87   <table>
    88   <tr valign="top">
    89   <th scope="row"><label for="_wcmmax_options_option_name">Custom alert message for maximum Quantity limit </label></th>
    90   </tr>
    91   <tr>
    92       <td><textarea cols="60" rows="5" id="_wcmmax_options_option_name" name="_wcmmax_options_option_name" ><?php echo get_option('_wcmmax_options_option_name'); ?> </textarea></td>
    93   </tr>
    94   </table>
    95   <?php  submit_button(); ?>
    96   </form>
    97   </div>
    98 <?php
    99 }
    100 
    101   /** Functions For Custom Option Page for Plugin **/
    102 
    103 /*Function to manipulate custom minimum and maximum purchase*/
     76function _wcmmax_options_page() {
     77    ?>
     78    <div>
     79
     80        <h2>WooCommerce Minimum & Maximum Quantity Limit Settings</h2>
     81        <form method="post" action="options.php">
     82            <?php settings_fields('_wcmmax_options_group'); ?>
     83
     84            <table>
     85                <tr valign="top">
     86                    <th scope="row"><label for="_wcmmax_options_option_name">Custom alert message for maximum Quantity limit </label></th>
     87                </tr>
     88                <tr>
     89                    <td><textarea cols="60" rows="5" id="_wcmmax_options_option_name" name="_wcmmax_options_option_name" ><?php echo get_option('_wcmmax_options_option_name'); ?> </textarea></td>
     90                </tr>
     91                <tr valign="top">
     92                    <th scope="row"><label for="_wcmmax_cart_error_message">Cart Page Custom alert message for quantity update voiding the increment rules </label></th>
     93                </tr>
     94                <tr>
     95                    <td><textarea cols="60" rows="5" id="_wcmmax_cart_error_message" placeholder="Enter Custom Error Message to be shown in the cart page for voiding the product quantity increment rules" name="_wcmmax_cart_error_message" ><?php echo get_option('_wcmmax_cart_error_message'); ?> </textarea></td>
     96                </tr>
     97
     98            </table>
     99            <?php submit_button(); ?>
     100        </form>
     101    </div>
     102    <?php
     103}
     104
     105/** Functions For Custom Option Page for Plugin * */
     106/* Function to manipulate custom minimum and maximum purchase */
    104107add_filter('woocommerce_quantity_input_args', 'wc_mmax_quantity_input_args', 10, 2);
    105 function wc_mmax_quantity_input_args($args, $product)
    106 {
    107 if(function_exists('icl_object_id')) {
    108     $default_language = wpml_get_default_language();
    109     $prodid = icl_object_id($product->get_id(), 'product', true, $default_language);
    110 } else {
    111     $prodid = $product->get_id();
    112 }
     108
     109function wc_mmax_quantity_input_args($args, $product) {
     110    if (function_exists('icl_object_id')) {
     111        $default_language = wpml_get_default_language();
     112        $prodid = icl_object_id($product->get_id(), 'product', true, $default_language);
     113    } else {
     114        $prodid = $product->get_id();
     115    }
    113116    $mmaxEnable = get_post_meta($prodid, '_wc_mmax_prd_opt_enable', true);
    114     $minQty     = get_post_meta($prodid, '_wc_mmax_min', true);
    115     $maxQty     = get_post_meta($prodid, '_wc_mmax_max', true);
    116     $incrementbyQty     = get_post_meta($prodid, '_wc_mmax_increment', true);
     117    $minQty = get_post_meta($prodid, '_wc_mmax_min', true);
     118    $maxQty = get_post_meta($prodid, '_wc_mmax_max', true);
     119    $incrementbyQty = get_post_meta($prodid, '_wc_mmax_increment', true);
    117120    if ($minQty > 0 && $maxQty > 0 && $mmaxEnable == 1) {
    118121        $args['min_value'] = $minQty; // Starting value
     
    120123        $args['step'] = $incrementbyQty; // Increase by value
    121124    }
    122  return $args;
    123    
    124 }
    125 /*Function to check weather the maximum quantity is already existing in the cart*/
    126 
    127 add_action('woocommerce_add_to_cart', 'wc_mmax_custom_add_to_cart',10,2);
    128 
    129 function wc_mmax_custom_add_to_cart($args,$product)
    130 {
    131     $orderQTY = $_POST['quantity'];
     125    return $args;
     126}
     127
     128/* Function to check weather the maximum quantity is already existing in the cart */
     129
     130add_action('woocommerce_add_to_cart', 'wc_mmax_custom_add_to_cart', 10, 2);
     131
     132function wc_mmax_custom_add_to_cart($args, $product) {
     133    $orderQTY = $_POST['quantity'];
    132134    $mmaxEnable = get_post_meta($product, '_wc_mmax_prd_opt_enable', true);
    133     $minQty     = get_post_meta($product, '_wc_mmax_min', true);
    134     $maxQty     = get_post_meta($product, '_wc_mmax_max', true);
    135 $cartQty =  wc_mmax_woo_in_cart($product);
    136 if(get_option('_wcmmax_options_option_name') != NULL && get_option('_wcmmax_options_option_name') !='')
    137 {
    138 $maxQTYMsg = get_option('_wcmmax_options_option_name');
    139 }
    140 else {
    141     $maxQTYMsg = 'You have already added the maximum Quantity for the product for the current purchase';
    142 }
    143 if($maxQty < $cartQty && $mmaxEnable == 1)
    144 {
    145 
    146 wc_add_notice($maxQTYMsg,'error');
    147 exit(wp_redirect( get_permalink($product) ));
    148 
    149 }
    150 
    151 if(($orderQTY + $cartQty)  < $minQty && $mmaxEnable == 1)
    152 {
    153 
    154 wc_add_notice(__('You have ordered '.$orderQTY.'  which is less than the allowed Minimum Quantity '.$minQty.'','wcmax'),'error');
    155 exit(wp_redirect( get_permalink($product) ));
    156 }
    157 
     135    $minQty = get_post_meta($product, '_wc_mmax_min', true);
     136    $maxQty = get_post_meta($product, '_wc_mmax_max', true);
     137    $incrementbyQty = get_post_meta($product, '_wc_mmax_increment', true);
     138    $cartQty = wc_mmax_woo_in_cart($product);
     139    if (get_option('_wcmmax_options_option_name') != NULL && get_option('_wcmmax_options_option_name') != '') {
     140        $maxQTYMsg = get_option('_wcmmax_options_option_name');
     141    } else {
     142        $maxQTYMsg = 'You have already added the maximum Quantity for the product for the current purchase';
     143    }
     144    if ($maxQty < $cartQty && $mmaxEnable == 1) {
     145
     146        wc_add_notice($maxQTYMsg . $orderQTY+$cartQty, 'error');
     147        exit(wp_redirect(get_permalink($product)));
     148    }
     149    if ((($orderQTY + $cartQty) < $minQty || ($orderQTY + $cartQty) % $incrementbyQty > 0) && $mmaxEnable == 1) {
     150
     151        wc_add_notice(__('You have ordered ' . $orderQTY . '  which is less than the allowed Minimum Quantity ' . $minQty . ' or its not incremented in the specific format of x ' . $incrementbyQty, 'wcmax'), 'error');
     152        exit(wp_redirect(get_permalink($product)));
     153    }
    158154}
    159155
    160156function wc_mmax_woo_in_cart($product_id) {
    161157    global $woocommerce;
    162     foreach($woocommerce->cart->get_cart() as $key => $val ) {
    163    
     158    foreach ($woocommerce->cart->get_cart() as $key => $val) {
     159
    164160        $_product = $val['data'];
    165         if($product_id == $_product->get_id()) {
    166 
    167        
    168         return  $val['quantity'];
    169    
     161        if ($product_id == $_product->get_id()) {
     162
     163
     164            return $val['quantity'];
    170165        }
    171166    }
    172  
     167
    173168    return 0;
    174169}
    175170
    176 
    177171#filter hook to remove extra add to cart button in the shop and category pages
    178 add_filter( 'woocommerce_loop_add_to_cart_link','_wcmmax_add2cart' );
    179 function _wcmmax_add2cart( $link ) {
    180     global $product;
    181     $product_id = $product->get_id();
    182     $product_sku = $product->get_sku();
    183     $product_type = $product->get_type();
    184     $qtylink = '';
    185         $ajax_class = '';
    186     $mmaxEnable = get_post_meta( $product_id, '_wc_mmax_prd_opt_enable', true );
    187     $minQty     = get_post_meta( $product_id, '_wc_mmax_min', true );
    188 
    189     if ( $product_type != 'simple' || $mmaxEnable != 1 ){
    190         return $link;
    191     }
    192     $qtylink = '&quantity='.$minQty;
    193     $ajax_cart_en = 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' );
    194     if ( $ajax_cart_en && $mmaxEnable == 0  && $product_type !='variable' ) { //solution by @tigerrockmartialarts https://wordpress.org/support/topic/add-to-cart-on-variable-products/
    195         $ajax_class = 'ajax_add_to_cart';
    196     }
    197     $link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s %s">%s</a>',
    198         esc_url( $product->add_to_cart_url().$qtylink ),
    199         esc_attr( $product_id ),
    200         esc_attr( $product->get_sku() ),
    201         esc_attr( isset( $minQty ) ? $minQty : 1 ),
    202         $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
    203         esc_attr( $product->product_type ),
    204         esc_attr( $ajax_class ),
    205         esc_html( $product->add_to_cart_text() )
    206     );
    207     return $link;
    208 }
     172add_filter('woocommerce_loop_add_to_cart_link', '_wcmmax_add2cart');
     173
     174function _wcmmax_add2cart($link) {
     175    global $product;
     176    $product_id = $product->get_id();
     177    $product_sku = $product->get_sku();
     178    $product_type = $product->get_type();
     179    $qtylink = '';
     180    $ajax_class = '';
     181    $mmaxEnable = get_post_meta($product_id, '_wc_mmax_prd_opt_enable', true);
     182    $minQty = get_post_meta($product_id, '_wc_mmax_min', true);
     183
     184    if ($product_type != 'simple' || $mmaxEnable != 1) {
     185        return $link;
     186    }
     187    $qtylink = '&quantity=' . $minQty;
     188    $ajax_cart_en = 'yes' === get_option('woocommerce_enable_ajax_add_to_cart');
     189    if ($ajax_cart_en && $mmaxEnable == 0 && $product_type != 'variable') { //solution by @tigerrockmartialarts https://wordpress.org/support/topic/add-to-cart-on-variable-products/
     190        $ajax_class = 'ajax_add_to_cart';
     191    }
     192    $link = sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s %s">%s</a>',
     193            esc_url($product->add_to_cart_url() . $qtylink),
     194            esc_attr($product_id),
     195            esc_attr($product->get_sku()),
     196            esc_attr(isset($minQty) ? $minQty : 1),
     197            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
     198            esc_attr($product->get_type()),
     199            esc_attr($ajax_class),
     200            esc_html($product->add_to_cart_text())
     201    );
     202    return $link;
     203}
     204
     205add_action('woocommerce_check_cart_items', 'wc_mmxax_limit_cart_item_quantity_rule', 99);
     206
     207function wc_mmxax_limit_cart_item_quantity_rule() {
     208    if (!is_cart() || !isset($_POST))
     209        return; // Only on cart page
     210
     211    foreach (WC()->cart->cart_contents as $cart_items => $ci) {
     212        $minQty = get_post_meta($ci['product_id'], '_wc_mmax_min', true);
     213        $maxQty = get_post_meta($ci['product_id'], '_wc_mmax_max', true);
     214        $mmaxEnable = get_post_meta($ci['product_id'], '_wc_mmax_prd_opt_enable', true);
     215        $incrementbyQty = get_post_meta($ci['product_id'], '_wc_mmax_increment', true);
     216        $factory = new WC_Product_Factory();
     217        $product = $factory->get_product($ci['product_id']);
     218        $mod = $ci['quantity'] % $incrementbyQty;
     219        $qty = wc_mmax_closestCartQuantity($ci['quantity'], $incrementbyQty);
     220        if ($mod > 0 && $mmaxEnable == 1) {
     221            WC()->cart->set_quantity($cart_items, $qty);
     222            if (get_option('_wcmmax_cart_error_message') != NULL && get_option('_wcmmax_cart_error_message') != '') {
     223                $cartQtyRuleError = get_option('_wcmmax_cart_error_message');
     224            } else {
     225                $cartQtyRuleError = __('The Quantity that you try to set voids the Increment rule for the product <strong>' . $product->get_title() . '</strong>, so the cart is updated  with possible nearest Quantity.', 'wcmmax');
     226            }
     227            wc_clear_notices();
     228            wc_add_notice($cartQtyRuleError, 'error');
     229            break;
     230        }
     231    }
     232}
     233
     234function wc_mmax_closestCartQuantity($n, $m) {
     235    // find the quotient
     236    $q = (int) ($n / $m);
     237
     238    // 1st possible closest number
     239    $n1 = $m * $q;
     240
     241    // 2nd possible closest number
     242    $n2 = ($n * $m) > 0 ?
     243            ($m * ($q + 1)) : ($m * ($q - 1));
     244
     245    // if true, then n1 is the
     246    // required closest number
     247    if (abs($n - $n1) < abs($n - $n2))
     248        return $n1;
     249
     250    // else n2 is the required
     251    // closest number
     252    return $n2;
     253}
  • woo-min-max-quantity-limit/trunk/changelog.txt

    r2512073 r2543347  
    11*** WC Minimum & Maximum Quantity  Changelog ***
    2 2021.04.09 - version 2.1.4
     22021.06.06 - version 2.1.6
     3 * Bug Fix: If quantity item manually changed in the product page / cart page and if the changed quantity voids the minimum / maximum order quantity rule / the quantity increment rule then it throws error which was not earlier.
     42021.04.09 - version 2.1.5
    35 * Added feature to do increments based on the increment set by admin
    462021.01.01 - version 2.1.4
  • woo-min-max-quantity-limit/trunk/readme.txt

    r2512073 r2543347  
    44Tags: woocommerce, minimum, maximum, quantity, minimum purchase, maximum purchase, purchase
    55Requires at least: 3.3.1
    6 Tested up to: 5.7
    7 Stable tag: 2.1.5
    8 WC tested up to: 5.1.0
     6Tested up to: 5.7.2
     7Stable tag: 2.1.6
     8WC tested up to: 5.3.0
    99License: GNU General Public License v3.0
    1010License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    3030 * Select a particular product from the product list for which you need to enable this feature.
    3131 * Enable this feature and add the minimum and maximum allowed quantity in the respective boxes
     32 * Setting up the increment, if the increment by is set other than one, the minimum order quantity should be the value of the increment set.
     33 * Setting custom error notice / messages, in the admin panel go to Settings --> WooCommerce Minimum and Maximum Quantity Limit, there you will find 2 text area, there you can set the custom error message which will override the default error message in the product and cart page
    3234 
    3335== Frequently Asked Questions ==
     
    4244
    4345== Changelog ==
     46= 2.1.6 =
     47Bug Fix: If quantity item manually changed in the product page / cart page and if the changed quantity voids the minimum / maximum order quantity rule / the quantity increment rule then it throws error which was not earlier.
    4448= 2.1.5 =
    4549Added feature to do increments based on the increment set by admin
  • woo-min-max-quantity-limit/trunk/woo-min-max-quantity.php

    r2512073 r2543347  
    11<?php
    22/*
    3 Plugin Name: Woocommerce Minimum and Maximum Quantity
    4 Plugin URI:  http://ashokg.in/
    5 Description: Allow the site admin to enable the feature of minimum and maximum purchase of a particular product in each product.
    6 Version: 2.1.5
    7 Author: Ashok G
    8 Text Domain: wcmmax
    9 Author URI: https://ashokg.in
    10 WC tested up to: 5.1.0
    11 Copyright: © 2020 Ashok G.
    12 License: GNU General Public License v3.0
    13 License URI: http://www.gnu.org/licenses/gpl-3.0.html
    14 */
     3  Plugin Name: Woocommerce Minimum and Maximum Quantity
     4  Plugin URI:  http://ashokg.in/
     5  Description: Allow the site admin to enable the feature of minimum and maximum purchase of a particular product in each product.
     6  Version: 2.1.6
     7  Author: Ashok G
     8  Text Domain: wcmmax
     9  Author URI: https://ashokg.in
     10  WC tested up to: 5.3.0
     11  Copyright: © 2020 Ashok G.
     12  License: GNU General Public License v3.0
     13  License URI: http://www.gnu.org/licenses/gpl-3.0.html
     14 */
    1515
    1616
    1717add_action('add_meta_boxes', 'wc_mmax_meta_box_create');
    1818add_action('save_post', 'wc_mmax_save_meta_box');
    19 function wc_mmax_meta_box_create()
    20 {
     19
     20function wc_mmax_meta_box_create() {
    2121    add_meta_box('wc_mmax_enable', __('Min Max Quantity', 'wcmmax'), 'wc_mmax_meta_box', 'product', 'side');
    2222}
    2323
    24 function wc_mmax_meta_box($post)
    25 {
     24function wc_mmax_meta_box($post) {
    2625    wp_nonce_field('wc_mmax_cst_prd_nonce', 'wc_mmax_cst_prd_nonce');
    27    
     26
    2827    echo '<table>';
    2928    echo '<tr><td><label for="_wc_mmax_prd_opt_enable">' . __('Enable', 'wcmmax') . '</label>';
     
    4645    echo '<td><input type="number" id="_wc_mmax_increment" class="short" name="_wc_mmax_increment" value="' . $wc_mmax_increment . '" /></td>';
    4746    echo '</tr></table>';
    48    
    49 }
    50 
    51 function wc_mmax_save_meta_box($post_id)
    52 {
     47}
     48
     49function wc_mmax_save_meta_box($post_id) {
    5350    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    5451        return;
     
    5653        return;
    5754    update_post_meta($post_id, '_wc_mmax_prd_opt_enable', (int) $_POST['_wc_mmax_prd_opt_enable']);
    58     update_post_meta($post_id, '_wc_mmax_max',(int) $_POST['_wc_mmax_max']);
     55    update_post_meta($post_id, '_wc_mmax_max', (int) $_POST['_wc_mmax_max']);
    5956    update_post_meta($post_id, '_wc_mmax_min', (int) $_POST['_wc_mmax_min']);
    6057    update_post_meta($post_id, '_wc_mmax_increment', (int) $_POST['_wc_mmax_increment']);
    6158}
    6259
    63 
    64 
    65  
    66 /** Functions For Custom Option Page for Plugin **/
     60/** Functions For Custom Option Page for Plugin * */
    6761function _wcmmax_options_register_settings() {
    68    add_option( '_wcmmax_options_option_name', 'Settings.');
    69    register_setting( '_wcmmax_options_group', '_wcmmax_options_option_name', '_wcmmax_options_callback' );
    70 }
    71 add_action( 'admin_init', '_wcmmax_options_register_settings' );
     62    add_option('_wcmmax_options_option_name', '');
     63    add_option('_wcmmax_cart_error_message', '');
     64    register_setting('_wcmmax_options_group', '_wcmmax_options_option_name', '_wcmmax_options_callback');
     65    register_setting('_wcmmax_options_group', '_wcmmax_cart_error_message', '_wcmmax_options_callback');
     66}
     67
     68add_action('admin_init', '_wcmmax_options_register_settings');
    7269
    7370function _wcmmax_register_options_page() {
    74   add_options_page('WooCommerce Minimum & Maximum Quantity Limit Settings', 'WooCommerce Minimum & Maximum Quantity Limit', 'manage_options', '_wcmmax_', '_wcmmax_options_page');
    75 }
     71    add_options_page('WooCommerce Minimum & Maximum Quantity Limit Settings', 'WooCommerce Minimum & Maximum Quantity Limit', 'manage_options', '_wcmmax_', '_wcmmax_options_page');
     72}
     73
    7674add_action('admin_menu', '_wcmmax_register_options_page');
    7775
    78 function _wcmmax_options_page()
    79 {
    80 ?>
    81   <div>
    82  
    83   <h2>WooCommerce Minimum & Maximum Quantity Limit Settings</h2>
    84   <form method="post" action="options.php">
    85   <?php settings_fields( '_wcmmax_options_group' ); ?>
    86  
    87   <table>
    88   <tr valign="top">
    89   <th scope="row"><label for="_wcmmax_options_option_name">Custom alert message for maximum Quantity limit </label></th>
    90   </tr>
    91   <tr>
    92       <td><textarea cols="60" rows="5" id="_wcmmax_options_option_name" name="_wcmmax_options_option_name" ><?php echo get_option('_wcmmax_options_option_name'); ?> </textarea></td>
    93   </tr>
    94   </table>
    95   <?php  submit_button(); ?>
    96   </form>
    97   </div>
    98 <?php
    99 }
    100 
    101   /** Functions For Custom Option Page for Plugin **/
    102 
    103 /*Function to manipulate custom minimum and maximum purchase*/
     76function _wcmmax_options_page() {
     77    ?>
     78    <div>
     79
     80        <h2>WooCommerce Minimum & Maximum Quantity Limit Settings</h2>
     81        <form method="post" action="options.php">
     82            <?php settings_fields('_wcmmax_options_group'); ?>
     83
     84            <table>
     85                <tr valign="top">
     86                    <th scope="row"><label for="_wcmmax_options_option_name">Custom alert message for maximum Quantity limit </label></th>
     87                </tr>
     88                <tr>
     89                    <td><textarea cols="60" rows="5" id="_wcmmax_options_option_name" name="_wcmmax_options_option_name" ><?php echo get_option('_wcmmax_options_option_name'); ?> </textarea></td>
     90                </tr>
     91                <tr valign="top">
     92                    <th scope="row"><label for="_wcmmax_cart_error_message">Cart Page Custom alert message for quantity update voiding the increment rules </label></th>
     93                </tr>
     94                <tr>
     95                    <td><textarea cols="60" rows="5" id="_wcmmax_cart_error_message" placeholder="Enter Custom Error Message to be shown in the cart page for voiding the product quantity increment rules" name="_wcmmax_cart_error_message" ><?php echo get_option('_wcmmax_cart_error_message'); ?> </textarea></td>
     96                </tr>
     97
     98            </table>
     99            <?php submit_button(); ?>
     100        </form>
     101    </div>
     102    <?php
     103}
     104
     105/** Functions For Custom Option Page for Plugin * */
     106/* Function to manipulate custom minimum and maximum purchase */
    104107add_filter('woocommerce_quantity_input_args', 'wc_mmax_quantity_input_args', 10, 2);
    105 function wc_mmax_quantity_input_args($args, $product)
    106 {
    107 if(function_exists('icl_object_id')) {
    108     $default_language = wpml_get_default_language();
    109     $prodid = icl_object_id($product->get_id(), 'product', true, $default_language);
    110 } else {
    111     $prodid = $product->get_id();
    112 }
     108
     109function wc_mmax_quantity_input_args($args, $product) {
     110    if (function_exists('icl_object_id')) {
     111        $default_language = wpml_get_default_language();
     112        $prodid = icl_object_id($product->get_id(), 'product', true, $default_language);
     113    } else {
     114        $prodid = $product->get_id();
     115    }
    113116    $mmaxEnable = get_post_meta($prodid, '_wc_mmax_prd_opt_enable', true);
    114     $minQty     = get_post_meta($prodid, '_wc_mmax_min', true);
    115     $maxQty     = get_post_meta($prodid, '_wc_mmax_max', true);
    116     $incrementbyQty     = get_post_meta($prodid, '_wc_mmax_increment', true);
     117    $minQty = get_post_meta($prodid, '_wc_mmax_min', true);
     118    $maxQty = get_post_meta($prodid, '_wc_mmax_max', true);
     119    $incrementbyQty = get_post_meta($prodid, '_wc_mmax_increment', true);
    117120    if ($minQty > 0 && $maxQty > 0 && $mmaxEnable == 1) {
    118121        $args['min_value'] = $minQty; // Starting value
     
    120123        $args['step'] = $incrementbyQty; // Increase by value
    121124    }
    122  return $args;
    123    
    124 }
    125 /*Function to check weather the maximum quantity is already existing in the cart*/
    126 
    127 add_action('woocommerce_add_to_cart', 'wc_mmax_custom_add_to_cart',10,2);
    128 
    129 function wc_mmax_custom_add_to_cart($args,$product)
    130 {
    131     $orderQTY = $_POST['quantity'];
     125    return $args;
     126}
     127
     128/* Function to check weather the maximum quantity is already existing in the cart */
     129
     130add_action('woocommerce_add_to_cart', 'wc_mmax_custom_add_to_cart', 10, 2);
     131
     132function wc_mmax_custom_add_to_cart($args, $product) {
     133    $orderQTY = $_POST['quantity'];
    132134    $mmaxEnable = get_post_meta($product, '_wc_mmax_prd_opt_enable', true);
    133     $minQty     = get_post_meta($product, '_wc_mmax_min', true);
    134     $maxQty     = get_post_meta($product, '_wc_mmax_max', true);
    135 $cartQty =  wc_mmax_woo_in_cart($product);
    136 if(get_option('_wcmmax_options_option_name') != NULL && get_option('_wcmmax_options_option_name') !='')
    137 {
    138 $maxQTYMsg = get_option('_wcmmax_options_option_name');
    139 }
    140 else {
    141     $maxQTYMsg = 'You have already added the maximum Quantity for the product for the current purchase';
    142 }
    143 if($maxQty < $cartQty && $mmaxEnable == 1)
    144 {
    145 
    146 wc_add_notice($maxQTYMsg,'error');
    147 exit(wp_redirect( get_permalink($product) ));
    148 
    149 }
    150 
    151 if(($orderQTY + $cartQty)  < $minQty && $mmaxEnable == 1)
    152 {
    153 
    154 wc_add_notice(__('You have ordered '.$orderQTY.'  which is less than the allowed Minimum Quantity '.$minQty.'','wcmax'),'error');
    155 exit(wp_redirect( get_permalink($product) ));
    156 }
    157 
     135    $minQty = get_post_meta($product, '_wc_mmax_min', true);
     136    $maxQty = get_post_meta($product, '_wc_mmax_max', true);
     137    $incrementbyQty = get_post_meta($product, '_wc_mmax_increment', true);
     138    $cartQty = wc_mmax_woo_in_cart($product);
     139    if (get_option('_wcmmax_options_option_name') != NULL && get_option('_wcmmax_options_option_name') != '') {
     140        $maxQTYMsg = get_option('_wcmmax_options_option_name');
     141    } else {
     142        $maxQTYMsg = 'You have already added the maximum Quantity for the product for the current purchase';
     143    }
     144    if ($maxQty < $cartQty && $mmaxEnable == 1) {
     145
     146        wc_add_notice($maxQTYMsg . $orderQTY+$cartQty, 'error');
     147        exit(wp_redirect(get_permalink($product)));
     148    }
     149    if ((($orderQTY + $cartQty) < $minQty || ($orderQTY + $cartQty) % $incrementbyQty > 0) && $mmaxEnable == 1) {
     150
     151        wc_add_notice(__('You have ordered ' . $orderQTY . '  which is less than the allowed Minimum Quantity ' . $minQty . ' or its not incremented in the specific format of x ' . $incrementbyQty, 'wcmax'), 'error');
     152        exit(wp_redirect(get_permalink($product)));
     153    }
    158154}
    159155
    160156function wc_mmax_woo_in_cart($product_id) {
    161157    global $woocommerce;
    162     foreach($woocommerce->cart->get_cart() as $key => $val ) {
    163    
     158    foreach ($woocommerce->cart->get_cart() as $key => $val) {
     159
    164160        $_product = $val['data'];
    165         if($product_id == $_product->get_id()) {
    166 
    167        
    168         return  $val['quantity'];
    169    
     161        if ($product_id == $_product->get_id()) {
     162
     163
     164            return $val['quantity'];
    170165        }
    171166    }
    172  
     167
    173168    return 0;
    174169}
    175170
    176 
    177171#filter hook to remove extra add to cart button in the shop and category pages
    178 add_filter( 'woocommerce_loop_add_to_cart_link','_wcmmax_add2cart' );
    179 function _wcmmax_add2cart( $link ) {
    180     global $product;
    181     $product_id = $product->get_id();
    182     $product_sku = $product->get_sku();
    183     $product_type = $product->get_type();
    184     $qtylink = '';
    185         $ajax_class = '';
    186     $mmaxEnable = get_post_meta( $product_id, '_wc_mmax_prd_opt_enable', true );
    187     $minQty     = get_post_meta( $product_id, '_wc_mmax_min', true );
    188 
    189     if ( $product_type != 'simple' || $mmaxEnable != 1 ){
    190         return $link;
    191     }
    192     $qtylink = '&quantity='.$minQty;
    193     $ajax_cart_en = 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' );
    194     if ( $ajax_cart_en && $mmaxEnable == 0  && $product_type !='variable' ) { //solution by @tigerrockmartialarts https://wordpress.org/support/topic/add-to-cart-on-variable-products/
    195         $ajax_class = 'ajax_add_to_cart';
    196     }
    197     $link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s %s">%s</a>',
    198         esc_url( $product->add_to_cart_url().$qtylink ),
    199         esc_attr( $product_id ),
    200         esc_attr( $product->get_sku() ),
    201         esc_attr( isset( $minQty ) ? $minQty : 1 ),
    202         $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
    203         esc_attr( $product->product_type ),
    204         esc_attr( $ajax_class ),
    205         esc_html( $product->add_to_cart_text() )
    206     );
    207     return $link;
    208 }
     172add_filter('woocommerce_loop_add_to_cart_link', '_wcmmax_add2cart');
     173
     174function _wcmmax_add2cart($link) {
     175    global $product;
     176    $product_id = $product->get_id();
     177    $product_sku = $product->get_sku();
     178    $product_type = $product->get_type();
     179    $qtylink = '';
     180    $ajax_class = '';
     181    $mmaxEnable = get_post_meta($product_id, '_wc_mmax_prd_opt_enable', true);
     182    $minQty = get_post_meta($product_id, '_wc_mmax_min', true);
     183
     184    if ($product_type != 'simple' || $mmaxEnable != 1) {
     185        return $link;
     186    }
     187    $qtylink = '&quantity=' . $minQty;
     188    $ajax_cart_en = 'yes' === get_option('woocommerce_enable_ajax_add_to_cart');
     189    if ($ajax_cart_en && $mmaxEnable == 0 && $product_type != 'variable') { //solution by @tigerrockmartialarts https://wordpress.org/support/topic/add-to-cart-on-variable-products/
     190        $ajax_class = 'ajax_add_to_cart';
     191    }
     192    $link = sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s %s">%s</a>',
     193            esc_url($product->add_to_cart_url() . $qtylink),
     194            esc_attr($product_id),
     195            esc_attr($product->get_sku()),
     196            esc_attr(isset($minQty) ? $minQty : 1),
     197            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
     198            esc_attr($product->get_type()),
     199            esc_attr($ajax_class),
     200            esc_html($product->add_to_cart_text())
     201    );
     202    return $link;
     203}
     204
     205add_action('woocommerce_check_cart_items', 'wc_mmxax_limit_cart_item_quantity_rule', 99);
     206
     207function wc_mmxax_limit_cart_item_quantity_rule() {
     208    if (!is_cart() || !isset($_POST))
     209        return; // Only on cart page
     210
     211    foreach (WC()->cart->cart_contents as $cart_items => $ci) {
     212        $minQty = get_post_meta($ci['product_id'], '_wc_mmax_min', true);
     213        $maxQty = get_post_meta($ci['product_id'], '_wc_mmax_max', true);
     214        $mmaxEnable = get_post_meta($ci['product_id'], '_wc_mmax_prd_opt_enable', true);
     215        $incrementbyQty = get_post_meta($ci['product_id'], '_wc_mmax_increment', true);
     216        $factory = new WC_Product_Factory();
     217        $product = $factory->get_product($ci['product_id']);
     218        $mod = $ci['quantity'] % $incrementbyQty;
     219        $qty = wc_mmax_closestCartQuantity($ci['quantity'], $incrementbyQty);
     220        if ($mod > 0 && $mmaxEnable == 1) {
     221            WC()->cart->set_quantity($cart_items, $qty);
     222            if (get_option('_wcmmax_cart_error_message') != NULL && get_option('_wcmmax_cart_error_message') != '') {
     223                $cartQtyRuleError = get_option('_wcmmax_cart_error_message');
     224            } else {
     225                $cartQtyRuleError = __('The Quantity that you try to set voids the Increment rule for the product <strong>' . $product->get_title() . '</strong>, so the cart is updated  with possible nearest Quantity.', 'wcmmax');
     226            }
     227            wc_clear_notices();
     228            wc_add_notice($cartQtyRuleError, 'error');
     229            break;
     230        }
     231    }
     232}
     233
     234function wc_mmax_closestCartQuantity($n, $m) {
     235    // find the quotient
     236    $q = (int) ($n / $m);
     237
     238    // 1st possible closest number
     239    $n1 = $m * $q;
     240
     241    // 2nd possible closest number
     242    $n2 = ($n * $m) > 0 ?
     243            ($m * ($q + 1)) : ($m * ($q - 1));
     244
     245    // if true, then n1 is the
     246    // required closest number
     247    if (abs($n - $n1) < abs($n - $n2))
     248        return $n1;
     249
     250    // else n2 is the required
     251    // closest number
     252    return $n2;
     253}
Note: See TracChangeset for help on using the changeset viewer.