How to set status order in woocommerce API - php

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

Related

assertJSON test not working even though response JSON matches

My assertJSON test isn't working and says I get the below error
Here's the test code
$user = Sanctum::actingAs(
User::factory()->create(),
['*']
);
$customer = Customer::first();
$response = $this->getJson('api/customers/' . $customer->slug);
$response->assertStatus(200);
$response->assertJson(
[
'data' => [
'uuid' => $customer->uuid,
'customer_name' => $customer->customer_name,
'email' => $customer->email,
'slug' => $customer->slug,
'street_1' => $customer->street_1,
'street_2' => $customer->street_2,
'city' => $customer->city,
'postcode' => $customer->postcode,
'telephone_number' => $customer->telephone_number,
'county' => $customer->county,
'customer_type' => $customer->customerType,
'archived' => $customer->archived ? 'Yes' : 'No',
]
]
);
And here is the customer resource
public function toArray($request)
{
return [
'uuid' => $this->uuid,
'customer_name' => $this->customer_name,
'email' => $this->email,
'slug' => $this->slug,
'street_1' => $this->street_1,
'street_2' => $this->street_2,
'city' => $this->city,
'postcode' => $this->postcode,
'telephone_number' => $this->telephone_number,
'county' => $this->county,
'customer_type' => $this->customerType,
'archived' => $this->archived ? 'Yes' : 'No',
];
}
The 2 JSON arrays are exactly the same and the other tests are working and everything on the front end is working just fine too.
More of the error

How do I insert meta data to my customers in woocommerce using an object array?

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"]
]
];

Auth.net eCheck implementation using John Conde's AuthnetXML Class

I have an existing implementation of this class processing subscriptions by credit card but I now need to add the facility to accept payment by eCheck but I am unsure of how to change this portion of the code:
'payment' => array(
'creditCard' => array(
'cardNumber' => '4111111111111111',
'expirationDate' => '2016-08'
)
),
I have come up with the following from referencing the AIM guide pdf and AIM guide XML pdf
'payment' => array(
'bankAccount' => array( // x_method equivalent ?
'routingNumber' => '', // x_bank_aba_code equivalent ?
'accountNumber' => '', // x_bank_acct_num equivalent ?
'nameOnAccount' => '', // x_bank_acct_name equivalent ?
'bankName' => '', // x_bank_name equivalent ?
'echeckType' => 'WEB' // x_echeck_type equivalent
/*
x_bank_acct_type has no equivalent ?
*/
)
),
but there appears to be some discrepancies between the required fields?
Any pointers before I start on this would be greatly appreciated.
Looking at Authorize.Net's documentation this should work:
'payment' => array(
'bankAccount' => array(
'accountType' => '', // 'checking'
'routingNumber' => '',
'accountNumber' => '',
'nameOnAccount' => ''
)
),
Following on from John's answer this is the complete code I used to solve this for anyone finding this through a google search:
$xml->ARBCreateSubscriptionRequest(array(
'subscription' => array(
'name' => 'SubscriptionName',
'paymentSchedule' => array(
'interval' => array(
'length' => '1',
'unit' => 'months'
),
'startDate' => date('Y-m-d', time()), // Format: YYYY-MM-DD
'totalOccurrences' => '9999' // To submit a subscription with no end date (an ongoing subscription), this field must be submitted with a value of 9999
),
'amount' => $eCart1->GrandTotal(), // total monthly subscription
'payment' => array(
'bankAccount' => array(
'accountType' => ((isset($_POST["accountType"]))?$_POST["accountType"]:""), // options available are checking or businessChecking in this instance
'routingNumber' => ((isset($_POST["routingNumber"]))?$_POST["routingNumber"]:""),
'accountNumber' => ((isset($_POST["accountNumber"]))?$_POST["accountNumber"]:""),
'nameOnAccount' => ((isset($_POST["nameOnAccount"]))?$_POST["nameOnAccount"]:""),
'echeckType' => ((isset($_POST["echeckType"]))?$_POST["echeckType"]:"") // if businessChecking is chosen then 'CCD' else 'WEB'
)
),
'customer' => array(
'id' => "'".$_SESSION['clientID']."'",
'email' => "'".((isset($_POST["email"]))?$_POST["email"]:"")."'"
),
'billTo' => array(
'firstName' => "'".((isset($_POST["firstname"]))?$_POST["firstname"]:"")."'",
'lastName' => "'".((isset($_POST["lastname"]))?$_POST["lastname"]:"")."'",
'company' => "'".((isset($_POST["company"]))?$_POST["company"]:"")."'",
'address' => "'".((isset($_POST["street1"]))?$_POST["street1"]:"")."'",
'city' => "'".((isset($_POST["city"]))?$_POST["city"]:"")."'",
'state' => "'".((isset($_POST["state_province"]))?$_POST["state_province"]:"")."'",
'zip' => "'".((isset($_POST["postcode"]))?$_POST["postcode"]:"")."'"
)
)
));

How to descend several levels in find results array

Calling Cake's find method on my table like this:
$this->Client->find('all',['recursive' => -1])
returns
array(
(int) 0 => array(
'Client' => array(
'id' => '1',
'name' => 'Intel Corporation',
'website' => 'www.intel.com',
'address' => '2200 Mission College Blvd.',
'city' => 'Santa Clara',
'state' => 'CA',
'zip' => '95054'
)
),
(int) 1 => array(
'Client' => array(
'id' => '3',
'name' => 'Motorola Mobility LLC',
'website' => 'www.motorola.com',
'address' => '222 W. Merchandise Mart Plaza',
'city' => 'Chicago',
'state' => 'IL',
'zip' => '60654'
)
),
(int) 2 => array(
'Client' => array(
'id' => '4',
'name' => 'Nokia',
'website' => 'www.nokia.com',
'address' => '6000 Connection Drive',
'city' => 'Irving',
'state' => 'TX',
'zip' => '75039'
)
),)
What I want is to remove the redundant 'Client' array level:
array(
(int) 0 => array(
'id' => '1',
'name' => 'Intel Corporation',
'website' => 'www.intel.com',
'address' => '2200 Mission College Blvd.',
'city' => 'Santa Clara',
'state' => 'CA',
'zip' => '95054'
),
(int) 1 => array(
'id' => '3',
'name' => 'Motorola Mobility LLC',
'website' => 'www.motorola.com',
'address' => '222 W. Merchandise Mart Plaza',
'city' => 'Chicago',
'state' => 'IL',
'zip' => '60654'
),
(int) 2 => array(
'id' => '4',
'name' => 'Nokia',
'website' => 'www.nokia.com',
'address' => '6000 Connection Drive',
'city' => 'Irving',
'state' => 'TX',
'zip' => '75039'
),
);
I'd like to do this in native Cake, with a param call or something, but if I have to do it in a php array function, please explain. It's basically data for paginating.
That is just how Cake does it.
Are you sure moving the inner array is even necessary? You can just use it as is...
Anyway, moving the inner array is rather trivial:
foreach ($clients as & $client) {
$client = $client['Client'];
}

Magento: create bundle order programmatically

