WooCommerce REST API - GET Custom Checkout Page Fields for Order - php

I'm using the WooCommerce REST API (http://woocommerce.github.io/woocommerce-rest-api-docs/#introduction) and have added a new field (shipping_phone) to the checkout page using their example here:
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
This new field appears on the checkout page and I can populate this and it is saved to my order - here's how it appears in the WordPress admin page for an order:
I'm now trying to download the Order with the shipping_phone data by using this GET request:
/wp-json/wc/v1/orders/3454
but the shipping_phone field isn't included in the downloaded fields. Here's the Shipping fields from the GET response:
"shipping": {
"address_1": "45 Jones Road",
"address_2": "",
"city": "Bondi",
"company": "BS Consulting Pty Ltd",
"country": "AU",
"first_name": "Betty",
"last_name": "Sanders",
"postcode": "2026",
"state": "NSW"
},
It's also not located anywhere else in the JSON data. How do I do a GET request to get an Order which includes this new custom field?

You can use the WooCommerce REST API v2 or later to get the Order Meta Data which includes these custom fields. If you GET an Order:
wp-json/wc/v3/orders/3454
it will include a meta_data array with these custom field values:
"meta_data" :
[
{
"id" : 4672,
"key" : "_shipping_method",
"value" : [ "flat_rate:1" ]
},
{
"id" : 4673,
"key" : "_shipping_phone",
"value" : "08 9632 7412"
}
]

Related

Not able to post some fields using createDraftListing API

We have implemented the createDraftListing API using this documentation to create new listing on etsy.
But we are not able to post below field using above API because in documentation request parameter this field are not present.
Also we have added the item weight, item length, item width and item height in request feed and posting this value to above api call but still showing blank on products.
Request feed:
{
"title": "this is my title",
"description": "this is my description",
"quantity": "100",
"state": "active",
"price": 44.55,
"who_made": "collective",
"when_made": "2020_2022",
"listing_type": "physical",
"shipping_profile_id": 12345678,
"processing_min": 1,
"processing_max": 1,
"item_weight": "0.71",
"item_weight_unit": "lb",
"item_length": 5.5,
"item_width": "7.5",
"item_height": 0.75,
"item_dimensions_unit": "in",
"has_variations": false,
"language": "en-US",
"is_taxable": true,
"should_auto_renew": true,
"is_customizable": true,
"is_personalizable": true,
"personalization_is_required": true,
"personalization_char_count_max": 1,
"personalization_instructions": "",
"taxonomy_id": 2279
}
So please let us know any api to post above fields to etsy?
Thanks.
first you can get id shipping profile id in get shipping profile api. I try and success

WooCommerce product WPML translation inserted by REST API doesn't include variations of original product

Unfortunately I'm working with wordpress and woocommerce, what I'm trying to accomplish is to insert products and relative translations using the woocommerce REST API, here's the scenario:
First, i create a simple product
{
"name": "Dummy",
"type": "variable",
"attributes": [
{
"id": 2,
"name": "Color",
"position": 1,
"variation": "true",
"options": [
"Bianco",
"Nero"
]
},
{
"id": 3,
"name": "Size",
"position": 2,
"variation": "true",
"options": [
"A",
"B",
"C"
]
}
]
}
This is a variable product, so I need to create its variations as well
The product created return its id, let's say 10
Then I create several product variations like this:
{
"regular_price": "10.10",
"stock_quantity": 10,
"attributes": [
{
"id": 2,
"name": "Color",
"option": "Bianco"
},
{
"id": 3,
"name": "Size",
"option": "A"
}
]
}
So I have a product with variations, now i want to create a translation of the product with wpml, according to wmpl rest api specifications, I create a product translation like this:
{
"name": "Dummy en",
"type": "variable",
"lang": "en",
"translation_of": 10
}
I specified the language en, the main language is it
I included translation_of with the id of the original product
What happens is that the english product is created, but without variations
What I have tried:
Several issues similar to this are solved by the troubleshooting page of wpml, where I can generate missing variations but I need this to be done automatically, products are created automatically, so relative translations too must be
If I enter product page from wordpress back end and I press the update button, variations are created and adjusted, this word for the original product and the variation, but again, I need this to be done automatically
I tried to modify and insert the translation in almost all ways, adding/removing sku, id, attributes, categories ecc.
Any hints? Thanks

