• Resolved Yeshan Perera

    (@yeshansachithak)


    Hi Team,

    I’m using below code to change quantity steps based on custom attribute on product page.

    
    // Add min value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_min', 'ysn_min_decimal');
    function ysn_min_decimal($val){
        return 1;
    }
    
    // Removes the WooCommerce filter, that is validating the quantity to be an int
    remove_filter('woocommerce_stock_amount', 'intval');
    
    // Add a filter, that validates the quantity to be a float
    add_filter('woocommerce_stock_amount', 'floatval');
    
    add_filter('woocommerce_quantity_input_step', 'ysn_quantity_input_step', 10, 2);
    function ysn_quantity_input_step($step, $product){
        $product_id = $product->id;
        $meta = get_post_meta($product_id);
        $sku_data = array();
        if(isset($meta['_product_attributes'][0])){
            $sku_data = unserialize($meta['_product_attributes'][0]);
        }
        $sku_unit_value = "";
        if(isset($sku_data['unit'])){
            $sku_unit_value = $sku_data['unit']['value'];
        }
        if(isset($sku_data['category'])){
            $sku_unit_value = $sku_data['category']['value'];
        }
        if(strtolower($sku_unit_value) == "kg"){
            $step = 0.25;
        }
        return $step;
    }

    But, it doesn’t work for me. Can you please help me on this.

    Thank you!
    Yeshan Perera

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Hi Yeshan!

    $product_id = $product->id;

    The id is called incorrectly — please use get_id() and see if that makes a difference.

    Cheers!

    Plugin Support Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Howdy!

    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘woocommerce_quantity_input_step not working properly’ is closed to new replies.