Create a function to record order value for adds tracking - woocommerce

I need to create a function on which will record the 'Total Order value' on my order confirmation page in woocommerce, the function will be be used by microsoft ads to record the value of order against what i have spent, anybody any experience of doing this?
The code provided by microsoft is:
window.uetq.push('event', '', {'revenue_value': Replace_with_Variable_Revenue_Function(), 'currency': 'Replace_with_Currency_Code'});
So i need to create a function called 'revenue_value' for example which will pass the total order value to the above code.
Anybody point me the right direction.

Related

GA4 with GTM - sending the items array as an event parameter without using datalayer?

Using GA4 with GTM. I'm questioning how to send an array for an event. For example, the add_to_cart event. In my situation I am triggering the tag on my purchase links. On those links I added data parameters for the id, name, and value such as :
Buy Now
There are multiple and the id, name, and value are the only things that change for each link.
Google requires an items array to be sent with the add_to_cart event. Can I enter the items array as shown in this picture using dot notation? I can't supply this information in the datalayer which is why I am grabbing the values that can be different from the link itself (data parameters)... the rest are static and won't change. I can't find any way to create an array variable in GTM so the dot notation is the only thing I could think of.
Is there another way to do this I am missing or not thinking of?
Unfortunately you can't.
Your solution sends every value from the items object as an individual event parameter.
GA4 requires you to send an array of objects, with one object for every sold item.
The good news is, you can use GTM to create the items array in the correct format using some JavaScript.
#Ramon Put me in the right direction. Set this up as a custom js variable. Since I trigger the tag on link click the {{Click Element}} lets me get those data-parameter values from it to create the array values that are dynamic. I suppose I could have also used the gtm variables I already created for those here too. Anyways, I use this variable as the items event parameter value which returns the array how I wish. Seems to be working fine.
function(){
var e = {{Click Element}};
var items = [{
item_id: e.dataset.id,
name : e.dataset.name,
affiliation : 'some name',
currency : 'USD',
item_brand : 'some name',
item_category : 'Software',
price : e.dataset.value,
quantity : 1
}];
return items;
}

How to check if an order number exists in woocommerce?

In my WooCommerce store, I have an input field where I want the user to enter the order number. When validating this field, I need to check if that order number actually exists in WooCommerce.
From this question, it seems like the WooCommerce order_number might not always be the same as the order_id (which is always the same as the post_id).
So I can't use wc_get_order($the_order) function since the documentation states that it wants the post_id in the $the_order parameter.
I can't find a specific function to get the order by the order number (neither in the documentation nor the code in github).
I'm fairly new to WooCommerce, so maybe I am missing something. Is there a way to do this at all? Or is the wc_get_order mis-documented?
I've done a lot of googling and searching here on stack overflow, but I really can't find the answer to this! Any help is appreciated.
PS: I suppose I could get all orders and loop through them one by one checking if the number matches, but I was hoping to find a simpler solution to this :D
You may try this if statement to see if given ID (number) is an Order Number or not:
function is_it_a_shop_order($givenNumber)
{
if(get_post_type($givenNumber) == "shop_order")
{
echo "yes this is a valid order number";
}
else
{
echo "no go away";
}
}
Wordpress stores all posts (pages, orders, products etc.) in wp_posts table and orders are stored with post type named "shop_order".
I tested the code. I hope this will help you. Have a good day.
The reason you're struggling to find anything about this in the documentation is because WooCommerce has no native concept of an order number. The ID is the order number.
What store owners tend to find however is that order IDs don't increase sequentially. There can be a large jump in ID between one order and the next because of the way WordPress handles posts. Often users will install a plugin to address that. It's those plugins that introduce the differentiation between order IDs and what they refer to as the order number.
Here's an example of one such plugin: https://en-gb.wordpress.org/plugins/woocommerce-sequential-order-numbers/
Included in the documentation is an example of how to find an order by its number. If you have one of these plugins installed, you'll simply need to lookup how the plugin in question resolves numbers to IDs.
Send the order number through the wc_get_order call. If the returned array is empty, the order number does not exist.
$bOrderExists = DoesOrderExist($OrderNumber);
function DoesOrderExist($OrderNumber)
{
$orderq = wc_get_order($OrderNumber);
if(empty($orderq))
return 0;
return 1;
}

Odoo: reference another record by its name in qweb

This is probably really simple, but I'm really tired. Anyway, I am writing custom invoice reports and I need to reference fields that are on the sale order that generated the invoice. This would be easy if they had a relational field, but they don't and I'm not allowed to put one in. The invoice when generated from a sale order has an 'origin' field where it stores the 'name' field of the sale order. I need to reference fields from said sale order via this field. Something along the lines of sale.order.browse('name', '=', o.origin).incoterm <- if incoterm was the field I needed. So anyone can help me with the proper syntax? Or maybe I have the wrong idea?
You can try in this manner,
sale_order = self.pool.get('sale.order')
order_id = sale_order.search(cr,uid,[('name','=',o.orgin)])
if order_id:
sale_obj = sale_order.browse(cr,uid,order_id[0])
then you can use sale_obj with dot notation to access corresponding sale order field value. eg: sale_obj.incoterm

