1 ? $step : 1; } if ( $max > 0 && $max < $min ) { $max = $min; } return array( 'min' => $min, 'step' => $step, 'max' => $max > 0 ? $max : 0, 'cumulative' => (bool) $cumulative, ); } /** * @param WC_Product|int $product * @return array|null */ public static function rules( $product ) { static $cache = array(); $product = $product instanceof WC_Product ? $product : wc_get_product( $product ); if ( ! $product ) { return null; } $pid = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(); if ( array_key_exists( $pid, $cache ) ) { return $cache[ $pid ]; } $parent = $product->is_type( 'variation' ) ? wc_get_product( $pid ) : $product; if ( ! $parent || $parent->is_sold_individually() ) { return $cache[ $pid ] = null; } $min = $parent->get_meta( '_h23pf_min_qty', true ); $step = $parent->get_meta( '_h23pf_step', true ); $max = $parent->get_meta( '_h23pf_max_qty', true ); $cum = 'yes' === $parent->get_meta( '_h23pf_qty_cumulative', true ); if ( '' === (string) $min && '' === (string) $step && '' === (string) $max ) { $rules = self::category_rules( $pid ); if ( $rules ) { $rules['cumulative'] = $cum; } } else { $rules = self::normalize( $min, $step, $max, $cum ); } return $cache[ $pid ] = apply_filters( 'h23pf_quantity_rules', $rules, $parent ); } /** Error message for a quantity, or '' when it is valid. */ public static function check( $qty, $r, $name ) { $qty = (int) $qty; if ( $qty < $r['min'] ) { return H23PF_Settings::msg( 'qty_min', array( 'product' => $name, 'min' => $r['min'] ) ); } if ( $r['max'] && $qty > $r['max'] ) { return H23PF_Settings::msg( 'qty_max', array( 'product' => $name, 'max' => $r['max'] ) ); } if ( $r['step'] > 1 && 0 !== ( $qty - $r['min'] ) % $r['step'] ) { $ex = array( $r['min'], $r['min'] + $r['step'], $r['min'] + 2 * $r['step'] ); return H23PF_Settings::msg( 'qty_step', array( 'product' => $name, 'step' => $r['step'], 'min' => $r['min'], 'example' => implode( ', ', $ex ) . '…', ) ); } return ''; } /* -------- Quantity input -------- */ public static function input_args( $args, $product ) { $r = self::rules( $product ); if ( ! $r ) { return $args; } $in_cart = isset( $args['input_name'] ) && 0 === strpos( (string) $args['input_name'], 'cart[' ); $args['step'] = $r['step']; $args['min_value'] = ( $in_cart && $r['cumulative'] ) ? 0 : $r['min']; if ( $r['max'] ) { $stock_max = isset( $args['max_value'] ) ? (int) $args['max_value'] : -1; $args['max_value'] = $stock_max > 0 ? min( $stock_max, $r['max'] ) : $r['max']; } if ( ! $in_cart && (int) $args['input_value'] < $r['min'] ) { $args['input_value'] = $r['min']; } return $args; } public static function variation_data( $data, $product, $variation ) { $r = self::rules( $product ); if ( $r ) { $data['min_qty'] = $r['min']; if ( $r['max'] ) { $data['max_qty'] = ( isset( $data['max_qty'] ) && '' !== $data['max_qty'] && (int) $data['max_qty'] > 0 ) ? min( (int) $data['max_qty'], $r['max'] ) : $r['max']; } $data['h23pf_step'] = $r['step']; } return $data; } public static function loop_args( $args, $product ) { $r = self::rules( $product ); if ( $r && $r['min'] > 1 ) { $args['quantity'] = $r['min']; } return $args; } /* -------- Validation -------- */ private static function cart_qty_for( $product_id, $skip_key = '' ) { $total = 0; if ( ! WC()->cart ) { return 0; } foreach ( WC()->cart->get_cart() as $key => $item ) { if ( $key !== $skip_key && (int) $item['product_id'] === (int) $product_id ) { $total += (int) $item['quantity']; } } return $total; } public static function validate_add( $passed, $product_id, $qty, $variation_id = 0 ) { if ( ! $passed ) { return $passed; } $r = self::rules( $product_id ); if ( ! $r ) { return $passed; } $total = $r['cumulative'] ? $qty + self::cart_qty_for( $product_id ) : $qty; $err = self::check( $total, $r, get_the_title( $product_id ) ); if ( $err ) { wc_add_notice( $err, 'error' ); return false; } return $passed; } public static function validate_update( $passed, $key, $values, $qty ) { if ( ! $passed || (int) $qty <= 0 ) { return $passed; } $r = self::rules( $values['product_id'] ); if ( ! $r ) { return $passed; } $total = $r['cumulative'] ? $qty + self::cart_qty_for( $values['product_id'], $key ) : $qty; $err = self::check( $total, $r, get_the_title( $values['product_id'] ) ); if ( $err ) { wc_add_notice( $err, 'error' ); return false; } return $passed; } public static function check_cart() { $groups = array(); foreach ( WC()->cart->get_cart() as $key => $item ) { $pid = (int) $item['product_id']; $r = self::rules( $pid ); if ( ! $r ) { continue; } $name = get_the_title( $pid ); if ( $r['cumulative'] ) { $groups[ $pid ] = array( 'qty' => ( isset( $groups[ $pid ] ) ? $groups[ $pid ]['qty'] : 0 ) + (int) $item['quantity'], 'r' => $r, 'name' => $name, ); } else { $err = self::check( $item['quantity'], $r, $name ); if ( $err && ! wc_has_notice( $err, 'error' ) ) { wc_add_notice( $err, 'error' ); } } } foreach ( $groups as $g ) { $err = self::check( $g['qty'], $g['r'], $g['name'] ); if ( $err && ! wc_has_notice( $err, 'error' ) ) { wc_add_notice( $err, 'error' ); } } } /* -------- Store API -------- */ public static function store_min( $value, $product ) { $r = self::rules( $product ); return ( $r && ! $r['cumulative'] ) ? $r['min'] : $value; } public static function store_max( $value, $product ) { $r = self::rules( $product ); return ( $r && $r['max'] ) ? min( (int) $value > 0 ? (int) $value : $r['max'], $r['max'] ) : $value; } public static function store_step( $value, $product ) { $r = self::rules( $product ); return ( $r && $r['step'] > 1 ) ? $r['step'] : $value; } }