$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Add max-value to hooked quantity selector in woocommerce|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Add max-value to hooked quantity selector in woocommerce

matteradmin10PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I have hooked the Quantity Selector to change the design and it works fine. But the problem is I can't set the max value dynamically. If I have only 2 items in the stock I need to set max-value as 2. Currently It's not working like that.

Following is the hook that I am using,

function woocommerce_quantity_input($data = null) {

global $product;
if (!$data) {
  $defaults = array(
    'input_name'   => 'quantity',
    'input_value'   => '1',
    'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
    'step'         => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
    'style'         => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
  );
}else {
  $defaults = array(
    'input_name'   => $data['input_name'],
    'input_value'   => $data['input_value'],
    'step'         => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
    'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
    'style'         => apply_filters( 'cw_woocommerce_quantity_style', 'float:left;', $product )
  );
}

if ( ! empty( $defaults['min_value'] ) )
  $min = $defaults['min_value'];
else 
  $min = 1;


if ( ! empty( $defaults['max_value'] ) )
   $max = $defaults['max_value'];
else 
   $max = 15;


if ( ! empty( $defaults['input_value'] ) )
   $defval = $defaults['input_value'];
else 
   $defval = 1; 


if ( ! empty( $defaults['step'] ) )
   $step = $defaults['step'];
else 
   $step = 1

?>
<div class="woo-single-selection">
  <p>Quantity <?php //echo $product->get_stock_quantity(); ?></p>
  <div>
    <span class="woo-qty-num woo-quantity-input-number-decrement">-</span> 
    <input class="woo-quantity-input-number input-text qty text cw_qty" type="number" step="<?php echo $step ?>" value="<?php echo $defval ?>" min="<?php echo $min ?>" max="<?php echo $max ?>" name="<?php echo esc_attr( $defaults['input_name'] ) ?>" title="<?php echo _x( 'Qty', 'Product Description', 'woocommerce' ) ?>"  pattern="[0-9]*" inputmode="numeric"><span class="woo-qty-num woo-quantity-input-number-increment">+</span>
  </div>
</div>
<?php
}

How can set the maximum value as the available stock quantity value?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I have hooked the Quantity Selector to change the design and it works fine. But the problem is I can't set the max value dynamically. If I have only 2 items in the stock I need to set max-value as 2. Currently It's not working like that.

Following is the hook that I am using,

function woocommerce_quantity_input($data = null) {

global $product;
if (!$data) {
  $defaults = array(
    'input_name'   => 'quantity',
    'input_value'   => '1',
    'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
    'step'         => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
    'style'         => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
  );
}else {
  $defaults = array(
    'input_name'   => $data['input_name'],
    'input_value'   => $data['input_value'],
    'step'         => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
    'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
    'style'         => apply_filters( 'cw_woocommerce_quantity_style', 'float:left;', $product )
  );
}

if ( ! empty( $defaults['min_value'] ) )
  $min = $defaults['min_value'];
else 
  $min = 1;


if ( ! empty( $defaults['max_value'] ) )
   $max = $defaults['max_value'];
else 
   $max = 15;


if ( ! empty( $defaults['input_value'] ) )
   $defval = $defaults['input_value'];
else 
   $defval = 1; 


if ( ! empty( $defaults['step'] ) )
   $step = $defaults['step'];
else 
   $step = 1

?>
<div class="woo-single-selection">
  <p>Quantity <?php //echo $product->get_stock_quantity(); ?></p>
  <div>
    <span class="woo-qty-num woo-quantity-input-number-decrement">-</span> 
    <input class="woo-quantity-input-number input-text qty text cw_qty" type="number" step="<?php echo $step ?>" value="<?php echo $defval ?>" min="<?php echo $min ?>" max="<?php echo $max ?>" name="<?php echo esc_attr( $defaults['input_name'] ) ?>" title="<?php echo _x( 'Qty', 'Product Description', 'woocommerce' ) ?>"  pattern="[0-9]*" inputmode="numeric"><span class="woo-qty-num woo-quantity-input-number-increment">+</span>
  </div>
</div>
<?php
}

How can set the maximum value as the available stock quantity value?

Share Improve this question asked Dec 24, 2018 at 5:39 RameshRamesh 1451 silver badge9 bronze badges 2
  • @vikrantzilpe thank you for your reply sir. I don't want to use plugins here. I just want to set the max value equal to the available stock quantity value – Ramesh Commented Dec 24, 2018 at 6:35
  • @vikrantzilpe It makes to add minimum and maximum value to the product. And also I found this line on the article that you shared "The above solution will only work on the WooCommerce product page. Which means, even after placing above code snippet in functions.php file, the customer can update the quantity on Cart page. Also, it will be applied to all the product available on your store." I want to set the stock amount as the maximum value sir – Ramesh Commented Dec 24, 2018 at 7:32
Add a comment  | 

1 Answer 1

Reset to default 2
function max_quantity_single( $max, $product ) {
    $stock = get_post_meta( get_the_ID(), '_stock', true );
    $max = $stock;  
    return $max;
}
add_filter( 'woocommerce_quantity_input_max','max_quantity_single', 10, 2 );

Add this hook in your functions.php or plugin file.

Post a comment

comment list (0)

  1. No comments so far