I would like to change the order received (thanks page) url for a new one, which includes the ordered product's ID.
Here is the code in the class-wc-order.php
/**
* Generates a URL for the thanks page (order received).
*
* #return string
*/
public function get_checkout_order_received_url() {
$order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_page_permalink( 'checkout' ));
if ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) || is_ssl() ) {
$order_received_url = str_replace( 'http:', 'https:', $order_received_url );
}
$order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
return apply_filters( 'woocommerce_get_checkout_order_received_url', $order_received_url, $product, $this, $product->id );
}
Any idea?
Thanks!
This answer on wordpress.stackexchange.com would solve the issue
As a brief, just define the following hook:
add_filter('woocommerce_get_checkout_order_received_url','override_return_url',10,2);
function override_return_url($return_url,$order){
//create empty array to store url parameters in
$sku_list = array();
// retrive products in order
foreach($order->get_items() as $key => $item)
{
$product = wc_get_product($item['product_id']);
//get sku of each product and insert it in array
$sku_list['product_'.$item['product_id'] . 'sku'] = $product->get_sku();
}
//build query strings out of the SKU array
$url_extension = http_build_query($sku_list);
//append our strings to original url
$modified_url = $return_url.'&'.$url_extension;
return $modified_url;
}
Then, you can set the analytics goal to point to the destination below as a regular expression
\/checkout\/order-received\/\d+\/\?key=\w+&product_ID_HERE_sku=SKU_HERE
We can make it simpler by including only product ids:
add_filter('woocommerce_get_checkout_order_received_url','override_return_url',0,2);
function override_return_url($return_url,$order){
$ids = array();
foreach($order->get_items() as $key => $item)
{
$ids[] = $item['product_id'];
}
return add_query_arg('product_ids', join(',', $ids) , $return_url);
}
In this case, the regex would be:
\/checkout\/order-received\/\d+\/\?key=\w+&product_ids=ID1,ID2,ID3
Or for a single product id:
\/checkout\/order-received\/\d+\/\?key=\w+&product_ids=[\w,]*ID[\w,]*
Related
I want to trigger action save_post_shop_order by url, because I have custom function that will create product meta there on checking.
I tried something like:
add_action('wp_head', 'update_orders_by_url');
function update_orders_by_url() {
if( isset( $_GET['update_woo_orders'] ) ) {
$query = new WC_Order_Query( array(
'limit' => -1
));
$orders = $query->get_orders();
foreach($orders as $order){
$arra2[] = $order;
// Save
$order->save();
}
}
//echo count($arra2);
}
But seems not working in my case.
How I can trigger update all orders and run action save_post_shop_order for all orders?
Thanks
You can do it this way, put your code in functions.php or a plugin
add_filter( 'handle_bulk_actions-edit-shop_order', 'save_post_shop_order', 10, 3 );
/**
* #param $redirect_to
* #param $action
* #param $post_ids
* #return mixed|string
*/
function save_post_shop_order( $redirect_to, $action, $post_ids ) {
// do your job
return $redirect_to = add_query_arg( array(
'bulk_action_name' => $action,
'processed_count' => count( $processed_ids ),
'processed_ids' => implode( ',', $processed_ids ),
), $redirect_to );
}
add_action( 'admin_notices', 'save_post_shop_order_admin_notice' );
/**
* The results notice from bulk action on orders
*/
function save_post_shop_order_admin_notice() {
$count = intval( $_REQUEST['processed_count'] );
printf( '<div id="message" class="updated fade"><p>' .
_n( "",
"",
$count,
''
) . '</p></div>', $count );
}
To update orders information through the URL, you can try the following steps.
Create a file in your root directory
On the top of the file, add this piece of code.
require(dirname(__FILE__) . '/wp-load.php');
Get all orders
$orders = wc_get_orders( array('numberposts' => -1) );
// Loop through each WC_Order object
foreach( $orders as $order ){
echo $order->get_id() . '<br>'; // The order ID
echo $order->get_status() . '<br>'; // The order status
.......
// do your stuff
}
Now, you will be able to update orders information through URL.
Hit your url/pagename, for example, http://example.com/test.php
I am trying to modify the product images in the auto generated WooCommerce order e-mails. Note, I am trying to do this with hooks, rather than creating a modified e-mail template.
To start, I have a hidden input on the single product page. I have some JS that sets the ID of the image, which is based on the product's colour (a custom attribute I made with ACF).
<input class="js-col-img-id" type="hidden" name="product-col-img-id" value="">
For reference, here's what I've done to get this to work on the cart page:
// Add custom attributes to cart item data
add_action('woocommerce_add_cart_item_data', 'jwd_add_custom_attr_to_cart', 10, 3);
function jwd_add_custom_attr_to_cart($cart_item_data, $product_id, $variation_id) {
$attrs = array(...,'product-col-img-id');
foreach ($attrs as $attr) {
$san_attr = filter_input( INPUT_POST, $attr );
if ( !empty( $san_attr ) ) {
$cart_item_data[$attr] = $san_attr;
}
}
return $cart_item_data;
}
// This function sets the image ID in the cart, and it displays the image properly
add_action( 'woocommerce_before_calculate_totals', 'bwa_new_price' );
function bwa_new_price($cart) {
// ...
$cart_items = $cart->get_cart();
foreach($cart_items as $item) {
$data = $item['data'];
// product-col-img-id is sent through the hidden input when the add to cart form is submitted
$colour_img_ID = $item['product-col-img-id'] ?? false;
if ($colour_img_ID) {
$data->set_image_id($colour_img_ID);
}
}
}
// Add custom attributes to order
add_action( 'woocommerce_checkout_create_order_line_item', 'jwd_add_attr_to_order_items', 10, 4 );
function jwd_add_attr_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( !empty( $values['product-colour'] ) ) {
$item->add_meta_data( 'Colour', $values['product-colour'] );
}
// I tried replicating this for the img ID. It does display it in the var dump mentioned below, and it does display it as an actual label in the e-mail (which I'd want to get rid of). The difficulty then becomes reading the ID through code and setting the product to display that image
if ( !empty( $values['product-col-img-id'] ) ) {
$item->add_meta_data( 'col_img_id', $values['product-col-img-id'] );
}
}
That works fine, but I'm having difficulty replicating this in the order e-mail. So far I've come across the following. This shows images in the e-mail, but it's the product's featured image. Of note is the "show_image" key being set to true.
// Add prod IMG to e-mails
function jwd_add_images_woocommerce_emails( $output, $order ) {
// set a flag so we don't recursively call this filter
static $run = 0;
// if we've already run this filter, bail out
if ( $run ) {
return $output;
}
$args = array(
'show_image' => true,
//'image_size' => array( 300, 300 ),
'image_size' => 'full',
);
// increment our flag so we don't run again
$run++;
// if first run, give WooComm our updated table
return wc_get_email_order_items($order, $args);
}
add_filter( 'woocommerce_email_order_items_table', 'jwd_add_images_woocommerce_emails', 10, 2 );
To modify the image, I found this filter, but I'm just having a difficult time figuring out what I need to do to replicate what I've done on the cart page. The closest I've got is having $item_data work, and I do see the product-col-img-id key in the var dump, though it's in a protected object, which I'm not sure how to access. Even if I could access, I'm not sure how I could even set the image from here.
add_filter('woocommerce_order_item_thumbnail', 'jwd_add_colour_img', 10, 2);
function jwd_add_colour_img($image, $item) {
$item_id = $item->get_id();
//var_dump($item->get_current_data());
$product = $item->get_product();
$product_id = $item->get_product_id();
$item_data = $item->get_data();
$col_img_id = false;
$item->set_image_id(1);
foreach ($item_data as $key => $item) {
//current_data
var_dump($item);
//echo "<br><br>";
$col_img_id = $item['product-col-img-id'] ?? false;
if ($col_img_id) {
break;
}
}
//var_dump($col_img_id);
//$item_id = $item->get_product_id();
//var_dump(wc_get_order_item_meta( $item_id, 'col_img_id', true ) );
return $image;
}
To get WooCommerce custom meta data, you can use WC_Data method get_meta().
Try the following instead to display your custom image on email notifications:
// Save custom image ID as order item meta
add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_image_id_to_order_item', 10, 4 );
function save_custom_image_id_to_order_item( $item, $cart_item_key, $values, $order ) {
if ( isset($values['product-colour']) && ! empty($values['product-colour']) ) {
$item->add_meta_data('Colour', $values['product-colour'] );
}
if ( isset($values['product-col-img-id']) && ! empty($values['product-col-img-id']) ) {
$item->add_meta_data('_col_img_id', $values['product-col-img-id'] );
}
}
// (optional) Force display item image on emails
add_filter( 'woocommerce_email_order_items_args', 'show_image_on_email_notifications' );
function show_image_on_email_notifications( $args ) {
$args['show_image'] = true;
return $args;
}
// Display custom image on emails
add_filter( 'woocommerce_order_item_thumbnail', 'display_email_order_item_custom_image', 10, 2 );
function display_email_order_item_custom_image( $image, $item ) {
// Only on email notifications
if( is_wc_endpoint_url() )
return $image;
$image_id = $item->get_meta('_col_img_id');
if( $image_id ) {
$image = wp_get_attachment_image( $image_id, array( 32, 32 ), false, array() );
}
return $image;
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
I am using Woocommerce + Gravity forms with the WooCommerce Gravity Forms Product Add-Ons. My form contains a Nested Gravity Perks Form. I need to add the Woocommerce Order number to both the parent and the child gravity form. I have accomplished this with the following code:
add_action( 'woocommerce_checkout_order_processed', 'wc_add_order_id' );<br>function wc_add_order_id( $order_id ) {
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ){
$product_id = $item['product_id'];
if ( has_term( 'camp', 'product_cat', $product_id ) ) { //my woocommerce category
$meta_data = $item->get_formatted_meta_data();
$meta_data_items = $item->get_meta_data();
foreach ( $meta_data_items as $meta ) {
$entry_id = $meta->value['_gravity_form_linked_entry_id'];
$entry = GFAPI::get_entry( $entry_id );
$entry['40'] = $order_id ; //40 is the id of a single field reserved for the Order #
$result = GFAPI::update_entry( $entry );
return $result;
}
}
}
};
I then tried the following to add the order number to my child form (this works if I manually update the parent form entry. It does not work automatically from the update_entry trigger above:
add_action( 'gform_after_update_entry_82', function ( $form, $entry_id ) { //82 is the parent form id<br>
$entry = GFAPI::get_entry( $entry_id );
$entry_child_order = rgar( $entry, '2' ); //2 is the field that contains the entry id of the child
$order_id = rgar( $entry, '40' ); //this has the Woocommerce Order #
$entry2 = GFAPI::get_entry( $entry_child_order);
$entry2['147'] = $order_id ; //147 is the single field reserved for the order number
$result = GFAPI::update_entry( $entry2 );
return $result;
}, 10, 2 );
Does anyone know why "GFAPI::update_entry( $entry );" doesn't automatically trigger the second half of this code?
Ok. I figured it out. I was able to put everything into the same function and it is working.
//add order number to gravity forms
add_action( 'woocommerce_checkout_order_processed', 'wc_add_order_id' );
function wc_add_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$cart_reg = $order->get_items();
$entry_id = array();
$entry_id2 = array();
$entry_id[] = $linked_entry;
$entry_id2[] = $linked_nested_value;
foreach( $cart_reg as $key => $value) {
$linked_entry=$value->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );
$entry['40'] = $order_id; //40 is my field number that will contain orede number
$result = GFAPI::update_entry( $entry );
$linked_nested_value=$value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
if(!$linked_nested_value == ''){
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
}//child entries
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id2=$nested_value_array[$n];
$entry2 = GFAPI::get_entry( $entry_id2 );
$entry2['147'] = $order_id;//this is my field number on my child form that will contain order number
$result2 = GFAPI::update_entry( $entry2 );
}
}
}
};
With WooCommerce, I am trying to change the download URLs for order items. Originally I was using the order ID but that only allows one subscription per order. So I need the customer to be able to purchase more than one subscription.
Here is the code I currently have:
if ( !class_exists( 'pl_wcdf_extention' ) ) {
class pl_wcdf_extention {
public function __construct() {
add_filter( 'woocommerce_order_get_downloadable_items', array( $this, 'download_link' ), 10, 2 );
add_filter( 'woocommerce_customer_get_downloadable_products', array( $this, 'download_link' ), 10, 1 );
}
public function download_link( $downloads, $order = null ) {
$download_url_base = 'https://xxxx.com/';
// retrieve Order ID from Download array
$order_id = $downloads [0]['order_id'];
// retrieve Order data
$order = wc_get_order( $order_id );
// retrieve Order Items
$order_item = $order->get_items();
// retrieve first Order Item ID
$order_item_id = $order_item [0][order_item_id];
foreach ( $downloads as $download ) {
$download['download_url'] = $download_url_base . "xxxx-" . $order_item_id . ".svg";
$tag_item = array(
'--native-libs-dir' => '/home/Lib/',
'--type' => 'template0001style1',
'--data'=> $order_item_id,
'--join' => 'horizontal',
'--output-file' => '/home/public_html/xxxx-' . $order_item_id . '.svg'
);
$args = "";
foreach ($tag_item as $k=>$v) {
$args .= " $k " . " $v ";
}
shell_exec("/home/Python-2.7.14/Python-3.6.3/python /home/Lib/generate-code.py $args");
}
return $downloads;
}
}
}
if ( class_exists( 'pl_wcdf_extention' ) ) {
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $wpdb;
define( 'PL_WCDFE_DIR_PATH', plugin_dir_path( __FILE__ ) );
define( 'PL_WCDFE_PLUGIN_FILE', __FILE__ );
new pl_wcdf_extention();
}
Everything works except I am not able to get the order item ID.
Any help would be appreciated.
To retrieve order item id from the WC_Order_Item Object, please use the get_id() method.
So in your script, you should replace this section:
// retrieve first Order Item ID
$order_item_id = $order_item [0][order_item_id];
with this:
// retrieve first Order Item ID
$order_item_id = $order_item[0]->get_id();
I'm trying to extract item meta value from Woocommerce's orders by using:
$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );
However, I can't find a way to get order_item_id as the first parameter (using get_items)
global $woocommerce, $post, $wpdb;
$order = new WC_Order($post->ID);
$items = $order->get_items();
foreach ( $items as $item ) {
$item_id = $item['order_item_id']; //???
$data = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
And I mean this order item_id (1 and 2)
Order_item_id in database - Image
How can I don that?
Thanks.
2018 Update:
Clarifying the answer with 2 possible cases
Added compatibility for woocommerce 3+
So There can be 2 cases:
1) Get product meta data (not set in order item meta data):
You will need to get the product ID in the foreach loop for a WC_Order and to get some metadata for this product you wil use get_post_meta() function ( but NOT wc_get_order_item_meta() ).
So here is your code:
global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach ( $order->get_items() => $item ) {
// Compatibility for woocommerce 3+
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();
// Here you get your data
$custom_field = get_post_meta( $product_id, '_tmcartepo_data', true);
// To test data output (uncomment the line below)
// print_r($custom_field);
// If it is an array of values
if( is_array( $custom_field ) ){
echo implode( '<br>', $custom_field ); // one value displayed by line
}
// just one value (a string)
else {
echo $custom_field;
}
}
2) Get order item meta data (custom field value):
global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach ( $order->get_items() as $item_id => $item ) {
// Here you get your data
$custom_field = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
// To test data output (uncomment the line below)
// print_r($custom_field);
// If it is an array of values
if( is_array( $custom_field ) ){
echo implode( '<br>', $custom_field ); // one value displayed by line
}
// just one value (a string)
else {
echo $custom_field;
}
}
If the custom field data is an array, you can access the data in a foreach loop:
// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
echo '<p>key: '.$key.' | value: '.$value.'</p>';
}
All code is tested and works.
Reference related to data in orders:
How to get WooCommerce order details (also for woocommerce 3)
Get Order items and WC_Order_Item_Product in Woocommerce 3
When doing the foreach on $order->get_items(), their key is actually the orderline ID. So:
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...
}
Late to the party, but being working with the same point with TM Extra Product Options plugin, I think this is what answers your question:
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach( $items as $item ){
$data = unserialize($item['item_meta']['_tmcartepo_data'][0]);
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
Tested and works in my case.
Use this <pre><?php print_r($items); ?></pre> to check all the contents of the $items array/object.
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...
This solution was worth to me, change "_tmcartepo_data" for your meta_key.
A simple way to get order items from database;
/**
* #param $order_id
*
* #return array|null|object
*/
function get_order_items( $order_id ) {
global $wpdb, $table_prefix;
$items = $wpdb->get_results( "SELECT * FROM `{$table_prefix}woocommerce_order_items` WHERE `order_id` = {$order_id}" );
$item_name = array();
foreach ( $items as $item ) {
$item_name[] = $item->order_item_name;
}
return $item_name;
}