Woocommerce update_status() Only Works for Admin Orders - wordpress

Very odd issue. I have built a plugin (for a client, not a public one) that creates a couple of REST endpoints that a shipping service (Shippo) passes data to (all this is fine and working). The plugin takes the data, gets an Order Number, and attempts to set a Completed status.
The code only works perfectly if the Order is from the Admin account. But any other custom account and the status will not save. An order note is generated claiming the status was changed, and I can set meta data on the order. But the status will not change.
In the code, I have attempted to use update_status() and set_status() both with and without .save();
add_action('rest_api_init', function () {
register_rest_route('lab/v1', '/shipment_label_created_shippo', ['methods' => 'POST', 'callback' => 'rest_shipment_label_created_shippo', 'permission_callback' => '__return_true', ]);
register_rest_route('lab/v1', '/shipment_label_updated_shippo', ['methods' => 'POST', 'callback' => 'rest_shipment_label_updated_shippo', 'permission_callback' => '__return_true', ]);
register_rest_route('lab/v1', '/shipment_tracking_updated_shippo', ['methods' => 'POST', 'callback' => 'rest_shipment_tracking_updated_shippo', 'permission_callback' => '__return_true', ]);});
// Label Created in Shippo (transaction_created)
function rest_shipment_label_created_shippo($data){
$log = new WC_Logger();
$response = new WP_REST_Response("Failed");
$response->set_status(200);
$meta_order_number = $data['data']['metadata'];
$woo_order_number = explode(" ", $meta_order_number);
$order_number = intval($woo_order_number[1]);
if ($shippo_status == "SUCCESS" && !is_null($shippo_order_id)) {
if( class_exists('WC_Order') && $order_number > 0 ) {
$order = wc_get_order($order_number);
if ($order) {
$payment_method = $order->get_payment_method();
$payment_method_title = $order->get_payment_method_title();
$date_paid = $order->get_date_paid();
// Update the Order Meta as well for tracking
$update_order_was_shipped = update_post_meta($order_number, '_order_was_shipped', $date_shipped);
// Add the Shippo Transaction ID
$update_transactionid = update_post_meta($order_number, '_shippo_transaction_id', $shippo_transaction_id);
if ($payment_method == "invoice" && !is_null($date_paid))
{
$order->update_status('shipped-invoiced');
$saved_order_id = $order->save();
}
else
{
$order->update_status('completed');
$saved_order_id = $order->save();
}
}
Thanks in advance for any thoughts you might have!

Just in case someone else stumbles onto this I wanted to answer the question.
The reason it appears as though only Admin orders (orders I placed) are working is because I never had the order tab up. Our client, always had the Order open that they were updating via a 3rd party. When the Rest API was hit by the 3rd party, order details were not saved because the order was locked.
This makes sense of course, but was not top of mind for me.

Related

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

Can Stripe have status as active and trialing at the same time?

Is it possible to have status as active and trialing both?
Following code works for active subscription but I also want to retrieve data having status as "trialing".
$all_subs = \Stripe\Subscription::all(
array(
"plan" => $group_plan_id,
"status" => "active",
'limit' => 100
)
);
The documentation specifically says "one of...", so you can only choose one status per call. You will need to either make two calls with each status type, or use the status "all" to get ALL of the types (which will also return cancelled, past_due, and unpaid) and then filter out the ones you don't want.
$all_subs = \Stripe\Subscription::all(
array(
"plan" => $group_plan_id,
"status" => "all",
"limit" => 100
)
);
$statuses = array('active','trialing');
foreach ($all_subs->data as $sub) {
if (!in_array($sub->status,$statuses)) {
continue;
}
// do some code here
}
Source: Stripe API Reference

Funds not getting transferred between paypal sandbox accounts

I am trying to integrate ci-merchant with codeigniter by using paypal express driver. I followed the steps as given in the document. I am able to get my site redirected to paypal sandbox payment site where the Total cost and everything is available. But when I try paying using paypal sandbox account by logging in, it does not show me the paypal balance even though I have enough in my account. When I click pay now, It redirects me to the return url successfully with token and payer ID in the url. But no fund is getting transferred.Not sure where I am going wrong.
url : ../retSuccess?token=EC-01M80248BN787213M&PayerID=9WLBBV9LM6TPA
$this->load->model('mainmodel');
$this->mainmodel->orderDetails();
$query = $this->mainmodel->retrieveOrder();
foreach ($query as $row){
$transaction_id = $row['transaction_id'];
}
$this->load->library('merchant');
$this->merchant->load('paypal_express');
$settings = array(
'username' => 'merchant_api1.canada.com',
'password' => '1369782104',
'signature' =>'AmTaSH3lkRIYxxjxUjB.1zqxD0cRA1hfMGBX2dV9h4DkcYQcjGtqDaYa',
'test_mode' => true);
$this->merchant->initialize($settings);
$params = array(
'amount' => $this->input->post('price'),
'currency' => 'CAD',
'description'=> $this->input->post('model_no'),
'return_url' => base_url('payment/transaction/'.$transaction_id),
'cancel_url'=> base_url('payment/cancel'));
$response = $this->merchant->purchase($params);
}
public function transaction(){
$transaction_id = $this->uri->segment(3);
$this->load->model('mainmodel');
$query = $this->mainmodel->loadTransaction($transaction_id);
foreach ($query as $row){
$price = $row['price'];
$desc = $row['model_no'];
$trans_id = $row['transaction_id'];
}
$this->load->library('merchant');
$this->merchant->load('paypal_express');
$params = array(
'amount' => '21.3',
'currency' => 'CAD',
'description' => 'SP66');
$response = $this->merchant->purchase_return($params);
if ($response->success())
{
$data['gateway_reference'] = $response->reference();
$data['model_no'] = $this->session->userdata('model_no');
$data['category'] = $this->session->userdata('category');
$data['specs'] = $this->session->userdata('specs');
$data['quantity'] = $this->session->userdata('quantity');
$newData = array('status'=>'complete',
'reference'=>$data['gateway_reference']);
$this->db->where('transaction_id',$trans_id);
$this->db->update('transactions',$newData);
$this->load->view('templates/success',$data);
}else{
$data['message'] = $response->message();
//$this->db->where('transaction_id',$trans_id);
//$this->db->delete('transactions');
$this->load->view('templates/failure',$data);
}
}
If I understand you correctly, the sandbox account should have a buy and seller account. You need to check the seller account to see the purchase from the buyer account.
I don't think the problem is from CI Merchant.
Make sure that the CI Merchant is calling PayPal's DoExpressCheckout API. This is the last API call of the Express Checkout, and this is the API that actually completes the payment and transfers the money.
With ci-merchant you need to call purchase_return on your return page to complete the payment, it looks like you just put everything on the initial (pre-paypal) page which won't do anything.

Drupal Commerce Line Items: alter the price?

I have to add to my cart some line items with a custom amount.
The commerce product is saved with price = 0, and my module compute the price and add the line item to the cart/order, but i dont understand how to set programmatically the price.
I've read about using Rules, but I need my module to be able to set/alter the price, without invoking rules.
I've tryed with an entity wrapper, i tryed to alter the line item created with commerce_product_line_item_new(), but nothing, when the line item gets into the cart always has the original product price (in my case, 0).
How to alter a line item price programmatically?
My code so far looks like:
// For debugging, this function is called by hook_menu()
function mymodule_test($product_id)
{
global $user;
$user = user_load($user->uid);
$order = commerce_cart_order_load($user->uid);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new(
$product,
1,
0,
array(
),
'cover'
);
$line_item_wrapper = entity_metadata_wrapper("commerce_line_item", $line_item);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'base_price',
array(
'amount' => 1234,
'currency_code' => 'EUR',
'data' => array(),
),
TRUE
);
$insert_line_item = commerce_cart_product_add($user->uid, $line_item_wrapper->value(), FALSE);
return 'done';
}
The strange thing, is that I tryed to adapt the code of commerce_line_item_unit_price_amount() found in commerce/modules/line_item/commerce_line_item.rules.inc, but this test:
<?php
global $user;
$product = commerce_product_load(4); // my commerce product for test
$line_item = commerce_product_line_item_new(
$product,
1,
0,
array(
),
'cover' // I do have this line_items type
);
// manually set amount and component name
$amount = 1234;
$component_name = 'base_price'; // tryed with discount, nothing change
$wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
// Calculate the updated amount and create a price array representing the
// difference between it and the current amount.
$current_amount = $unit_price['amount'];
$updated_amount = commerce_round(COMMERCE_ROUND_HALF_UP, $amount);
$difference = array(
'amount' => $updated_amount - $current_amount,
'currency_code' => $unit_price['currency_code'],
'data' => array(),
);
// Set the amount of the unit price and add the difference as a component.
$wrapper->commerce_unit_price->amount = $updated_amount;
$wrapper->commerce_unit_price->data = commerce_price_component_add(
$wrapper->commerce_unit_price->value(),
$component_name,
$difference,
TRUE
);
$insert_line_item = commerce_cart_product_add($user->uid, $line_item, FALSE);
?>
still fail, the line_item get into the cart but with the original price of the referenced product.
Any idea?
For those people who don't want to use rules and hope to alter the price directly. Here is my solution:
// Alter the price in list and single product page.
function my_module_commerce_product_calculate_sell_price_line_item_alter($line_item){
$price = 100; //1 dollar
$line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $price;
}
// Alter the price in cart & order.
function my_module_commerce_cart_line_item_refresh($line_item, $order_wrapper){
$price = 100; //1 dollar
$line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $price;
// Alter the base_price component.
$line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount'] = $price;
}
If you're looking to ignore whatever previous values have been saved to a line item and recalculate the total from your new amount the function you're looking for is commerce_line_item_rebase_unit_price.
Set the new amount value and then run your line item through there, save the line item and the order:
$line_item_wrapper->commerce_unit_price->amount = 13;
commerce_line_item_rebase_unit_price($line_item_wrapper->value());
commerce_line_item_save($line_item_wrapper->value());
I struggled through this issue all day today and final figured out the correct path to altering line items prices. The problem is that, even if you successfully change the line item price to a custom value, on the next page refresh the cart will reset the line items to match the original product price. Take a look at the commerce_cart_order_refresh() function for details. This function is executed every time an order/cart is loaded on the page and there is no way around it.
It turns out that the proper way to alter a line item price is to either use Rules or to implement the hook_commerce_cart_line_item_refresh() function. Either way, Drupal Commerce need to be able to apply the alteration logic each time the cart/order is loaded.
I ended up creating a custom field in the Line Item where I stored the custom price value I wanted. I then used a Pricing Rule to copy the custom price value to the product price value whenever the cart is refreshed.
The following blog post was very helpful in figuring this out. It shows you how to add a custom field to a line item type and how to setup a pricing rule to copy the custom amount to the unit price.
http://commerceguys.com/blog/using-custom-line-items-provide-donation-feature-drupal-commerce
Recently I had to implement a donation form in Commerce but the Commerce Express Checkout module doesn't handle custom line items. Since it was a donation and all (who is trying to screw the house?), I felt it appropriate to pass the donation amount as a 3rd parameter in URL the Express Checkout module provides. Here is how I went about hacking the module:
I added a new entry to the router:
$items['commerce-express-checkout/%/%/%'] = array(
'title' => 'Express Checkout w/ extra argument',
// 'page callback' => 'commerce_express_checkout_create_order',
'page callback' => 'commerce_express_checkout_create_order_extra',
'page arguments' => array(1, 2, 3),
'access arguments' => array('access checkout'),
'type' => MENU_CALLBACK,
);
I duplicated and tweaked the default callback and tacked '_extra' to it. Note that the "data" property seems to be a static variable store for occasions just such as this and persists the life of the line item.
function commerce_express_checkout_create_order_extra($product_id, $token, $amount) {
if (drupal_hmac_base64($product_id, drupal_get_private_key().drupal_get_hash_salt()) == $token && is_numeric($amount)) {
global $user;
$product = commerce_product_load($product_id);
$product->commerce_price['und'][0]['amount'] = (int)$amount;
$order = ($user->uid) ? commerce_order_new($user->uid, 'checkout_checkout') : commerce_cart_order_new();
commerce_order_save($order);
$price = array('amount' => commerce_round(COMMERCE_ROUND_HALF_UP, $amount), 'currency_code' => commerce_default_currency());
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
$line_item->data = array('und' => array('0' => $price));
commerce_line_item_save($line_item);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
$order->data['type'] = 'commerce_express_checkout_order';
commerce_order_save($order);
drupal_goto('checkout/' . $order->order_id);
return "";
}
return "";
}
Here is the part that ended up being most tricky simply due to the learning curve and not knowing what the heck function to use:
/**
* Implements hook_commerce_cart_line_item_refresh().
*/
function commerce_express_checkout_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
if ($line_item->commerce_product['und'][0]['line_item_label'] == 'DONATE' || $line_item->commerce_product['und'][0]['product_id'] == '11') {
$price = array('amount' => commerce_round(COMMERCE_ROUND_HALF_UP, $line_item->data['und'][0]['amount']), 'currency_code' => commerce_default_currency());
$line_item->commerce_unit_price = array('und' => array('0' => $price));
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(), 'base_price', $price, TRUE
);
}
}
Every single time the cart is modified, it refreshes and attempts to set the products in the cart to their in-code prototype. It seems pretty inefficient to me too, but I could be missing something.
This post pointed me in the right direction for programmatically altering a drupal commerce line item by using hook_commerce_cart_line_item_refersh(). However, some of the answers here are either outright wrong, or very inefficient and sloppy.
This would be a correct working solution for altering the line item type in Drupal Commerce:
/*
* implements hook_commerce_cart_line_item_refresh()
*
*/
function MYMODULE_commerce_cart_line_item_refresh($line_item, $order_wrapper){
$line_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$new_price = 100; //I use a function to calculate the value of $new_price
if(!empty($new_price)){
$line_wrapper->commerce_unit_price->amount->set($new_price);
$line_wrapper->save();
}
}

