nshelper

WPGlobaCart - WooCommerce Min/Max Quantities

Oct 20th, 2025 (edited)
2,286
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.78 KB | None | 0 0
  1. /**
  2.          * Validate cart items against set rules
  3.          *
  4.          * @throws Exception When cart validation fails.
  5.          */
  6.         public function check_cart_items() {
  7.  
  8.             try {
  9.                 $checked_ids         = array();
  10.                 $product_quantities  = array();
  11.                 $category_quantities = array();
  12.                 $total_quantity      = 0;
  13.                 $total_cost          = 0;
  14.                 $apply_cart_rules    = false;
  15.                 /**
  16.                  * Use this filter to include/exclude tax when calculating total cost of the cart.
  17.                  *
  18.                  * @since 5.1.0
  19.                  *
  20.                  * @param  boolean $include_tax Include tax.
  21.                  */
  22.                 $include_tax = apply_filters( 'wc_min_max_quantity_include_tax_order_value_restrictions', true );
  23.  
  24.                 $price_fn = $include_tax ? 'wc_get_price_including_tax' : 'wc_get_price_excluding_tax';
  25.  
  26.                 // Count items + variations first.
  27.                 foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  28.                     do_action( 'woocommerce/cart_loop/start', $values );
  29.                     $product     = $values['data'];
  30.                     $checking_id = $this->get_id_to_check( $values );
  31.  
  32.                     /**
  33.                      * Use this filter to prevent the quantity of a cart item from being counted in cart-level rules.
  34.                      *
  35.                      * @since 2.4.11
  36.                      *
  37.                      * @param  boolean $do_not_count
  38.                      * @param  int     $checking_id
  39.                      * @param  string  $cart_item_key
  40.                      * @param  array   $values
  41.                      */
  42.                     if ( apply_filters( 'wc_min_max_cart_quantity_do_not_count', false, $checking_id, $cart_item_key, $values ) ) {
  43.                         $values['quantity'] = 0;
  44.                     }
  45.  
  46.                     if ( ! isset( $product_quantities[ $checking_id ] ) ) {
  47.                         $product_quantities[ $checking_id ] = $values['quantity'];
  48.                     } else {
  49.                         $product_quantities[ $checking_id ] += $values['quantity'];
  50.                     }
  51.  
  52.                     $checking_product = wc_get_product( $checking_id );
  53.                     $parent_product   = $values['product_id'] === $checking_id ? $checking_product : wc_get_product( $values['product_id'] );
  54.  
  55.                     $minmax_do_not_count = 'no';
  56.                     if ( $checking_product && 'yes' === $checking_product->get_meta( 'variation_minmax_do_not_count', true ) ) {
  57.                         $minmax_do_not_count = 'yes';
  58.                     } elseif ( $parent_product && 'yes' === $parent_product->get_meta( 'minmax_do_not_count', true ) ) {
  59.                         $minmax_do_not_count = 'yes';
  60.                     }
  61.  
  62.                     /**
  63.                      * Use this filter to prevent the quantity or cost of a cart item from being counted in cart-level rules.
  64.                      *
  65.                      * @since 2.3.6
  66.                      *
  67.                      * @param  boolean $do_not_count
  68.                      * @param  int     $checking_id
  69.                      * @param  string  $cart_item_key
  70.                      * @param  array   $values
  71.                      */
  72.                     $minmax_do_not_count = apply_filters( 'wc_min_max_quantity_minmax_do_not_count', $minmax_do_not_count, $checking_id, $cart_item_key, $values );
  73.  
  74.                     $minmax_cart_exclude = 'no';
  75.                     if ( $checking_product && 'yes' === $checking_product->get_meta( 'variation_minmax_cart_exclude', true ) ) {
  76.                         $minmax_cart_exclude = 'yes';
  77.                     } elseif ( $parent_product && 'yes' === $parent_product->get_meta( 'minmax_cart_exclude', true ) ) {
  78.                         $minmax_cart_exclude = 'yes';
  79.                     }
  80.  
  81.                     /**
  82.                      * Use this filter to exclude a product from cart-level rules.
  83.                      *
  84.                      * @since 2.3.6
  85.                      *
  86.                      * @param  boolean $exclude
  87.                      * @param  int     $checking_id
  88.                      * @param  string  $cart_item_key
  89.                      * @param  array   $values
  90.                      */
  91.                     $minmax_cart_exclude = apply_filters( 'wc_min_max_quantity_minmax_cart_exclude', $minmax_cart_exclude, $checking_id, $cart_item_key, $values );
  92.  
  93.                     if ( 'yes' !== $minmax_do_not_count && 'yes' !== $minmax_cart_exclude ) {
  94.                         $total_cost += $price_fn(
  95.                             $product,
  96.                             array(
  97.                                 'price' => (float) $product->get_price(),
  98.                                 'qty'   => (float) $values['quantity'],
  99.                             )
  100.                         );
  101.                     }
  102.                    
  103.                     do_action( 'woocommerce/cart_loop/end', $values );
  104.                 }
  105.  
  106.                 // Check cart items.
  107.                 foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  108.                     do_action( 'woocommerce/cart_loop/start', $values );
  109.                     $checking_id    = $this->get_id_to_check( $values );
  110.                     $terms          = get_the_terms( $values['product_id'], 'product_cat' );
  111.                     $found_term_ids = array();
  112.  
  113.                     // If a product belongs to multiple categories with different 'Group of' values, find the category with the smallest 'Group of' value.
  114.                     $min_group_of_category_id    = 0;
  115.                     $min_group_of_category_value = 0;
  116.  
  117.                     $checking_product = wc_get_product( $checking_id );
  118.                     $parent_product   = $values['product_id'] === $checking_id ? $checking_product : wc_get_product( $values['product_id'] );
  119.  
  120.                     if ( $terms ) {
  121.  
  122.                         foreach ( $terms as $term ) {
  123.  
  124.                             if ( in_array( $term->term_id, $found_term_ids, true ) ) {
  125.                                 continue;
  126.                             }
  127.  
  128.                             if ( ( $checking_product && 'yes' === $checking_product->get_meta( 'variation_minmax_category_group_of_exclude', true ) ) || ( $parent_product && 'yes' === $parent_product->get_meta( 'minmax_category_group_of_exclude', true ) ) ) {
  129.                                 continue;
  130.                             }
  131.  
  132.                             if ( ( ! $checking_product || ! empty( $checking_product->get_meta( 'variation_group_of_quantity', true ) ) ) || ( ! $parent_product || ! empty( $parent_product->get_meta( 'group_of_quantity', true ) ) ) ) {
  133.                                 continue;
  134.                             }
  135.  
  136.                             $found_term_ids[]        = $term->term_id;
  137.                             $category_group_of_value = absint( get_term_meta( $term->term_id, 'group_of_quantity', true ) );
  138.  
  139.                             if ( 0 !== $category_group_of_value && ( 0 === $min_group_of_category_value || $category_group_of_value < $min_group_of_category_value ) ) {
  140.                                 $min_group_of_category_value = $category_group_of_value;
  141.                                 $min_group_of_category_id    = $term->term_id;
  142.                             }
  143.  
  144.                             // Record count in parents of this category too.
  145.                             $parents = get_ancestors( $term->term_id, 'product_cat' );
  146.  
  147.                             foreach ( $parents as $parent ) {
  148.                                 if ( in_array( $parent, $found_term_ids, true ) ) {
  149.                                     continue;
  150.                                 }
  151.  
  152.                                 $found_term_ids[]        = $parent;
  153.                                 $category_group_of_value = absint( get_term_meta( $parent, 'group_of_quantity', true ) );
  154.  
  155.                                 if ( 0 !== $category_group_of_value && ( 0 === $min_group_of_category_value || $category_group_of_value < $min_group_of_category_value ) ) {
  156.                                     $min_group_of_category_value = $category_group_of_value;
  157.                                     $min_group_of_category_id    = $parent;
  158.                                 }
  159.                             }
  160.                         }
  161.                     }
  162.  
  163.                     if ( 0 !== $min_group_of_category_id && 0 !== $min_group_of_category_value ) {
  164.                         $category_quantities[ $min_group_of_category_id ] = isset( $category_quantities[ $min_group_of_category_id ] )
  165.                             ? $category_quantities[ $min_group_of_category_id ] + $values['quantity']
  166.                             : $values['quantity'];
  167.                     }
  168.  
  169.                     // Check item rules once per product ID.
  170.                     if ( in_array( $checking_id, $checked_ids, true ) ) {
  171.                         continue;
  172.                     }
  173.  
  174.                     $product = $values['data'];
  175.  
  176.                     $minmax_do_not_count = 'no';
  177.                     if ( $checking_product && 'yes' === $checking_product->get_meta( 'variation_minmax_do_not_count', true ) ) {
  178.                         $minmax_do_not_count = 'yes';
  179.                     } elseif ( $parent_product && 'yes' === $parent_product->get_meta( 'minmax_do_not_count', true ) ) {
  180.                         $minmax_do_not_count = 'yes';
  181.                     }
  182.  
  183.                     /**
  184.                      * Use this filter to prevent the quantity or cost of a cart item from being counted in cart-level rules.
  185.                      *
  186.                      * @since 2.3.6
  187.                      *
  188.                      * @param  boolean $do_not_count
  189.                      * @param  int     $checking_id
  190.                      * @param  string  $cart_item_key
  191.                      * @param  array   $values
  192.                      */
  193.                     $minmax_do_not_count = apply_filters( 'wc_min_max_quantity_minmax_do_not_count', $minmax_do_not_count, $checking_id, $cart_item_key, $values );
  194.  
  195.                     $minmax_cart_exclude = 'no';
  196.                     if ( $checking_product && 'yes' === $checking_product->get_meta( 'variation_minmax_cart_exclude', true ) ) {
  197.                         $minmax_cart_exclude = 'yes';
  198.                     } elseif ( $parent_product && 'yes' === $parent_product->get_meta( 'minmax_cart_exclude', true ) ) {
  199.                         $minmax_cart_exclude = 'yes';
  200.                     }
  201.  
  202.                     /**
  203.                      * Use this filter to exclude a product from cart-level rules.
  204.                      *
  205.                      * @since 2.3.6
  206.                      *
  207.                      * @param  boolean $exclude
  208.                      * @param  int     $checking_id
  209.                      * @param  string  $cart_item_key
  210.                      * @param  array   $values
  211.                      */
  212.                     $minmax_cart_exclude = apply_filters( 'wc_min_max_quantity_minmax_cart_exclude', $minmax_cart_exclude, $checking_id, $cart_item_key, $values );
  213.  
  214.                     if ( 'yes' === $minmax_do_not_count || 'yes' === $minmax_cart_exclude ) {
  215.                         // Do not count.
  216.                         $this->excludes[] = $product->get_name();
  217.  
  218.                     } else {
  219.                         $total_quantity += $product_quantities[ $checking_id ];
  220.                     }
  221.  
  222.                     if ( 'yes' !== $minmax_cart_exclude ) {
  223.                         $apply_cart_rules = true;
  224.                     }
  225.  
  226.                     $checked_ids[] = $checking_id;
  227.  
  228.                     if ( $values['variation_id'] ) {
  229.                         $variation         = wc_get_product( $values['variation_id'] );
  230.                         $min_max_rules     = $variation ? 'yes' === $variation->get_meta( 'min_max_rules', true ) : false;
  231.                         $allow_combination = $parent_product ? 'yes' === $parent_product->get_meta( 'allow_combination', true ) : false;
  232.  
  233.                         [
  234.                             WC_Min_Max_Quantities_Quantity_Rules::MINIMUM => $minimum_quantity,
  235.                             WC_Min_Max_Quantities_Quantity_Rules::MAXIMUM => $maximum_quantity,
  236.                             WC_Min_Max_Quantities_Quantity_Rules::GROUP_OF => $group_of_quantity
  237.                         ] = $this->get_quantity_rules_for_product( $values['variation_id'] );
  238.  
  239.                         $filter_id = $min_max_rules && ! $allow_combination ? $values['variation_id'] : $values['product_id'];
  240.                     } else { // Not a variable product.
  241.                         [
  242.                             WC_Min_Max_Quantities_Quantity_Rules::MINIMUM => $minimum_quantity,
  243.                             WC_Min_Max_Quantities_Quantity_Rules::MAXIMUM => $maximum_quantity,
  244.                             WC_Min_Max_Quantities_Quantity_Rules::GROUP_OF => $group_of_quantity
  245.                         ] = $this->get_quantity_rules_for_product( $checking_id );
  246.  
  247.                         $filter_id = $checking_id;
  248.                     }
  249.  
  250.                     /**
  251.                      * Use this filter to filter the Minimum Quantity of a product/variation.
  252.                      *
  253.                      * @since 2.2.7
  254.                      *
  255.                      * @param  string  $quantity
  256.                      * @param  int     $product_id
  257.                      * @param  string  $cart_item_key
  258.                      * @param  array   $cart_item
  259.                      */
  260.                     $minimum_quantity = absint( apply_filters( 'wc_min_max_quantity_minimum_allowed_quantity', $minimum_quantity, $filter_id, $cart_item_key, $values ) );
  261.  
  262.                     /**
  263.                      * Use this filter to filter the Maximum Quantity of a product/variation.
  264.                      *
  265.                      * @since 2.2.7
  266.                      *
  267.                      * @param  string  $quantity
  268.                      * @param  int     $product_id
  269.                      * @param  string  $cart_item_key
  270.                      * @param  array   $cart_item
  271.                      */
  272.                     $maximum_quantity = absint( apply_filters( 'wc_min_max_quantity_maximum_allowed_quantity', $maximum_quantity, $filter_id, $cart_item_key, $values ) );
  273.  
  274.                     /**
  275.                      * Use this filter to filter the Group of quantity of a product/variation.
  276.                      *
  277.                      * @since 2.2.7
  278.                      *
  279.                      * @param  string  $quantity
  280.                      * @param  int     $product_id
  281.                      * @param  string  $cart_item_key
  282.                      * @param  array   $cart_item
  283.                      */
  284.                     $group_of_quantity = absint( apply_filters( 'wc_min_max_quantity_group_of_quantity', $group_of_quantity, $filter_id, $cart_item_key, $values ) );
  285.  
  286.                     $this->check_rules( $product, $product_quantities[ $checking_id ], $minimum_quantity, $maximum_quantity, $group_of_quantity, $checking_id );
  287.                    
  288.                     do_action( 'woocommerce/cart_loop/end', $values );
  289.                 }
  290.  
  291.                 // Cart rules.
  292.                 if ( $apply_cart_rules ) {
  293.  
  294.                     $excludes = '';
  295.  
  296.                     if ( count( $this->excludes ) > 0 ) {
  297.                         $excludes = ' (' . __( 'excludes ', 'woocommerce-min-max-quantities' ) . implode( ', ', $this->excludes ) . ')';
  298.                     }
  299.  
  300.                     if ( $this->minimum_order_quantity > 0 && $total_quantity < $this->minimum_order_quantity ) {
  301.                         /* translators: %d: Minimum amount of items in the cart */
  302.                         $notice = sprintf( __( 'To place an order, your cart must contain at least %d items.', 'woocommerce-min-max-quantities' ), $this->minimum_order_quantity ) . $excludes;
  303.                         throw new Exception( $notice );
  304.                     }
  305.  
  306.                     if ( $this->maximum_order_quantity > 0 && $total_quantity > $this->maximum_order_quantity ) {
  307.                         /* translators: %d: Maximum amount of items in the cart */
  308.                         $notice = sprintf( __( 'Your cart must not contain more than %d items to place an order.', 'woocommerce-min-max-quantities' ), $this->maximum_order_quantity );
  309.  
  310.                         throw new Exception( $notice );
  311.  
  312.                     }
  313.  
  314.                     // Check cart value.
  315.                     if ( $this->minimum_order_value && $total_cost < $this->minimum_order_value ) {
  316.                         /* translators: %s: Minimum order value */
  317.                         $notice = sprintf( __( 'To place an order, your cart total must be at least %s.', 'woocommerce-min-max-quantities' ), wc_price( $this->minimum_order_value ) ) . $excludes;
  318.  
  319.                         throw new Exception( $notice );
  320.                     }
  321.  
  322.                     if ( $this->maximum_order_value && $total_cost > $this->maximum_order_value ) {
  323.                         /* translators: %s: Maximum order value */
  324.                         $notice = sprintf( __( 'Your cart total must not be higher than %s to place an order.', 'woocommerce-min-max-quantities' ), wc_price( $this->maximum_order_value ) );
  325.  
  326.                         throw new Exception( $notice );
  327.                     }
  328.                 }
  329.  
  330.                 // Check category rules.
  331.                 foreach ( $category_quantities as $category => $quantity ) {
  332.  
  333.                     $group_of_quantity = get_term_meta( $category, 'group_of_quantity', true );
  334.  
  335.                     if ( $group_of_quantity > 0 && ( intval( $quantity ) % intval( $group_of_quantity ) > 0 ) ) {
  336.  
  337.                         $term          = get_term_by( 'id', $category, 'product_cat' );
  338.                         $product_names = array();
  339.  
  340.                         foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  341.  
  342.                             // If exclude is enable, skip.
  343.                             if ( ( $parent_product && 'yes' === $parent_product->get_meta( 'minmax_category_group_of_exclude', true ) ) || ( $variation && 'yes' === $variation->get_meta( 'variation_minmax_category_group_of_exclude', true ) ) ) {
  344.                                 continue;
  345.                             }
  346.  
  347.                             if ( has_term( $category, 'product_cat', $values['product_id'] ) ) {
  348.                                 $product_names[] = $values['data']->get_title();
  349.                             }
  350.                         }
  351.  
  352.                         if ( $product_names ) {
  353.                             /* translators: %1$s: Category name, %2$s: Comma separated list of product names, %3$d: Group amount */
  354.                             $notice = sprintf( __( 'Products in the <strong>%1$s</strong> category (<em>%2$s</em>) must be bought in multiples of %3$d.', 'woocommerce-min-max-quantities' ), $term->name, implode( ', ', $product_names ), $group_of_quantity, $group_of_quantity - ( $quantity % $group_of_quantity ) );
  355.  
  356.                             throw new Exception( $notice );
  357.                         }
  358.                     }
  359.                 }
  360.             } catch ( Exception $e ) {
  361.  
  362.                 if ( WC_MMQ_Core_Compatibility::is_store_api_request() ) {
  363.                     throw $e;
  364.  
  365.                 } else {
  366.  
  367.                     $notice = $e->getMessage();
  368.  
  369.                     if ( $notice ) {
  370.                         wc_add_notice( $notice, 'error' );
  371.                     }
  372.                 }
  373.             }
  374.         }
Advertisement
Comments
  • nshelper
    1 year
    # text 0.06 KB | 0 0
    1. woocommerce-min-max-quantities/woocommerce-min-max-quantities.php
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Sacimte
    21 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=eftcgrtnbm
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment