Woocommerce REST API Retrieve Order By Transaction ID? - wordpress

I got some problem with Woocommerce REST API.
My goal is simple, to check whether the order is exist or not by transaction ID. So far, parameter that works is order status.
this is my script:
$param = array('status' => 'on-hold', 'transaction_id' => 'XXXXXXXXXXXXXX');
//OR $param = array('search' => 'XXXXXXXX');
$cek = $woocommerce->get('orders', $param);
print_r($cek);
but when I add more parameter like 'transaction_id', the results is weird, resulting all orders.

You can get order data using following way
$order_details = $woocommerce->get('orders/1'); // 1 = Transaction id
I hope it will work for you.

Related

Why wordpress get_post_meta not returning anything, when my DB has value?

database data
return array( get_post_meta(12785, 'attribute_settings', true));
or
return array( get_post_meta("12785", 'attribute_settings', true));
it returns [""]
changing the key with another value, it returns the value in the database.
You mention that if you change the key, you get the value you want. Looking at your screenshot, you've got duplicate keys for attribute_settings. How you got a duplicate key in there, I'm not sure (did you add them directly in phpMyAdmin? Using update_post_meta() will update the record if it exists)
Now, I haven't seen the case before where a duplicate post_id:meta_key pair exists, but I'd wager it's returning at the NULL value since it's first. Delete that entry and you should be good.
Documentation & Function Reference
Function
Linked Description
update_post_meta()
Updates a post meta field based on the given post ID.
Try this below code.
$attribute_settings = array();
$attribute_settings[] = get_post_meta( 12785, 'attribute_settings', true );
return $attribute_settings;

Get list of Webhooks from Woocommerce

I have built a Wordpress plugin that among other things, creates several Woocommerce Webhooks upon activation. This is done using internal API classes and functions, as per below:
function createWebhook($userID,$topic,$secret,$deliveryURL,$status)
{
$webhook = new WC_Webhook();
$webhook->set_user_id($userID); // User ID used while generating the webhook payload.
$webhook->set_topic( $topic ); // Event used to trigger a webhook.
$webhook->set_secret( $secret ); // Secret to validate webhook when received.
$webhook->set_delivery_url( $deliveryURL ); // URL where webhook should be sent.
$webhook->set_status( $status ); // Webhook status.
$save = $webhook->save();
return $save;
}
This works well.
What I want to be able to do is remove these Webhooks upon deactivation of the plugin. Is there any way to fetch the Woocommerce Webhooks via the internal Wordpress or Woocommerce API, so I can loop through and remove the relevant ones?
I would just remove all Webhooks where the delivery URL has a domain of xyz.com. This part is straight-forward, I just don't know how to fetch the Webhooks.
I don't want to use the external Woocommerce API, which requires an API key and HTTP requests.
Thanks
I ended up querying the database to get the webhooks, which looks to be working well. I'm not sure there's any other way. Please let me know if there is!
global $wpdb;
$results = $wpdb->get_results( "SELECT webhook_id, delivery_url FROM {$wpdb->prefix}wc_webhooks" );
foreach($results as $result)
{
if(strpos($result->delivery_url, 'domain.com') !== false)
{
$wh = new WC_Webhook();
$wh->set_id($result->webhook_id);
$wh->delete();
}
}
#greg's answer points you in the right direction, but the returned data is just an array of ID's for each webhook, to get more data you need to parse those ID's into webhook objects - which has protected props, but public getter methods, like so:
$data_store = \WC_Data_Store::load( 'webhook' );
$webhooks = $data_store->search_webhooks([ 'status' => 'active', 'paginate' => true ] );
$_items = array_map( 'wc_get_webhook', $webhooks->webhooks );
$_array = [];
foreach( $_items as $_item ){
$_array[] = [
'id' => $_item->get_id(),
'name' => $_item->get_name(),
'topic' => $_item->get_topic(),
'delivery_url' => $_item->get_delivery_url(),
'secret' => $_item->get_secret(),
];
}
You can get an array of all webhook IDs with the following:
$data_store = WC_Data_Store::load( 'webhook' );
$webhooks = $data_store->search_webhooks();
That's what WooCommerce does when building the table list:
https://github.com/woocommerce/woocommerce/blob/master/includes/admin/class-wc-admin-webhooks-table-list.php

Multiple orderby within Criteria

