I am trying to apply coupon code using WooCommerce Rest API. I am following the way that has been explained in woo API document. But it is not working anyhow.
Coupon code is applied but discount is not applied.
So can you please tell me if I am doing anything wrong or is there any way to do this.
I have tried searching for the solution, but found nothing so far.
Thanks in advance
Below is my data that I am using as Request to place order using API,
$data = [
'payment_method' => 'bacs',
'payment_method_title' => 'Direct Bank Transfer',
'set_paid' => true,
'billing' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe#example.com',
'phone' => '(555) 555-5555'
],
'shipping' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
],
'line_items' => [
[
'product_id' => 93,
'quantity' => 2
],
[
'product_id' => 22,
'variation_id' => 23,
'quantity' => 1
]
],
'shipping_lines' => [
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
]
],
'coupon_lines'=>
[
[
'code'=>'wer',
'id'=>128,
'amount'=>'10.00',
'discount'=>'10'
]
]
];
$response = $woocommerce->post('orders', $data));
echo "<pre>";
print_r($response);
echo "</pre>";
exit;
For anyone looking for a solution, there is none at the moment. but there is a work around that can help you do the discounts. The solution can be found issuecomment-333092453
Solution summary:
getItemsCart() {
const {cartItems} = this.props;
let items = []
for (var i = 0; i < cartItems.length; i++) {
const cartItem = cartItems[i]
let item = {
product_id: cartItem.product.id,
quantity: cartItem.quantity,
total: cartItem.total // The magic happens here, you apply the coupon to your item's price
}
items.push(item)
}
return items;
}
let data = {
customer_id: user.id,
customer_note: null,
billing: customerInfo,
line_items: this.getItemsCart(),
}
API.post('orders', data)
Total is the total price of an item after discount. make sure this quantity is included: total = price_total_of_single_item * quantity_of_item
Related
I've read and re-read the stripe API documentation, and I'm only working with PHP to set a payment, I got satisfactory results with paying with, card, afterpay, alipay ... however when it comes to wallets I didn't understand anything, I noticed that the wallet collects data from the saved cards but when it comes to PHP the documentation doesn't explain anything regarding wallets, They just say you can use Stripe.js but i don't want to use js to get the payment data, i want to use pure php, but when it comes to PHP nothing is mentioned. Does anyone have any idea how the PHP paymentMethod to collect the wallets? I'm collecting payment data like this:
CARD
$payment = $stripe->paymentMethods->create([
'type' => 'card',
'billing_details' => [
'address' => [
'city' => 'nova york',
'country' => 'US',
'line1' => 'asdasd',
'line2' => 'asdfasd',
'postal_code' => '999876',
'state' => 'boom'
],
],
'card' => [
'number' => '4242424242424242',
'exp_month' => 9,
'exp_year' => 2022,
'cvc' => '314',
],
'customer' => 'cus_...'
]);
AFTERPAY
$payment = $stripe->paymentMethods->create([
'type' => 'afterpay_clearpay',
'billing_details' => [
'address' => [
'city' => 'Miami',
'country' => 'US',
'line1' => 'Street 33',
'line2' => 'United aLA',
'postal_code' => '99854332',
'state' => 'Florida'
],
'email' => 'jenny#example.com',
'name' => 'Lois',
'phone' => '+15555555555'
],
'metadata' => ['id' => 'corrego']
]);
EPS
$payment = $stripe->paymentMethods->create([
'type' => 'eps',
'billing_details' => [
'address' => [
'city' => 'Miami',
'country' => 'BR',
'line1' => 'Street 33',
'line2' => 'United aLA',
'postal_code' => '99854332',
'state' => 'SP'
],
'email' => 'jenny#example.com',
'name' => 'Lois',
'phone' => '+15555555555'
],
]);
...
These methods are explained in the documentation on how to implement both Stripe.js and pure PHP, but wallets they don't even mention a way to do it in pure PHP, does anyone have any notions? My native language is not english so maybe this is making it a little difficult to understand the documentation
Hi I am trying to use the woocommerce API and do post requests of customer data and adding an internal customer ID.
Here is my data
I have tried to pass this data through a post request
$extension = "/wp-json/wc/v3/customers?";
$method = "POST";
$data = [
'email' => 'johndoe#example.com',
'first_name' => 'John',
'last_name' => 'Doe',
'username' => 'johndoe',
'billing' => [
'first_name' => 'John',
'last_name' => 'Doe',
'company' => '',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe#example.com',
'phone' => '(555) 555-5555',
],
'shipping' => [
'first_name' => 'John',
'last_name' => 'Doe',
'company' => '',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
],
'meta_data' => [
'proceed_id' => '2'
]
];
$response = woo_api_request($extension, $method, $data);
Below is the response when I commented out the meta data. But as you can see there is a meta data array.
{"id":7,"date_created":"2021-01-04T21:50:58","date_created_gmt":"2021-01-04T21:50:58","date_modified":"2021-01-04T21:51:02","date_modified_gmt":"2021-01-04T21:51:02","email":"johndoe#example.com","first_name":"John","last_name":"Doe","role":"customer","username":"johndoe","billing":{"first_name":"John","last_name":"Doe","company":"","address_1":"969 Market","address_2":"","city":"San Francisco","postcode":"94103","country":"US","state":"CA","email":"john.doe#example.com","phone":"(555) 555-5555"},"shipping":{"first_name":"John","last_name":"Doe","company":"","address_1":"969 Market","address_2":"","city":"San Francisco","postcode":"94103","country":"US","state":"CA"},"is_paying_customer":false,"avatar_url":"https:\/\/secure.gravatar.com\/avatar\/fd876f8cd6a58277fc664d47ea10ad19?s=96&d=mm&r=g","meta_data":[],"_links":{"self":[{"href":"https:\/\/localhost.com\/wp-json\/wc\/v3\/customers\/7"}],"collection":[{"href":"https:\/\/localhost.com\/wp-json\/wc\/v3\/customers"}]}}
But when I try to pass the proceed_id I get an error.This is the error
{"code":"rest_invalid_param","message":"Invalid parameter(s): meta_data","data":{"status":400,"params":{"meta_data":"meta_data is not of type array."}}}
Do I need to register the meta data? Or am I missing something else?
UPDATE 1:
I've tried to change the way I structure my meta_data to this:
'meta_data' => [
['key' => "proceed_id"],
['value' => "4"]
]
It seems to be not throwing an error with array type now, but I am getting a notice :
Notice: Undefined index: key in
wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php
on line 106
According to the WooCommerce REST API documentation you should provide meta_data as an array. Most likely single PHP array gets converted to JSON object instead of an array.
In cases like this, it can help if you try making a request manually, using Postman or some similar tool.
If I understand documentation correctly, this should work:
'meta_data' => [
['proceed_id' => 4],
]
'meta_data' =>
[
[
'key' => "proceed_id",
'value' => "4"
]
]
This method worked. Since the documentation categorizes meta_data as an array instead of an object. You need to pass it as such.
Found the answer here:
https://github.com/claudiosanches/woocommerce-extra-checkout-fields-for-brazil/issues/56
Full parms :
$data = [
'email' => 'johndoe6#example.com',
'first_name' => 'John',
'last_name' => 'Doe',
'username' => 'johndoe6',
'proceed_id' => '2',
'billing' => [
'first_name' => 'John',
'last_name' => 'Doe',
'company' => '',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe#example.com',
'phone' => '(555) 555-5555',
],
'shipping' => [
'first_name' => 'John',
'last_name' => 'Doe',
'company' => '',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
],
'meta_data' => [
['key' => "proceed_id",
'value' => "4"]
]
];
Take for example the following piece of code from WooCommerce API Documentation. What I am trying to do is add some if conditions within the array. For example, I want the payment_details array to be a part of $data based on some if condition. Is this possible? How?
<?php
$data = [
'order' => [
'payment_details' => [
'method_id' => 'bacs',
'method_title' => 'Direct Bank Transfer',
'paid' => true
],
'billing_address' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe#example.com',
'phone' => '(555) 555-5555'
],
'shipping_address' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
],
'customer_id' => 2,
'line_items' => [
[
'product_id' => 546,
'quantity' => 2
],
[
'product_id' => 613,
'quantity' => 1,
'variations' => [
'pa_color' => 'Black'
]
]
],
'shipping_lines' => [
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
]
]
]
];
print_r($woocommerce->post('orders', $data));
?>
The point is, instead of defining the entire array again, I want to put an if condition here:
'order' => [
if ($payment = 'xyz') {
'payment_details' => [
'method_id' => 'bacs',
'method_title' => 'Direct Bank Transfer',
'paid' => true
],
}
else {
'payment_details' => [
'method_id' => 'monopoly',
'method_title' => 'Monopoly',
'paid' => true
],
}
Is it possible to concatenate the array using dot equals? .=
Thanks.
"payment_details" is already part of $data.
To get to it use:
$paymentDetails = $data['order']['payment_details'];
Then you can display it with:
echo $paymentDetails['method_title'];
If you want to use if conditions:
if ($paymentDetails['method_title'] === 'Monopoly money') {
echo 'That will not work';
}
To change the payment details:
$data['order']['payment_details']['method_title'] = 'Changed';
or
$data['order']['payment_details'] = ['method_title' => 'something', 'method_id' => 5, 'paid' => false];
You can define the payment_details in advance and use it in the $data array.
if ($payment = 'xyz') {
$payment_details = array ( 'method_id' => 'bacs',
'method_title' => 'Direct Bank Transfer',
'paid' => true);
}else{
$payment_details = array ( 'method_id' => 'monopoly',
'method_title' => 'Monopoly',
'paid' => true);
}
$data = array (
'order' => array(
'payment_details' => $payment_details,
......
......
)
);
print_r($woocommerce->post('orders', $data));
Answering my own question. This is what I was looking for.
$orderData = [];
if ($payment == 'Monopoly') {
$orderData['order']['payment_details'] = [];
$orderData['order']['payment_details']['method_id'] = 'Monopoly';
$orderData['order']['payment_details']['method_title'] = 'Monopoly';
$orderData['order']['payment_details']['paid'] = true;
$orderData['order']['status'] = 'completed';
}
else {
$orderData['order']['status'] = 'pending';
}
I'm trying to send a post to an API, but the "items should be an array" error is returned.
$response2 = $client2->request('POST', 'https://api.iugu.com/v1/invoices?api_token='.$token, [
'form_params' => [
'email' => $email,
'due_date' => $due_date,
'items' => ['description' =>
'Item Um',
'quantity' => 1,
'price_cents' => 1000
],
'payer' => [
'cpf_cnpj' => $cpf_cnpj,
'name' => $name,
'phone_prefix' => $phone_prefix,
'phone' => $phone,
'email' => $email,
'address' => [
'zip_code' => $zip_code,
'street' => $street,
'number' => $number,
'district' => $district,
'city' => $city,
'state' => $state,
'country' => 'Brasil',
'complement' => $complement
]
]
]
]);
I've tried it in several ways.
['items' => 'description' =>
'Item Um',
'quantity' => 1,
'price_cents' => 1000
],
But none of the ways showed the result I wanted. It's weird, because when I run with PHP and CURL lib this code works like charm.
Any sugestion? Thanks in advance for community!
Each item should be an array with their own description, quantity, and price_cents keys. Wrap each within one more array like so:
'items' => [
[
'description' => 'Item Um',
'quantity' => 1,
'price_cents' => 1000
],
]
You'll get an array of items now:
Array
(
[0] => Array
(
[description] => Item Um
[quantity] => 1
[price_cents] => 1000
)
)
i have something problem in there i want to make my status order when i create an order is on-hold because in default is processing so i try make like this :
$data = array(
'order' => array(
'status' => 'on-hold',
'payment_details' => array(
'method_id' => 'bacs',
'method_title' => $a['method'],
'paid' => true
),
'billing_address' => array(
'first_name' => $a['nama'],
'last_name' => $a['last'],
'address_1' => $a['address_1'],
'address_2' => $a['address_2'],
'city' => $a['city'],
'state' => $a['state'],
'postcode' => $a['postcode'],
'country' => $a['country'],
'email' => $a['email'],
'phone' => $a['phone']
),
'shipping_address' => array(
'first_name' => $a['nama'],
'last_name' => $a['last'],
'address_1' => $a['address_1'],
'address_2' => $a['address_2'],
'city' => $a['city'],
'state' => $a['state'],
'postcode' => $a['postcode'],
'country' => $a['country']
),
'customer_id' => $a['customer_id'],
'line_items' => json_decode($a['testing'], true),
)
);
but its doesnt work, iam using woocommerce V2 and kloon/WooCommerce-REST-API-Client-Library
what should i do ? have someone help me to solve my problem ?
If you know the order_id then you can simply do something like this.
$client = new WC_API_Client( 'http://your-store-url.com', 'ck_enter_your_consumer_key', 'cs_enter_your_consumer_secret', $options );
$client->orders->update_status( $order_id, 'on-hold' )
You can see more example there https://github.com/kloon/WooCommerce-REST-API-Client-Library/blob/master/example/example.php#L50