Wordpress REST API: filter by custom taxonomy

I have a custom post type called "products" and it has a taxonomy called "domain".
I am using the WP Rest API and AngularJS 1.5.1. I am using a service to get the product posts. This part works fine.
getProducts: function () {
return $http.get('URL/wp-json/wp/v2/products').then(function (result) {
return result.data;
});
}
This returns an array of products, each being (partial):
{
"id": 29,
"date": "2017-10-09T16:21:56",
"date_gmt": "2017-10-09T16:21:56",
"guid": {
"rendered": "URL/?post_type=product&p=29"
},
"modified": "2017-10-09T19:58:32",
"modified_gmt": "2017-10-09T19:58:32",
"slug": "product-name",
"status": "publish",
"type": "product",
"link": "URL/product/product-name/",
"title": {
"rendered": "product name"
},
"content": {
"rendered": "some content",
"protected": false
},
"featured_media": 30,
"template": "",
"domain": [
2
],
...
}
As you can see, my taxonomy term "domain" is represented by a number, "2" in this case.
However, Using Postman, if I do a GET as described here:
URL/wp-json/wp/v2/products?filter[domain]=2
I still get all my products back, not just the ones with domain=2 as I expected.
What am I missing here?
It seems WP removed the filter parameter in v4.7. You can get a plugin to add the feature back in here.
I just merged that function into my functions.php and it works like this:
/wp-json/wp/v2/products?filter[taxonomy_name]&filter[term]=taxonomy-slug

Podio PHP: category is empty when no value for item