I currently use the Criteria to filter a collection of objects. But when I want to achieve with 2 orderBy fields, only the first is considered. I do not understand.
$events = new Collections\ArrayCollection($results);
$dateFrom = new \DateTime($date);
$dateTo = new \DateTime(date('Y-m-d H:i:s', strtotime($date . ' + 1 day')));
$criteria = Criteria::create()
->where(Criteria::expr()->eq('activity', $activity));
$criteria->orderBy(array(
"time" => "ASC",
"title" => "ASC"
));
How can I make it work with two orderby fields and not only the first ?
Thank you in advance for any answers !
Your code is correct, assuming that $criteria is then applied correctly to the ArrayCollection.
Judging by the names of your fields you're trying to order by, it's possible that title doesn't affect the order because time doesn't repeat among the elements in the collection.
If this isn't the case, please provide more information on the results you're getting and I will update my answer.
Update (in response to additional data provided that has since been deleted):
You are sorting the collection correctly. The problem is that you're then feeding the ArrayCollection into the Paginator, which apparently cannot sort by more than one field.
There's an open issue in Knp's tracker about this: https://github.com/KnpLabs/KnpPaginatorBundle/issues/109.

Drupal commerce create paypal wps order programmatically

I m trying to create a paypal order programmatically but I need the redirection key. The paypal WPS module gets this data from the $order->data['payment_redirect_key'] like this:
// Return to the payment redirect page for processing successful payments
'return' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),
However i cannot find where the payment_redirect_key is created (i.e. which function creates it) in order to create it programmatically. Any help is appreciated.
My goal is to bypass the default drupal commerce checkout mechanism
I'm trying to do the same with no luck yet, but I find out where the payment_redirect_key is created. You can found it in the function commerce_payment_redirect_pane_checkout_form in the commerce/modules/commerce_payment/includes/commerce_payment.checkout_pane.inc function commerce_payment_redirect_pane_checkout_form, line 360 of the current version (http://cgit.drupalcode.org/commerce/tree/modules/payment/includes/commerce_payment.checkout_pane.inc#n360)
Basically, is this:
$order->data['payment_redirect_key'] = drupal_hash_base64(time());
commerce_order_save($order);
EDIT
After a couple days working on this, I found the solution in just a few lines of code. I use this function as a page callback of a menu item (something like product/%sku). Here is the code:
<?php
function custom_module_create_order($sku) {
global $user;
$product = commerce_product_load_by_sku($sku);
$order = ($user->uid) ? commerce_order_new($user->uid, 'checkout_checkout') : commerce_cart_order_new();
// Save to get the Order ID.
commerce_order_save($order);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
commerce_line_item_save($line_item);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
// Select here the payment method. Usually something like:
// [module_name]|commerce_payment_[module_name]
$order->data['payment_method'] = 'commerce_sermepa|commerce_payment_commerce_sermepa';
commerce_order_save($order);
// Set status to order checkout to go to the payment platform redirect.
commerce_order_status_update($order, 'checkout_payment');
drupal_goto('checkout/' . $order->order_id);
}

Woocommerce Dynamic Pricing - manually inserting pricing rules

I am using the Woocommerce Dynamic Pricing plugin, which is very useful but does not easily allow me to ammend pricing updates via. the database.
Looking at the sample data,
{"set_5339c459a78e7":
{"conditions_type":"all",
"conditions":{"1":{"type":"apply_to","args":{"applies_to":"everyone"}}},
"collector":{"type":"product"},
"mode":"block",
"date_from":"",
"date_to":"",
"rules":{"1":{"from":"","to":"","type":"price_discount","amount":""}},
"blockrules": {"1":{
"from":"2",
"adjust":"1",
"type":"fixed_adjustment",
"amount":"0.26","repeating":"yes"}}}}
it seems that the meta requires set_* to be specific with the product meta, or it will not apply correctly.
Revising the code, I notice this:
$terms = get_terms('product_cat', array('hierarchical' => false, 'hide_empty' => false, 'parent' => 0));
foreach ($terms as $item_id => $item) {
$set_index = $item->term_id;
$name = 'set_' . $set_index;
}
This is bizarre to me, as the term_id appears to be a 13 character alphanumeric string than the expected bigint. Could anyone explain how I can reproduce this string for when I manually update my tables?
I had a look through the code and I found that in /admin/admin-init.php where it specifies the AJAX handlers, this is how it comes up with new set names:
function woocommerce_pricing_product_admin_create_empty_ruleset() {
global $wc_product_pricing_admin;
$wc_product_pricing_admin->create_empty_ruleset( uniqid('set_') );
die();
}
As you can see it just uses PHP's uniqid function prefixed by nothing but "set_" so in the case of product rules it appears to be just a random ID.
Hope that helps.

Resources