WooCommerce v2 - echo order quantity - php

I am manipulating a table that shows orders.
I have managed to show some data from this using the below example (getting their phone number)
$order = new WC_Order( $item['order_id'] );
$phone = $order->billing_phone;
I have tried $order->quantity but that returns nothing - so not sure what variable I need to use.
Can someone assist?

Related

Woocommerce & Opayo: Add a custom field to the data sent to the API

Very specific issue but, I've been dragged onto an issue with our companies Payment Gateway on our Wordpress / Woocommerce website where we are using the Opayo Plugin (For Opayo Direct).
The issue is:
When originally setup, there was no template / option selected for the Reference field on the data / object sent to the API
The guys originally doing the website then tried contacting the original developer of this now unsupported plugin to which they was sent some code to put into another plugin named PHP Injection the code was similar to below:
add_filter( 'opayo_direct_custom_field_vendordata', 'my_opayo_direct_custom_field_vendordata', 10, 2 );
function my_opayo_direct_custom_field_vendordata ( $vendordata, $order ) {
// Get Order ID
$order_id = $order->get_order_number();
$reference = "test_" . $order_id;
// Get the reference field - set '_reference_field' to the meta_key from your order
if( isset( get_post_meta( $order_id, '_reference_field', TRUE ) ) ) {
$vendordata = get_post_meta( $order_id , '_reference_field', TRUE );
//$vendordata['_reference_field'] = $reference; ### Commented as I'm unsure if this is correct
}
return $vendordata;
}
After doing numerous testing and small changes, still nothing seems to be showing up in the Reference field in Opayo itself?
Please tell me someone has encountered this situation before or knows what I might be missing, it's been a while since I've touched PHP
First you mot use $order->get_order_number()when trying to get order meta data but use $order->get_id() with get_post_meta() function instead.
Now you can also use the WC_Data method get_meta() to be used on $order object variable.
What you need to find out is the key slug that you need to use to incorporate that custom field value to the vendor data via opayo_direct_custom_field_vendordata filter hook.
Try the following (where I use 'reference' as key slug, to be replaced with the right slug):
add_filter( 'opayo_direct_custom_field_vendordata', 'my_opayo_direct_custom_field_vendordata', 10, 2 );
function my_opayo_direct_custom_field_vendordata ( $vendor_data, $order ) {
$reference = $order->get_meta('_reference_field');
if ( ! empty($reference) ) {
$vendor_data['reference'] = $reference;
}
return $vendor_data;
}
or using get_post_meta() function:
add_filter( 'opayo_direct_custom_field_vendordata', 'my_opayo_direct_custom_field_vendordata', 10, 2 );
function my_opayo_direct_custom_field_vendordata ( $vendor_data, $order ) {
$reference = get_post_meta($order->get_id(), '_reference_field', true);
if ( ! empty($reference) ) {
$vendor_data['reference'] = $reference;
}
return $vendor_data;
}
It could better work…
Alright, so, I contacted Opayo myself to see what the Mapping was for the Reference column on the Opayo website and what data that is sent to the API is mapped to that Reference column in the table.
Apparently the value sent to the API that corresponds to this is the VendorData (Not required, 200 Char limit, free-text)
So, to fix this, I had to:
Open the Plugin Editor on Wordpress
Select the woocommerce-gateway-sagepay-form Plugin
Navigate and select the woocommerce-gateway-sagepay-form/classes/direct/sagepay-direct-request-class.php file
I then scrolled to Line 335 inside of the $end array, I then added the field of VendorData as seen by the code below:
$end = array(
"CustomerEMail" => $order->get_billing_email(),
"ClientIPAddress" => $this->get_ipaddress(),
"AccountType" => $this->accounttype,
"ReferrerID" => $this->referrerid,
"Website" => site_url(),
"VendorData" => '#######',
"Crypt" => MD5( $this->open_salt . $order->get_order_key() . $this->close_salt ),
);
And then set the ####### to the Value I needed to send to Opayo to show in the Reference column in the payments table.
Then just to be sure it wasn't going to get pruned (Again, not touched PHP for a while, I went to (NOW) Line 378 and commented it out, see below:
// Customiseable fields
$end['TransType'] = apply_filters( 'opayo_direct_custom_field_transtype', '01', $order );
//$end['VendorData'] = apply_filters( 'opayo_direct_custom_field_vendordata', '', $order );
Saving this, then when an Order / Purchase was made, the Reference field on Opayo's website was populated with what I set the VendorData to.
Quick Note: The VendorData field is max of 200 characters and only Aa or 0-9, I had a couple of failed attempts when I was trying to have _ until I searched the error I received on this page:
https://www.opayo.co.uk/support/error-codes?keyword=3189
I hope that my issue and resolution helps someone in the future and sorry for anyone's time I wasted!

Get Woocommerce last order id from database using php