We are using the Podio PHP library to create forms for our users. These forms load their data from Podio trough the Podio PHP library. A form is linked to a specific Podio Item ID for this.
With this ID we get the fields and their possible values. Most of them are Category fields. We get the values for this trough:
<?php
$item_data = PodioItem::get_basic( $podio_item_id );
$car_types = $item_data->fields["car-type"]->config['settings']['options'];
var_dump( $car_types );
?>
But the curious thing is, if the item in Podio has no value selected for this category, getting the options fail. It's empty. It doesn't exists.
How do we populate a form with values even if the item has no options selected for it?
Podio doesn't store empty value, it's empty, it doesn't exist, so there is nothing to store. In fact, what you are probably looking for is Application definition, which has list fields. Then category field has config with list of possible values and id's and colors.
Like this:
"fields": [
{
"status": "active",
"type": "category",
"field_id": 81772,
"label": "Status",
"config": {
"required": true,
"label": "Status",
.....some other config values .....
"settings": {
"multiple": false,
"options": [
{
"status": "deleted", <= example of deleted category option
"text": "Not groomed",
"id": 13,
"color": "DCEBD8"
},
{
"status": "active", <= example of active category option
"text": "Open",
"id": 1,
"color": "FFD5C2"
},
.... <= other category options goes here
You can read more about Apps here: https://developers.podio.com/doc/applications/get-app-22349

Not able to place the order using paypal in magento 2 rest

I am developing an e-commerce mobiloe application using magento 2 rest apis only.This is the flow for making the REST API calls for order placement.
1.Create a cart
api -->{{url}}/index.php/rest/V1/carts/mine
This api will return a unique cart id
2.Add products to cart
api --> {{url}}/index.php/rest/V1/carts/mine/items
body ->
{
"cart_item": {
"quote_id": cartId,
"sku": skuName,
"qty": 1
}
}
3. Estimate Shipping Methods
url --> {{url}}/index.php/rest/V1/carts/mine/estimate-shipping-methods
body ->
{
"address": {
"region": "Trivandrum",
"region_id": 12,
"region_code": "CA",
"country_id": "IN",
"street": [
"Amstor house",
"Eramam"
],
"telephone": "5656565454",
"postcode": "670390",
"city": "Kazhakuttam",
"firstname": "Peter",
"lastname": "K",
"same_as_billing": 0,
"save_in_address_book": 0
}
}
This will return all possible shipping methods based on shipping address.In my case the result is
[
{
"carrier_code": "freeshipping",
"method_code": "freeshipping",
"carrier_title": "Free Shipping",
"method_title": "Free",
"amount": 0,
"base_amount": 0,
"available": true,
"error_message": "",
"price_excl_tax": 0,
"price_incl_tax": 0
}
]
4)Save shipping information
url --> {{url}}/index.php/rest/V1/carts/mine/shipping-information
body data ->
{
"addressInformation": {
"shipping_address": {
"region": "Trivandrum",
"region_id": 12,
"region_code": "CA",
"country_id": "IN",
"street": [
"Amstor house",
"Eramam"
],
"telephone": "5656565454",
"postcode": "670390",
"city": "Kazhakuttam",
"firstname": "Peter",
"lastname": "K",
},
"billing_address": {
"region": "Trivandrum",
"region_id": 12,
"region_code": "CA",
"country_id": "IN",
"street": [
"Amstor house",
"Eramam"
],
"telephone": "5656565454",
"postcode": "670390",
"city": "Kazhakuttam",
"firstname": "Peter",
"lastname": "K",
},
"shipping_method_code": "freeshipping",
"shipping_carrier_code": "freeshipping"
}
}
This will return all possible payment methods. Here i am using paypal_express for payment.
5. Payment using paypal plugin
Here i will pay the amount using paypal cordova plugin.Also configured the IPN [{{url}}/paypal/ipn/]in paypal account
This api will return the following data,
{
"client": {
"environment": "sandbox",
"paypal_sdk_version": "2.14.4",
"platform": "Android",
"product_name": "PayPal-Android-SDK"
},
"response": {
"create_time": "2016-11-19T05:25:46Z",
"id": "PAY-5VS11410F5341972MLAX6ETA",
"intent": "sale",
"state": "approved"
},
"response_type": "payment"
}
5.Save payment and place order
url --> {{url}}/index.php/rest/V1/carts/mine/payment-information
data ->
{
"cartId": 3,
"billingAddress‌​": {
"region": "Trivandrum",
"region_id": 12,
"region_code": "CA",
"country_id": "IN",
"street": [
"Amstor house",
"Eramam"
],
"telephone": "5656565454",
"postcode": "670390",
"city": "Kazhakuttam",
"firstname": "Peter",
"lastname": "K"
},
"paymentMethod": {
"method": "paypal_express"
}
}
But this api will returning
{
"message": "PayPal gateway has rejected request. Invalid token (#10410: Invalid token)."
}
Is there any api missing in the above flow for capturing payments.Please help me.
Paypal Express payment method doesn't support online capturing. There is no way to get a full order creation flow like on Checkout via Magento API interface. It is impossible to change the order state and process payments. As a workaround try the following:
Create a custom payment method
Enable for REST API only(Not on website checkout page)
While making payment using rest api use this method (after successful payment using you android/ios SDK)
After placing the order make send transaction id(PAY-xxxxx) return by paypal sdk payment to save trasaction.(tell your server side tio implement this call).
I am writting a complete atrticle regarding this step by step. I will let you know when it is done.
Place Order by PayPal Rest API
For the Place order by the Paypal rest API, you need an active cart with shipping and billing address
By default, we need to follow the few setups for placing the order
• Step 1. Create an empty cart
• Step 2. Add products to the cart
• Step 3. Set the shipping address
• Step 4. Set billing address
• Step 5. Set the delivery method
• Step 6. Apply a coupon (if you have)
• Step 7. Set the payment method
• Step 8. Place order
After steps 6 follow below APIs
We need to call the below APIs one by one for placing the order with Paypal
Create Paypal Express Token
URL : {you website
url}/rest/default/V1/paypalapi/createpaypalexpresstoken
Method: POST
Set Bearer Token in the Hearer (if customer ) for the guest user no need to set it
Content-type : JSON
Body For guest users:
{
"cart_id":"5QWFYZdyccucvgD2QMLDCp5fhjmaH2xg",
"cancel_url":"cancel_url",
"return_url":"return_url"
}
Body For Customer user:
{
"cart_id": 22,
"cancel_url": "cancel_url",
"return_url": "return_url"
}
You will get the response like this :
[
{
"code": 200,
"token": "EC-4MD50688YD296870K",
"paypal_urls":{
"start": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-4MD50688YD296870K&useraction=commit",
"edit": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=continue&token=EC-4MD50688YD296870K"
}
}]
Explanation
you need to redirect the customer to the {start URL } and after making the payment, PayPal will redirect the user with two params in the redirect URL "payer_id" and "token" Both values you need to call in the below API
Set Payment Method On Cart
URL : {you website
url}/rest/default/V1/paypalapi/setpaymentmethodoncart
Method: POST
Set Bearer Token in the Hearer (if customer ) for the guest user no
need to set it
Content-type : JSON
Body For guest users:
{
"cart_id": "5QWFYZdyccucvgD2QMLDCp5fhjmaH2xg",
"payer_id": "9T3GV67ZSL378",
"token": "EC-4MD50688YD296870K",
"payment_method": "paypal_express"
}
Body For Customer user:
{
"cart_id": 22,
"payer_id": "9T3GV67ZSL378",
"token": "EC-4MD50688YD296870K",
"payment_method": "paypal_express",
"customer_id": 141
}
You will get the response like this :
[
{
"code": 200,
"selected_payment_method": {
"code": "paypal_express",
"title": "PayPal Express Checkout"
}
}
]
Place Order
URL : {you website url}/rest/default/V1/paypalapi/placeorder
Method: POST
Set Bearer Token in the Hearer (if customer ) for the guest user no
need to set it
Content-type : JSON
Body For guest users:
{ "cart_id": "5QWFYZdyccucvgD2QMLDCp5fhjmaH2xg" }
Body For Customer user:
{ "cart_id": 22, "customer_id": 141 }
You will get the response like this :
[ { "code": 200, "order_number": 000000142 } ]
Here is the link to the Paypal Rest API module
https://github.com/santosh-gaggle/paypal-rest-api
In case someone still looking the solution.
In the time I'm answering this, You will need to create a Magento 2 module to process the payment ID.
After you receive the response from in example Paypal android SDK.
Below is the JSON format that you can send to Magento endpoint :
for logged user : PUT /V1/carts/mine/order
for guest : PUT /V1/guest-carts/:cartId/order
Referrence : http://devdocs.magento.com/swagger
The "paypal_express_payment_payload" is just a custom attribute to hold the paypal payment response previously from android SDK.
{
"paymentMethod": {
"method": "paypal_express",
"additional_data": {
"paypal_express_payment_payload": "{\"create_time\":\"2017-06-15T23:13:52Z\",\"id\":\"PAY-2LB41725NU654612TLFBRIUQ\",\"intent\":\"sale\",\"state\":\"approved\"}"
}
}
}
To process the "paypal_express_payment_payload" data, you can implement a Interceptor in your Magento 2 module :
di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Paypal\Model\Express">
<plugin name="mymodule_magento_paypal_model_express_plugin"
type="Mycompanyorpersonal\Mymodule\Plugin\Paypal\Model\Express"
sortOrder="99999"
disabled="false" />
</type>
</config>
Mycompanyorpersonal\Mymodule\Plugin\Paypal\Model\Express.php
You can find the full PHP codes in my following gist : https://gist.github.com/feelinc/de817030e00adc7ff7001de1807c1835
If you use the below to run a post query replace runPostQuery with your curl request. this will pass a token that already has been successful to magento 2.
$payment['paymentMethod'] = ['method' =>'paypal_express',
'additional_data' => array (
'paypal_express_checkout_token' => $request->query->get('token'),
'paypal_express_checkout_redirect_required' => false,
'paypal_express_checkout_payer_id' => $request->query->get('PayerID')
)];
$completedPayment = $this->runPostQuery('carts/mine/payment-information', $headers, json_encode($payment));
You will need to create a plugin to add the last transaction id to the payment see the above comment, but the above payload to payment-information will allow you to get past _placeOrder function in Paypal\Model\Express.php
The paypal_express_checkout_token is the token passed back to the browser from paypal same as PayerId this allows to check the payment, which will return successful and not require a redirect, but is not the payment reference just the action token.

Categories