Woocommerce: how do I add metadata to a cart item?

I have a digital product which is described by a quantity and a price, but which also needs 3 more numbers to completely specify it (Unix dates, etc). Problem: how do I get these numbers into the cart?
As far as I can see, there are 2 possible ways to handle this:
A product variation
A product custom field
It looks like variations can only handle discrete values with a limited range (ie. red/yellow/green, S/M/L, etc), and can't handle general integers, like dates. That leaves custom fields. I think I'm right in saying that custom fields are ordinary meta data on the product post page, so I can handle them with get_post_meta and update_post_meta.
So, if I go for custom fields, then I would update the product page field during ordering, and then I would read back the field during checkout, when the WC_Order is created, and add the field to the new order. However, this won't work. I can't change metadata on the product page, because the product is global to all customers, and this operation would interfere with other customers. In other words, you can't store order-specific information in a product, so neither of these options would work.
So, how do I store temporary product metadata and pass it between the ordering and checkout phases (ie. between WC_Cart and WC_Order)?
One option would be to store it as user metadata (or as session data?), but there's got to be a better way - any ideas?
It turns out to be easy to do this with session data. When you're adding an item to the cart (see the source for add_to_cart_action) you create a session variable, containing all your additional meta data:
WC()->session->set(
'my_session_var_name',
array(
'members' => $members,
'start' => $start,
'expiry' => $expiry,
'etc' => $etc));
When the user checks out, the cart data disappears, and a new order is created. You can hook into woocommerce_add_order_item_meta to add the session meta data to the order meta data:
add_action(
'woocommerce_add_order_item_meta', 'hook_new_order_item_meta', 10, 3);
function hook_new_order_item_meta($item_id, $values, $cart_item_key) {
$session_var = 'my_session_var_name';
$session_data = WC()->session->get($session_var);
if(!empty($session_data))
wc_add_order_item_meta($item_id, $session_var, $session_data);
else
error_log("no session data", 0);
}
That's it. You do have to figure out how to get the order metadata out and do something useful with it, though. You may also want to clear the session data, from hooks into woocommerce_before_cart_item_quantity_zero, and woocommerce_cart_emptied. There's gist here which has some example code for this.

Google Analytics - How to filter by page, group by query string value?

I have a site with a store search that posts in the following format.
www.site.com/store-locator?city=&province=&zip[postal_code]=68123
I am trying to configure GA to give me feedback on people visiting this page and a count of specific zips searched.
example report data
/store-locator?city=&province=&zip[postal_code]=68123 1000 visits
/store-locator?city=&province=&zip[postal_code]=68456 768 visits
/store-locator?city=&province=&zip[postal_code]=68789 221 visits
note: the 'city' and 'province' values may also be populated (and I will want to mod GA to give similar data on these too).
Can anyone give feedback on how to configure GA to give me data similar to this?
Thanks!
As far as I know, the only way to look at this type of segment historically is using individual segments, which doesn't work well for an arbitrary number of zip codes. However, you can collect this data more effectively as described for new traffic. This comes up often with information like categories, tags, dates, query string variables, etc.
You can create Segments for each zip. This will work for historical analysis, but is impractical beyond a few. https://support.google.com/analytics/answer/3124493?hl=en&ref_topic=3123779
You can also use Content Grouping to create groups. This will not work historically. https://support.google.com/analytics/answer/2853423
The way I've handled this is using Custom Dimensions, which replaced Custom Variables when Universal came out. This also only works for future data.
To use Custom Dimensions, you would pass the zip code to google analytics explicitly when calling the analytics javascript code.
You can pull querystrings with javascript, or echo the parameter using something like PHP as follows:
<?php
if (array_key_exists("zip",$_GET)) { $theZip = $_GET["zip"]; }
else { $theZip = "nozip"; }
?>
And, sending the custom dimension --
ga('create', 'UA-XXXXX');
ga('set', {'dimension1': '<?php echo $theZip; ?>'})
ga('send', 'pageview');
You also need to setup the custom dimension in the Analytics Profile. Docs on custom dimensions https://developers.google.com/analytics/devguides/platform/customdimsmets
/store-locator?city=&province=&zip[postal_code]=68123 1000 visits
Step 1: In GTM, create a new macro. I called mine {{province}}
Macto Type = URL
Component Type = Query
Query Key = province
This will populate the Macro with the value of province from the query string.
Step 2: In your Google Analytics property, define a custom dimension called "province". This will assign an index key to the dimension.
Step 3: In your GTM tag for Google Analytics, you will find Custom Dimensions under more settings. Add a new dimension, apply the index number from #2 and for the dimension select the macro you created from #1
Publish and you are all set.
Now when you look in Google Analytics, you can add a secondary dimension and choose your newly created custom dimension.

Resources