I have this code: http://pastebin.com/iFwyKM7G
Inside an event-observer class which executes after the customer registered. It creates an order automatically and it works for simple products. However I cannot figure out for the life of me how to make it work with Bundled product!
How can I make this work with a bundled product?
I DID IT!
I changed my code to the following:
$customer = Mage::getSingleton('customer/customer')->load($observer->getCustomer()->getId());
$session = Mage::getSingleton('adminhtml/session_quote');
$order_create_model = Mage::getSingleton('adminhtml/sales_order_create');
Mage::log($customer->debug(), null, 'toszodj_meg.log');
//$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
Mage::log($customer->getDefaultBillingAddress()->debug(), null, 'toszodj_meg.log');
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
$session->setCustomerId((int) $customer->getId());
$session->setStoreId((int) $storeId);
$orderData = array(
'session' => array(
'customer_id' => $customer->getId(),
'store_id' => $storeId,
),
'payment' => array(
'method' => 'banktransfer',
'po_number' => (string) '-',
),
// 123456 denotes the product's ID value
'add_products' =>array(
'2' => array(
'qty' => 1,
'bundle_option' => array(
2 => 2,
1 => 1,
),
'bundle_option_qty' => array(
2 => 1,
1 => 1,
),
),
),
'order' => array(
'currency' => 'EUR',
'account' => array(
'group_id' => $customer->getGroupId(),
'email' => (string) $customer->getEmail(),
),
'comment' => array('customer_note' => 'API ORDER'),
'send_confirmation' => 1,
'shipping_method' => 'flatrate_flatrate',
'billing_address' => array(
'customer_address_id' => $customer->getDefaultBillingAddress()->getEntityId(),
'prefix' => $customer->getDefaultBillingAddress()->getPrefix(),
'firstname' => $customer->getDefaultBillingAddress()->getFirstname(),
'middlename' => $customer->getDefaultBillingAddress()->getMiddlename(),
'lastname' => $customer->getDefaultBillingAddress()->getLastname(),
'suffix' => $customer->getDefaultBillingAddress()->getSuffix(),
'company' => $customer->getDefaultBillingAddress()->getCompany(),
'street' => $customer->getDefaultBillingAddress()->getStreet(),
'city' => $customer->getDefaultBillingAddress()->getCity(),
'country_id' => $customer->getDefaultBillingAddress()->getCountryId(),
'region' => $customer->getDefaultBillingAddress()->getRegion(),
'region_id' => $customer->getDefaultBillingAddress()->getRegionId(),
'postcode' => $customer->getDefaultBillingAddress()->getPostcode(),
'telephone' => $customer->getDefaultBillingAddress()->getTelephone(),
'fax' => $customer->getDefaultBillingAddress()->getFax(),
),
'shipping_address' => array(
'customer_address_id' => $customer->getDefaultShippingAddress()->getEntityId(),
'prefix' => $customer->getDefaultShippingAddress()->getPrefix(),
'firstname' => $customer->getDefaultShippingAddress()->getFirstname(),
'middlename' => $customer->getDefaultShippingAddress()->getMiddlename(),
'lastname' => $customer->getDefaultShippingAddress()->getLastname(),
'suffix' => $customer->getDefaultShippingAddress()->getSuffix(),
'company' => $customer->getDefaultShippingAddress()->getCompany(),
'street' => $customer->getDefaultShippingAddress()->getStreet(),
'city' => $customer->getDefaultShippingAddress()->getCity(),
'country_id' => $customer->getDefaultShippingAddress()->getCountryId(),
'region' => $customer->getDefaultShippingAddress()->getRegion(),
'region_id' => $customer->getDefaultShippingAddress()->getRegionId(),
'postcode' => $customer->getDefaultShippingAddress()->getPostcode(),
'telephone' => $customer->getDefaultShippingAddress()->getTelephone(),
'fax' => $customer->getDefaultShippingAddress()->getFax(),
),
),
);
$order_create_model->importPostData($orderData['order']);
$order_create_model->getBillingAddress();
$order_create_model->setShippingAsBilling(true);
$order_create_model->addProducts($orderData['add_products']);
$order_create_model->collectShippingRates();
$order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
$order_create_model
->initRuleData()
->saveQuote();
$order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
$order_create_model->setPaymentData($orderData['payment']);
$order_create_model
->importPostData($orderData['order'])
->createOrder();
$session->clear();
Mage::unregister('rule_data');
Mage::log('Order Successfull', null, 'siker_bammer.log');
And it works! thought the customer doesn't get notified. which iam trying to figure out now.

Categories