Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter kamilen12

    (@kamilen12)

    Is there any solution for this? :/

    Thread Starter kamilen12

    (@kamilen12)

    I’m sorry, but I don’t know what plugin you are writing about. Where have you included name of the plugin? I can’t see any plugin below your post :/.

    Many thanks!

    Thread Starter kamilen12

    (@kamilen12)

    Hello,
    I think I have repaired it. I’ve came back to my previous code, which let customers to order decimal in every product, but I’ve add some code:

    
    add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2 );
       
    function bloomer_woocommerce_quantity_changes( $args, $product ) {
       
    	if( $product->is_type( 'simple' ) ){  
          $args['step'] = 1; // Increment/decrement by this value (default = 1)
    	}   
       return $args;
       
    }
    

    So now it’s working as it should (at least it seems so).
    I’m writing it for the others. Maybe someone will have similar problem.

    Thread Starter kamilen12

    (@kamilen12)

    I have resolved it. Right code:

    
    // Add min value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_min', 'min_decimal', 10, 2 );
    function min_decimal($val, $product) {
        if ( $product->is_type( 'variable' ) ) {
            return 0.1;
        }
        return $val;
    }
     
    // Add step value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal', 10, 2 );
    function nsk_allow_decimal($val, $product) {
        if ( $product->is_type( 'variable' ) ) {
            return 0.01;
        }
        return $val;
    }
     
    // 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 unit price fix when showing the unit price on processed orders
    add_filter('woocommerce_order_amount_item_total', 'unit_price_fix', 10, 5);
    function unit_price_fix($price, $order, $item, $inc_tax = false, $round = true) {
        $qty = (!empty($item['qty']) && $item['qty'] != 0) ? $item['qty'] : 1;
        if($inc_tax) {
            $price = ($item['line_total'] + $item['line_tax']) / $qty;
        } else {
            $price = $item['line_total'] / $qty;
        }
        $price = $round ? round( $price, 2 ) : $price;
        return $price;
    }
    
    Thread Starter kamilen12

    (@kamilen12)

    Thank you very much! Your code really help me. For others: All you have to do is correct two things. You have to delete the “$” before ending return:

    
    // Add min value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_min', 'min_decimal', 10, 2 );
    function min_decimal($val, $product) {
        if ( $product->is_type( 'variable' ) ) {
            return 0.1;
        }
        <strong>$return</strong> $val;
    }
     
    // Add step value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal', 10, 2 );
    function nsk_allow_decimal($val, $product) {
        if ( $product->is_type( 'variable' ) ) {
            return 0.01;
        }
        <strong>$return</strong> $val;
    }
    

    (I have bolded them) And it works as it should.
    Thank you one more time!

    Thread Starter kamilen12

    (@kamilen12)

    Thank you very much! I’ve resolved my problem, but I have another one. I think it’s easier but I don’t know much php. I would be grateful if you could help me. It’s not related with the plugin, but I think for you it’s very easy. I have this code:

    
    // Add min value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_min', 'min_decimal');
    function min_decimal($val) {
        return 0.1;
    }
     
    // Add step value to the quantity field (default = 1)
    add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal');
    function nsk_allow_decimal($val) {
        return 0.01;
    }
     
    // 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 unit price fix when showing the unit price on processed orders
    add_filter('woocommerce_order_amount_item_total', 'unit_price_fix', 10, 5);
    function unit_price_fix($price, $order, $item, $inc_tax = false, $round = true) {
        $qty = (!empty($item['qty']) && $item['qty'] != 0) ? $item['qty'] : 1;
        if($inc_tax) {
            $price = ($item['line_total'] + $item['line_tax']) / $qty;
        } else {
            $price = $item['line_total'] / $qty;
        }
        $price = $round ? round( $price, 2 ) : $price;
        return $price;
    }
    

    And it’s working perfectly, but I would like to make this code work only for variable products. I think it’s about change somewhere in ‘woocommerce_quantity_input_step’ to include the “variable” somewhere, but I dont know where and if it’s correct. I’ve tried many combinations but they weren’t working correctly.

    Thank you much in advance!

    • This reply was modified 6 years, 3 months ago by kamilen12.
Viewing 6 replies - 1 through 6 (of 6 total)