woocommerce / woocommerce

A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
https://woocommerce.com
9.43k stars 10.77k forks source link

Mismatch data type #18719

Closed emrahu closed 6 years ago

emrahu commented 6 years ago

API product price field returns mismatch data type Example link: https://yourwebsite.com/wp-json/wc/v2/products/?consumer_key=ck_***&consumer_secret=cs_***&filter[category]=category

If a product doesn't have a price the value type retrieved is an integer ex: 0 if a product does have a price the value type retrieved is a string ex: "$4.75" Seems like regressed Issue #9220

mikejolley commented 6 years ago

Does not appear to be the case. Also, currencies? Thats not part of our API. Would need more details.

Empty price:

insomnia create product 2018-01-31 19-16-27

Zero price:

insomnia create product 2018-01-31 19-17-03

Set price:

insomnia create product 2018-01-31 19-16-44

All use strings.

emrahu commented 6 years ago

I see it 0 in my case. WP: v4.9.2 WC: v3.2.6 What about currencies? screen shot 2018-01-31 at 2 20 50 pm

mikejolley commented 6 years ago

Need to see the full request and response/product type.

What about currencies?

You said:

if a product does have a price the value type retrieved is a string ex: "$4.75"

emrahu commented 6 years ago

That currency symbol is a typo. Below is the entire json object for that specific product.

Response Header HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 31 Jan 2018 19:35:02 GMT Content-Type: application/json; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Pragma: no-cache X-Robots-Tag: noindex X-Content-Type-Options: nosniff Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages Access-Control-Allow-Headers: Authorization, Content-Type Expires: Wed, 11 Jan 1984 05:00:00 GMT Cache-Control: no-cache, must-revalidate, max-age=0 X-WP-Total: 112 X-WP-TotalPages: 12 Link: https://yourwebsite.com/wp-json/wc/v2/products?consumer_key=ck_***&consumer_secret=cs_***; rel="next" Allow: GET, POST

Request Header GET /wp-json/wc/v2/products/?consumerkey=ck&consumersecret=cs HTTP/1.1 Host: yourwebsite.com Connection: keep-alive Cache-Control: max-age=0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 Upgrade-Insecure-Requests: 1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cookie: cookie

{ "id": 642, "name": "Tip", "slug": "tip", "permalink": "https://yourwebsite.com/menu/tip/", "date_created": "2015-02-26T19:45:13", "date_created_gmt": "2015-02-27T00:45:13", "date_modified": "2015-02-26T19:45:13", "date_modified_gmt": "2015-02-27T00:45:13", "type": "simple", "status": "publish", "featured": false, "catalog_visibility": "hidden", "description": "", "short_description": "", "sku": "", "price": 0, "regular_price": "", "sale_price": "", "date_on_sale_from": null, "date_on_sale_from_gmt": null, "date_on_sale_to": null, "date_on_sale_to_gmt": null, "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#36;</span>0.00</span>", "on_sale": false, "purchasable": true, "total_sales": 3978, "virtual": false, "downloadable": false, "downloads": [], "download_limit": -1, "download_expiry": -1, "external_url": "", "button_text": "", "tax_status": "none", "tax_class": "zero-rate", "manage_stock": false, "stock_quantity": null, "in_stock": true, "backorders": "no", "backorders_allowed": false, "backordered": false, "sold_individually": true, "weight": "", "dimensions": { "length": "", "width": "", "height": "" }, "shipping_required": true, "shipping_taxable": false, "shipping_class": "", "shipping_class_id": 0, "reviews_allowed": false, "average_rating": "0.00", "rating_count": 0, "related_ids": [], "upsell_ids": [], "cross_sell_ids": [], "parent_id": 0, "purchase_note": "", "categories": [], "tags": [], "images": [ { "id": 0, "date_created": "2018-01-31T09:20:09", "date_created_gmt": "2018-01-31T19:20:09", "date_modified": "2018-01-31T09:20:09", "date_modified_gmt": "2018-01-31T19:20:09", "src": "https://yourwebsite.com/wp-content/plugins/woocommerce/assets/images/placeholder.png", "name": "Placeholder", "alt": "Placeholder", "position": 0 } ], "attributes": [], "default_attributes": [], "variations": [], "grouped_products": [], "menu_order": 0, "meta_data": [ {}, {}, {}, {}, {}, {}, {} ], "_links": { "self": [ { "href": "https://yourwebsite.com/wp-json/wc/v2/products/642" } ], "collection": [ { "href": "https://yourwebsite.com/wp-json/wc/v2/products" } ] } }

mikejolley commented 6 years ago

Extensions maybe?

https://github.com/woocommerce/woocommerce/blob/e15f2672705cc2e8ec81f6b592d476d872568fe4/includes/abstracts/abstract-wc-product.php#L274-L276 string.

Setter: https://github.com/woocommerce/woocommerce/blob/e15f2672705cc2e8ec81f6b592d476d872568fe4/includes/abstracts/abstract-wc-product.php#L816-L818

wc_format_decimal returns strings.

claudiosanches commented 6 years ago

I made a few tests:

Creating an empty simple product:

    "price": "",
    "regular_price": "",
    "sale_price": "",

And updating the regular price to zero, so will be a free product:

    "price": "0",
    "regular_price": "0",
    "sale_price": "",

So I can't see how you got that example: https://github.com/woocommerce/woocommerce/issues/18719#issuecomment-362044054

So this sounds like some extension changing the response.

emrahu commented 6 years ago

I found the problem. This particular product has filters attached to it. It's setting an integer 0 if there's no price is set. I updated the filter, now it sets a string 0. In my functions.php

if($product->id == PRODUCT_ID){ return isset($woocommerce->session->agr_donation) ? floatval($woocommerce->session->agr_donation) : "0"; }

Thanks for the help. It took me a while to figure this out.

claudiosanches commented 6 years ago

@emrahu this took while because not following the issue template, there is essential information to helps everyone debug things like that.