co-cart / co-cart

🛒 CoCart makes it easy to decouple your WooCommerce store via a customizable REST API that takes the pain out of developing – allowing you to build fast and flexible headless stores.

Home Page:https://cocartapi.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect subtotal price?

rjdeveloper007 opened this issue · comments

When we use number of decimals setting for woocommerce price to set it to 2 decimal point after that when we add any product to cart using CoCart API then subtotal price is adding two leading zeros with product actual price.

For e.g.: IF product price was $3999 then if we use upto 2-decimal price setting in woocommerce then after adding that product to cart using CoCart API subtotal becomes $399900 which is incorrect and causing other issues.

total and tax are coming correctly but not subtotal. Please check attached screenshot.

cocart-subtotal-issue - screenshot
woo-decimal-value - screenshot

@rjdeveloper007 I can replicate the same price and it's correct.

image
image
image

You may find these filters helpful.

<?php
add_filter( 'woocommerce_price_trim_zeros', '__return_true' ); // WooCommerce
add_filter( 'cocart_prepare_money_disable_decimals', '__return_true' ); // CoCart REST API

You can apply it using any code snippet plugin.

Here is what it returns once filter applied.

image

Hello @seb86

We need to allow decimal points on frontend and for that we need to keep 2 decimal points. you were checking on woo frontend not through postman API. I am still facing same issue:

1). I have made number of decimal to 2 from woo setting page.
2). Now I have made product price to 3399
3). Now try to add that product using addToCart CoCart API .
4). Now call second API which is get CoCart cart using CoCart get API from postman you will the issue I am referring to.
5). check for items price which will now show like this: 339900

"price": "339900",

I hope you get my point on the issue.

@rjdeveloper007 I did get your point and I was able to replicate the issue as I said in my reply. This is why I recommended the filters and showed you the results after they are activated. My last response was a screenshot of the API with the price as you want it.

@seb86

Actually the thing is that I am using react on frontend and I need decimal that's why I can't use those filters.

Actually I am using woocommerce deposit with wordpress and I am allowing users to pay in deposit from frontend. So I am doing some calculations from frontend and for that I will need subtotal from CoCart API which in our current case is coming incorrectly. So is there any way to fix that subtotal issue from CoCart API.

Will you be going to fix this in next version? Please let me know and thanks for your quick reply.

incorrect-product-price

I will take a look and run some more test. In the mean time, maybe try using this since you are building in React.

https://www.npmjs.com/package/@woocommerce/currency

@seb86

Thanks a lot for looking into this issue. I will look forward for your reply!

The price and totals can be filtered individually by plugins such as Mix and Match Products which format the values there own way once they have calculated the product container and it's child items.

See here for filters: https://github.com/co-cart/co-cart/blob/dev/plugins/cocart/includes/classes/rest-api/controllers/v2/cart/class-cocart-rest-cart-v2-controller.php#L1213,L1223

The product price and item subtotal are wrapped using cocart_prepare_money_response() function.

@rjdeveloper007 Maybe you can try experimenting while I look at a better solution without breaking current plugins that support these fields.

@seb86

Thanks for that. Sure I will also experimenting on it but just to make something clear that there is no role of Mix and Match Products and other plugins here because this issue is in simple product itself.

Please check attached video. I have just take simple woo and CoCart plugin and setup a new wordpress and tested there.

https://drive.google.com/file/d/1wXcFP0d0DkFGxmV-5-rOplayY2HcJ27d/view?usp=sharing

I was only giving an example. Never mind.

@rjdeveloper007 I have created a work around for you.

https://gist.github.com/seb86/317708b3270a5fcd3bd5a94b450fbc36

Not 100% that it's accurate but it's on the right path. There is a reason why the values return the way they do differently but I will go into more detail on that in a doc.

I did come across this library called Dinero.js that you could use instead since you are using React.

Hope it helps you at least for now.

@seb86

Thanks for quick reply. Sure I will check with Dinero Js but for now I have tested by adding you provided hooks and it added decimal but incorrectly. For e.g: If price was: $3399.12 then after applying your filter it becomes: $3399.9912

Please check attached screenshot.
cocart_monetary_value

As I said it's not going to be 100% accurate specially when you are entering a custom price.

Here is an altered version of the cocart_monetary_value_formatted function I provided in the Gist.

function cocart_monetary_value_formatted( $value ) {
	$decimal_separator = wc_get_price_decimal_separator();
	$thousand_separator = wc_get_price_thousand_separator();
	$decimals = wc_get_price_decimals();

	$value = number_format( wc_format_decimal( $value ), 2, '.', '' ); // Set manually as English notation without thousands separator. Use values above to set as WC store settings.
	$value = wc_trim_zeros( $value );

	$decimal_cost = substr( $value, intval( '-' . $decimals ) );

	$value = substr_replace( $value, $decimal_separator . $decimal_cost, intval( '-' . $decimals ) );

	return $value;
}

@seb86

Are you going to release new version for above updates. For now I have added in child theme function.php file but just want to confirm.

No update. This is more a custom job. I'm just using it as a test case.

Hello @seb86 ,

Decimal point is causing many issues. I have used your updated code but still its not working with all calculations. For e.g. when we use add-fee CoCart PRO API it will adding two extra leading zeros at the end.

To reproduce this issue:
1). Change woo setting to accept 2 decimal point.
2). Now call API(Add a Fee) and add $100 as price for extra addon and then call get-cart API. You will see that issue:

when we add: 100
It is showing: 10000

Can you please take a look and suggest on this.

Also Is there any issue in officially supporting decimal points with CoCart API's as its woocommerce core feature. Just want to know on this.

@rjdeveloper007 When you add a sum of 100, WooCommerce returns it formatted as $100.00. In JSON it returns with a HTML wrapper.

With CoCart we remove all the HTML, the currency symbol and decimal point which then gives you 10000 so it can be used well for any framework.

If you are setting a custom price when adding an item such as $33.99 you need to apply it like 33.99 so that it can return as 3399. What your doing is applying it as 3399 which returns 339900 because WooCommerce is formatting the value based on what you entered. It can't detect where the decimal point is because you haven't set it.

If you use this filter add_filter( 'cocart_prepare_money_disable_decimals', '__return_true' ); it will round up/down depending on the value to the nearest number so if used it will return 34.

I hope I have now explained your issue.