In Woocommerce, I am trying to get the last order id using the following code:
<?php $order = new WC_Order($post->ID);echo $order->id;//to escape # from order id $order_id = trim(str_replace('#', '', $order->get_order_number())); ?>
But it doesn't work as I am getting a zero value.
The purpose is to add 1 to this last order ID, to get the new usable order ID.
Any help is appreciated.
It seems that you would like to get the next usable POST ID (Order Id), to save it, for example, as new Order in database with some related data.
This is absolutely not the way to do it and you need to think it different... Now you can use one of the 3 following ways:
Using the WordPress dedicated function wp_insert_post() that returns the post ID (Order ID).
Using the Woocommerce dedicated function wc_create_order() that returns the WC_Order Object.
Then from the the order object, you can get the order ID using $order->get_id().
Using the Woocommerce empty WC_Order object instance and the save() method:
// Get an empty instance of the `WC_Order` Object
$order = new WC_Order();
// Save the order to the database
$order->save();
// Get the Order ID
$order_id = $order->get_id();
Addition - Get the last Order ID in Woocommerce:
To get and display the last order ID in woocommerce use a WC_Order_Query in this two simple line:
<?php
$last_order_id = wc_get_orders(array('limit' => 1, 'return' => 'ids')); // Get last Order ID (array)
echo (string) reset($last_order_id); // Displaying last order ID
?>
Tested and works

How to know when an order gets open(date and time) in woocommerce

PHP:
order status is wc_open and I am trying to find out the way to know when an order gets open(date and time).
$order = wc_get_order( $nextorder->ID );
$order->get_date_modified();
I am using get_date_modified() but this method not giving correct answer because modified date changed if any change happens.
Please help me.
You need to do it like this:
$order = wc_get_order($nextorder->ID);
$order_date = $order->order_date;

woocommerce admin create order get customer id on create

It's been a couple of days now, and I can't seem to find a hook for adding/updating user meta
add_user_meta( 'user_id', 'custom_key', 'custom_value');
when creating an order in woocommerce admin (woocommerce->orders->add order), backend.
Using
add_action('woocommerce_process_shop_order_meta', 'admin_process_shop_order', 10, 1);
This works fine for doing things while the order is processed. However, I need to get the customer ID, which from what I can tell doesn't exist until the order is actually created (makes sense).
So my question is, what hook (or other solution) can I use to get the customer ID once the order is created and searchable with
get_post_meta($order_id, '_customer_user', true);
Thank you #Gugan for your suggestions! It looks like that with your help I was finally able to get this mess sorted :)
Since I only wanted this to fire one time, i.e. when the order is created (and not again when updated), I hade to combine two actions.
First 'woocommerce_process_shop_order_meta'. Here I can check if the post meta exists (if it does, the order has already been created and should be left alone)
function check_order($post_id){
$new_order = get_post_meta($post_id, '_customer_user', true);
if(!$new_order){
add_action('woocommerce_order_status_[MY_CUSTOM_ORDER_STATUS]-processing', 'total_count');
}
}add_action('woocommerce_process_shop_order_meta', 'check_order', 10, 1);
If this is a new order move on to 'woocommerce_order_status_[MY_CUSTOM_ORDER_STATUS]-processing' (with my function 'total_count')
function total_count($post_id){
$order = wc_get_order($post_id);
$customer_id = $order->get_user_id();
$user_role = get_user_meta($customer_id, 'wp_capabilities', true);
$custom = serialize(array('[MY_CUSTOM_USER_ROLE]' => true));
$today = date('Y-m-d');
if($user_role = $custom){
$current_total = get_user_meta($customer_id, 'total', true);
$increment_total = $current_total+1;
update_user_meta( $customer_id, 'total', $increment_total);
update_user_meta( $customer_id, 'last', $today);
}
}
Now I only get an increment on my custom user metas "total" and "last" if this is a new order and if the customer is of my custom user role. Another plus to this is that it will only work for one order status (i.e. in my case [MY_CUSTOM_ORDER_STATUS]-processing).
Just jotting down my solution here for anyone else looking to handle similar custom order creation work.

Using wc_get_product() with a PHP variable for product ID

I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.
Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..
I can't get the wc_get_product(); to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
Update
I get the following error using wc_get_product( $courseID ); or get_product( $courseID );:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
Update related to your recent comment. The 2 ways to explore:
1) Instead of you should try to use to get the product object (avoiding the error):
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) Alternatively if this doesn't work, you should try to use get_post_meta() function to get the product price (or any product meta data) this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This time you should get displayed the price with one or the other solutions.
Update: May be Also you need to convert $courseID to an integer variable.
Because you need to use your variable $courseID inside wc_get_product() (without the 2 ') function this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This should work now.
You can try this out :
$courseID = the_field('course_id');
$product = get_product( $courseID );
I figured out the answer after running through the possible solution routes that #LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.
I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field() in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field() which is to use it to store a value, echo a value and interact with a value.
Once I switched to setting my $courseID to this..
$courseID = get_field('course_id');
Everything worked. My code worked, and all #LoicTheAztec's code approaches also worked.

Categories