最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - woocommerce specific quantities for product

programmeradmin1浏览0评论

I have a product that I want to allow to add to cart only in numbers that I allow. The numbers arent exactly ordered so I want to create an array of the allowed quantities per product. Is there a way to allow only the numbers I allow?

I have a product that I want to allow to add to cart only in numbers that I allow. The numbers arent exactly ordered so I want to create an array of the allowed quantities per product. Is there a way to allow only the numbers I allow?

Share Improve this question asked Oct 29, 2019 at 12:50 Asaf HadadAsaf Hadad 12 bronze badges 4
  • Put a look here: stackoverflow/questions/49531738/… on the first answer second step of message. Don't forget change min for max. – Sam Commented Oct 29, 2019 at 13:04
  • I dont need min and max i need specific numbers – Asaf Hadad Commented Oct 29, 2019 at 13:08
  • Aaahhh sorry! My bad english haha. But are you good in coding? with this code, you can easy create a value for each product ids and controle min/max at the same time for allow the button "add to cart". – Sam Commented Oct 29, 2019 at 13:22
  • I want to add a select box with values that are in an array. lets say the numbers are 5 8 10 13 15 18 and so on. I know how to create the array but what i dont know is how to only allow those numbers – Asaf Hadad Commented Oct 29, 2019 at 13:32
Add a comment  | 

1 Answer 1

Reset to default 1

Try this in your functions.php:

add_filter( 'woocommerce_quantity_input_args', 'control_product_qty_input', 20, 2 );
function control_product_qty_input( $args, $product ) {

    $settings = array('49' => 3, '48' => 10); // for product id 49 3 products set
    foreach ($settings as $k => $v) {
        if($k === $product->get_id()){
            $args['max_value'] = $v;
            $args['min_value'] = $v;
        }
    }

    return $args;
}

Note: Woocommerce hide the input number when min and max are same.

Note #2: This code'll work only on single product view.

EDIT: You can have multiple values in dropdown like this: (But I still have a little problem with the existing input and I do not catch why. Try this code with me:)

add_filter( 'woocommerce_quantity_input_args', 'control_product_qty_input', 20, 2 );
function control_product_qty_input( $args, $product ) {

    $settings = array('49' => array(5, 8, 10, 13), '48' => array(2, 4));
    $options = '';

    $defaults = array(
        'input_name'    => 'quantity',
        'input_value'   => '1',
        'max_value'     => apply_filters( '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; margin-right:10px;', $product )
    );

    $html = '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">';

    foreach ($settings as $k => $v) {
        if($k === $product->get_id()){
            foreach ($v as $v) {
                $options .= '<option value="' . $v . '">' . $v . '</option>';
            }
        }
    }
    $html .= $options . '</select></div>';

    echo $html;
}
发布评论

评论列表(0)

  1. 暂无评论