Using token as data selector

I created the following token; however, when I try to use site:coupons as a data selector in a loop action
It does not appear in data selection browser. Note that it does appear as replacement pattern when i use for example "Show a message on the site" action.
I spent lot of time searching in the internet and rules' token' issue queue, i tried to read the source codes of core token , token and rules as well. I also found some information too like data selector are no tokens! or rules only works with entities!
So far i couldn't get this to work no matter hard i tried. My data is not entity. Is there anyway to integrate it with rules?
I couldn't find any official documentation on this so i created an issue with hope that some of the rule's experts can help me out.
Note : if i replace site with coupon-link in the following code, it won't even appear as replacement pattern in rules. but it works fine as token anywhere else but in rules
Thanks in advance
<?php
/**
* Implements hook_token_info().
*/
function coupon_link_token_info() {
$types['coupon-link'] = array(
'name' => t("Coupon link coupon info"),
'description' => t("Info about linked coupon via url."),
);
// Andy Pangus specific tokens.
$tokens['site']['coupon-code'] = array(
'name' => t("Coupon Link Coupon Code"),
'description' => t("The code of the coupon entered via url."),
);
$tokens['site']['coupon'] = array(
'name' => t("Coupon Link Coupon"),
'description' => t("The coupon entered via url."),
'type' => 'commerce_coupon'
);
$tokens['site']['coupons'] = array(
'name' => t("Coupon Link List Coupons"),
'description' => t("The coupons entered via url."),
'type' => 'array'
);
return array(
'types' => $types,
'tokens' => $tokens,
);
}
/**
* Implements hook_tokens().
*
* #ingroup token_example
*/
function coupon_link_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
// Text format tokens.
if ($type == 'site' && __coupon_link_get_coupon_code()) {
//$format = $data['format'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'coupon-code':
// Since {filter_format}.format is an integer and not user-entered
// text, it does not need to ever be sanitized.
$replacements[$original] = $sanitize ? filter_xss(__coupon_link_get_coupon_code()) : __coupon_link_get_coupon_code();
break;
case 'coupon':
// Since the format name is user-entered text, santize when requested.
$replacements[$original] = __coupon_link_get_coupon(__coupon_link_get_coupon_code());
break;
case 'coupons':
// Since the format name is user-entered text, santize when requested.
$replacements[$original] = array(__coupon_link_get_coupon(__coupon_link_get_coupon_code()));
break;
}
}
}
return $replacements;
}
?>
A few things.
Tokens are formatted as [type:token] as explained on the hook_token_info api page. For your example, it would be [coupon-link:coupon]. I'm not sure why you're appending your tokens to the site array, as your custom coupon token probably has nothing to do with sitewide tokens like *site_url* or *site_name*.
Because the types are machine names, you should change it to coupon_link as machine names with dashes are not Drupal standard.
If you truly get lost, I suggest also looking at the token example from the examples module.

Resources