Support » Plugin: Cart REST API for WooCommerce - CoCart Lite » Allow float amount

  • ResolvedPlugin Contributor Mohammad Etemaddar

    (@sefid-par)


    Thanks again for great plugin and great updates.
    One of our needs is to add and reduce amount in float. it’s used for example to sell fruits in Kg.
    I have changed the plugin’s code in order to access float amount. and it worked by changing some absint( $data[‘quantity’] ) to floatval( $data[‘quantity’] ) and change the type of amount from integer to float for update and add item.

    It would be appreciated if you do this modification in new update.

Viewing 15 replies - 1 through 15 (of 18 total)
  • Plugin Author Sébastien Dumont

    (@sebd86)

    How does being able to add a float value work for quantity?

    Plugin Contributor Mohammad Etemaddar

    (@sefid-par)

    We can do it in this way. Of course my code checks if the product measurement is not quantized then does the following.

    // 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.5;
    }
    add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple products
    
    function jk_woocommerce_quantity_input_args( $args, $product ) {
        	$args['step'] = 0.5;
    	return $args;
    }
    // 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');
    Plugin Author Sébastien Dumont

    (@sebd86)

    I understand the possibility now. I will look further into this.

    Plugin Contributor Mohammad Etemaddar

    (@sefid-par)

    Thanks a lot. Is this included in version 2.0.11?

    Plugin Author Sébastien Dumont

    (@sebd86)

    No. As I said I will be looking into this further. Will let you know if it’s included in the next release.

    Your welcome to push your change to the CoCart GitHub repository and I will test the change you have made. You will also be given credit for your contribution.

    How does that sound?

    Plugin Contributor Mohammad Etemaddar

    (@sefid-par)

    Oh sure. Thank you.
    I forked it. I’ll do the changes and push.

    Plugin Author Sébastien Dumont

    (@sebd86)

    Here are my test results before and after your request change. Please let me know if it looks correct to you with the totals.

    ### Before with a singular quantity value.

    json
    {
        "4818e3493e7621149b6906333721e341": {
            "addons": [],
            "key": "4818e3493e7621149b6906333721e341",
            "product_id": 32,
            "variation_id": 0,
            "variation": [],
            "quantity": 1,
            "data": {},
            "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",
            "line_tax_data": {
                "subtotal": {
                    "13": 8.4
                },
                "total": {
                    "13": 8.4
                }
            },
            "line_subtotal": 42,
            "line_subtotal_tax": 8.4,
            "line_total": 42,
            "line_tax": 8.4,
            "product_name": "Hoodie",
            "product_title": "Hoodie",
            "product_price": "£42.00"
        }
    }
    

    ### After with a float value for quantity.

    json
    {
        "4818e3493e7621149b6906333721e341": {
            "addons": [],
            "key": "4818e3493e7621149b6906333721e341",
            "product_id": 32,
            "variation_id": 0,
            "variation": [],
            "quantity": 2.032123,
            "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",
            "line_tax_data": {
                "subtotal": {
                    "13": 17.0698332
                },
                "total": {
                    "13": 17.0698332
                }
            },
            "line_subtotal": 85.34916600000001,
            "line_subtotal_tax": 17.07,
            "line_total": 85.34916600000001,
            "line_tax": 17.07,
            "data": {},
            "product_name": "Hoodie",
            "product_title": "Hoodie",
            "product_price": "£42.00"
        }
    }
    
    Plugin Contributor Mohammad Etemaddar

    (@sefid-par)

    Yes Sébastien. It’s true. It needs also a test for update too. I think it has different route in code.

    Of course we do not calculate tax. But it seems ok. I just used 0.5 for amount change.

    Plugin Author Sébastien Dumont

    (@sebd86)

    Tax needs to be tested along with several quantity examples. I do not want to make this change and it ends up breaking for those who are using it as it is now. So please test test test.

    Plugin Author Sébastien Dumont

    (@sebd86)

    Next release ready for public testing.

    https://github.com/co-cart/co-cart/releases/tag/v2.0.13-rc.1

    Plugin Contributor Mohammad Etemaddar

    (@sefid-par)

    Thanks a lot.
    I think tax calculation is ok. Because the calculation is from woocommerce core. And is officially allowed in the docs.

    I try to install and test.
    Thank you.

    Plugin Author Sébastien Dumont

    (@sebd86)

    I hope so. The filter you showed only affects the default values for the quantity input on the frontend. Nothing else so can’t really say 100%!

    Plugin Author Sébastien Dumont

    (@sebd86)

    How is the testing going @sefid-par ?

    Plugin Contributor Mohammad Etemaddar

    (@sefid-par)

    Yes, It worked Sébastien.

    Plugin Author Sébastien Dumont

    (@sebd86)

    I filtered the woocommerce_stock_amount filter in CoCart. Can you make sure after disabling the filter you did yourself that it still works. Thank you.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Allow float amount’ is closed to new replies.