How to get woocommerce order information inside of wp_head - wordpress

This is driving me nuts. One of our clients is using a company to track pixels on the site. I'm currently using a snippet I put together to add different tracking scripts to wp_head depending on what page they are on.
One of those if statements outputs a pixel if the page is the Woocommerce Order Received Page (After the customer places an order)
if (is_wc_endpoint_url('order-received')) { // Order Received Page which contains order ID and Total Price of Order
$pixel_code = '<!-- Conversion Pixel for [misc]- DO NOT MODIFY --><img src="https://data.adxcel-ec2.com/pixel/?ad_log=referer&action=misc&value=[value]&order_id=[order_id]&pixid=xxxxxxxxxxxxxxxxx" width="1" height="1" border="0"><!-- End of Conversion Pixel -->';
}
Here is my problem.
I can access the order id and order total amount on the page directly using the following code, and it will echo on page.
add_action( 'woocommerce_thankyou', 'pixels_checkout_thankyou', 10, 1 );
function pixels_checkout_thankyou( $order_id ) {
//getting order object
$order = wc_get_order($order_id);
$order_id = $order->get_id(); // Get the order ID
$order_total = $order->get_total();
if(is_wc_endpoint_url( 'order-received' )) {
echo 'Order ID is: ' . $order_id . '<br>';
echo 'order total is: ' . $order_total;
};
}
I need to be able to access that same information (Order ID and Order Total) inside of wp_head. I need to replace values in the script tag with the id and total.
So this >
https://data.adxcel-ec2.com/pixel/?ad_log=referer&action=misc&value=[value]&order_id=[order_id]&pixid=xxxxxxxxxxxxxxxxx
Would become >
https://data.adxcel-ec2.com/pixel/?ad_log=referer&action=misc&value=**ORDERTOTAL**&order_id=**ORDERID**&pixid=xxxxxxxxxxxxxxxxx
As you can imagine, the woocommerce order object doesn't work if you try to add it to wp_head with add_action('wp_head', 'function_name');
How would I be able to access the order information to be able to add it to the wp_head portion of the website?
What I tried and doesn't work because the order object is not accessible this way >
function testpixel() {
if (is_wc_endpoint_url('order-received')) { // Order Received Page
//getting order object
$order = wc_get_order($order_id);
$order_id = $order->get_id(); // Get the order ID
$order_total = $order->get_total();
$pixel_code = '<!-- Conversion Pixel for [misc]- DO NOT MODIFY --><img src="https://data.adxcel-ec2.com/pixel/?ad_log=referer&action=misc&value=' . $order_total . '&order_id=' . $order_id . '&xxxxxxxxxxxxxx" width="1" height="1" border="0"><!-- End of Conversion Pixel -->';
echo $pixel_code;
}
add_action('wp_head', 'testpixel');

You almost figured it out :)
Inside the wp_head you can use the get_query_var to get the order_id. After that, it's all easy.
function testpixel() {
// Do things only if on Order Received Page.
if ( is_wc_endpoint_url( 'order-received' ) ) {
$order_id = absint( get_query_var( 'order-received' ) );
$order = wc_get_order( $order_id );
$order_total = $order->get_total();
$pixel_code = '<!-- Conversion Pixel for [misc]- DO NOT MODIFY --><img src="https://data.adxcel-ec2.com/pixel/?ad_log=referer&action=misc&value=' . esc_attr( $order_total ) . '&order_id=' . esc_attr( $order_id ) . '&xxxxxxxxxxxxxx" width="1" height="1" border="0"><!-- End of Conversion Pixel -->';
echo $pixel_code;
}
}
add_action( 'wp_head', 'testpixel' );

Related

I need create custom order tracking functionality in woo commerce

Hello I have a woo commerce website and I am selling some books every thing is cleared but I need create custom order tracking functionality in woo commerce code to add order tracking functionality for end user is it possible if possible how can I do this please help me.
I create a custom page name as woocommerce-custom-order-tracking.php
and code is given below
<?php
// Register a custom endpoint to handle order tracking
function wc_register_order_tracking_endpoint() {
add_rewrite_endpoint( 'order-tracking', EP_PAGES );
}
add_action( 'init', 'wc_register_order_tracking_endpoint' );
// Display the order tracking form
function wc_display_order_tracking_form() {
if ( ! is_wc_endpoint_url( 'order-tracking' ) ) {
return;
}
// Get the order id from the query string
$order_id = absint( $_GET['order_id'] );
// Get the order
$order = wc_get_order( $order_id );
if ( $order ) {
// Display the order tracking information
echo '<p>Order Number: ' . $order->get_order_number() . '</p>';
echo '<p>Order Status: ' . wc_get_order_status_name( $order->get_status() ) . '</p>';
echo '<p>Tracking Number: ' . get_post_meta( $order_id, '_tracking_number', true ) . '</p>';
} else {
echo '<p>Invalid order ID.</p>';
}
}
add_action( 'woocommerce_before_single_product', 'wc_display_order_tracking_form' );

Problems with a custom pay link shortcode on WooCommerce/wordpress

Hello I am trying to create custom payment links for various payment methods. I would like the link to be generated on WooCommerce based on a cart or order total at the checkout page. Here is what I have been trying in my functions php file.
function customhaywoo(){
global $woocommerce, $total, $amount ;
$woocommerce->cart->get_cart();
$total = $woocommerce->cart->get_total();
$amount = $woocommerce->cart->total;
if ( ! $amount ) {
return;
}
extract(shortcode_atts(array(
"href" => 'https://venmo.com/hayden-55?txn=pay&amount=',
esc_attr( wp_kses_post( $amount ) ),
), $atts));
return ''.$content.'';
}
add_shortcode ('haywoo','customhaywoo');
I have tried other combinations as well however as you can probably tell I have absolutely no experience and have no idea what I am doing. I got the idea of what I should include in the code from "checkout with Venmo" by the African boss plugin. I would just use the plugin however I need this for a few other Payment methods. For many of the Payment methods you can just add the number amount at the end of the url and it will automatically have the amount inserted into the payment method. Basically I somehow need to get the cart or order total to appear at the end of a link without the currency symbol.
Edit- I tried this for displaying on the thank you page. This just shows up as nothing at all. not even the [haywoo]. Any help on this would be awesome!
function customhaywoo()
{
$order = new WC_Order( $order_id );
$total = $order->get_total();
$order_total = floatval( preg_replace( '#[^\d.]#', '', $order->get_total()) );
if (!$order_total) {
return;
}
ob_start();
$pay_link = 'https://venmo.com/hayden-595?txn=pay&amount='.$order_total;
$payment_text = __('Click here to pay '.$order_total, 'text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents; ''.$content.'';
}
add_shortcode('haywoo', 'customhaywoo');
If you want the shortcode to output the payment link with the total amount of cart then try out below edited code.
function customhaywoo()
{
global $woocommerce, $total, $amount;
$woocommerce->cart->get_cart();
$total_amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
if (!$total_amount) {
return;
}
ob_start();
$pay_link = 'https://venmo.com/hayden-55?txn=pay&amount='.$total_amount;
$payment_text = __('Click here to pay '.$total_amount, 'text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
add_shortcode('haywoo', 'customhaywoo');
I Hope it helps.

Issue adding date created to order number when creating order manually in WooCommerce

I use the following code to modify the order number in WooCommerce.
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);
function change_woocommerce_order_number( $order_id, $order ) {
$prefix = '160-';
// you will need to get your city as a variable to pass in to priffix
$order = wc_get_order( $order_id );
$order_date = $order->get_date_created();
$date_created = $order_date->date( 'Ymd' );
// You can use either one of $order->id (or) $order_id
// Both will wor
return $prefix . $order->id . '-'.$date_created;
}
This code works during the checkout process but I get an error like this when i manually create an order in WooCommerce backend.
How can I prevent this?
Your code contains some mistakes
The use $order = wc_get_order( $order_id );is not necessary, as $order already passed to the function
$order->id has been replaced since WooCommerce 3 (2017) with $order->get_id()
However, using/replacing $order->get_id() is not needed in this answer, as this is also passed to the function
The error will not occur when you place an order, but will occur when you want to create a new order in the backend (which is the case for you). Simply because the order has yet to be created, and that value does not yet exist
So you get
function filter_woocommerce_order_number( $order_number, $order ) {
// Prefix
$prefix = '160-';
// Is null
if ( is_null( $order->get_date_created() ) ) {
$order->set_date_created( new WC_DateTime() );
}
// Get date created
$date_created = $order->get_date_created()->date( 'Ymd' );
return $prefix . $order_number . '-' . $date_created;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );
Try this code.
Replace this lines.
$order_date = $order->get_date_created();
$date_created = $order_date->date( 'Ymd' );
To
$date_created = $order->get_date_created()->date('Ymd');

WooCommerce Conversion Tracking Script for two Pixel

I want to promote my products by some affiliate networks.
Do only thing you have to do, is to go into the function.php file and add this script with the pixel. With this script the tracking of the amount value works fine. This script works only for one network and if you are the only vendor.
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
$order = new WC_Order( $order_id );
$total = $order->get_subtotal();
$id = str_replace('#', '', $order->get_order_number());
echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
}
My problem: I have multiple Vendors who are using my plattform for Product delivery / purchase processing.
I need to know how i can modify the function file in order to add a working second script for a 2nd pixel if a specific product has been selected and bought.
My it skills in woocommerce are limited, so i would like to understand how to modify the script without harming the (general) tracking.
If somebody buys the "normal" Products than the 1st pixel above should fire.
If somebody buys a specific product with the Product ID 2004 - than a 2nd different pixels needs to fire and ignore the first pixel.
Do i need to add a second function or modify the first one?
Thank you
Additional questions (Update 16.05.2017)
In the future I will probably have to install a third pixel. How would the structure be?
add_action('woocommerce_thankyou', 'wh_custom_tracking');
function wh_custom_tracking($order_id)
{
$product_ids = [2004, 2000]; //<-- list of product_id(s) for which 2nd pixels should fire
$checkSecond = FALSE;
$product_ids = [2003, 2001]; //<-- list of product_id(s) for which 3nd pixels should fire
$checkThird = FALSE;
$order = wc_get_order($order_id);
$total = $order->get_subtotal();
$id = str_replace('#', '', $order->get_order_number());
$items = $order->get_items();
foreach ($items as $item)
{
$item_id = $item['product_id']; // <= Here is your product ID
if (in_array($item_id, $product_ids))
{
$checkSecond = TRUE;
break;
}
{
$checkThird = TRUE;
break;
}
}
if ($checkSecond)
{
//add your 2nd pixel here 2nd pixel
}
else
if ($checkThird)
{
//add your 3nd pixel here 2nd pixel
}
else
{
echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
}
}
Is the same structure also valid for variation IDs?
In the affiliate software within the offer a "Target pixel" and the "final pixel" can be used.
Some products are "test products" and have a value of € 0.00. If the main pixel fires, then the affiliate receives no compensation, even if the customer afterwards purchases the product.
In this case, a kind of target pixel would have to be installed for the variation ID of a particular product. If the customer decides after the test month for the purchase, then the "right pixel" should fire.
You can get the list of product ID(s) by Order ID, and then you can apply your conditioning and trigger different pixel.
Here is a sample code which should solve your query:
add_action('woocommerce_thankyou', 'wh_custom_tracking');
function wh_custom_tracking($order_id)
{
$product_ids = [2004, 2000]; //<-- list of product_id(s) for which 2nd pixels should fire
$checkSecond = FALSE;
$order = wc_get_order($order_id);
$total = $order->get_subtotal();
$id = str_replace('#', '', $order->get_order_number());
$items = $order->get_items();
foreach ($items as $item)
{
$item_id = $item['product_id']; // <= Here is your product ID
if (in_array($item_id, $product_ids))
{
$checkSecond = TRUE;
break;
}
}
if ($checkSecond)
{
//add your 2nd pixel here 2nd pixel
}
else
{
echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
}
}
Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Related Question: How to insert Google Merchant Review JS code in WooCommerce Order Complete page
Hope this helps!

How to display customer name in woocommerce email template?

I'm new to Woocommerce, and I'm trying to show the name of the customer in the beginning of the email.
So the email will look something like this:
[HEADER]You order has been placed [/HEADER]
[H2]Hi {CUSTOMER_NAME}[/H2]
[P]Some text[/P]
[THE REST OF THE MAIL]
I've tried adding something like this to functions.php in my theme.
//add the first name of the person to the email.
add_filter('woocommerce_email_order_details','name_to_processing_customer_email', 10, 3);
function name_to_processing_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if( 'customer_processing_order' == $email->id ){
// Set here as you want your custom content (for customers and email notification related to processing orders only)
echo '<h2>Hej '.$order->billing_first_name .'</h2>';
}
}
But this doesn't work.
Can someone help?
Try this One ::
add_filter('woocommerce_email_order_details','name_to_processing_customer_email', 10, 3);
function name_to_processing_customer_email( $order_id, $sent_to_admin, $plain_text, $email ) {
$order = new WC_Order( $order_id );
echo '<h2>Hej '.$order->billing_first_name .' '.$order->billing_last_name.'</h2>';
}
add_action( 'woocommerce_email_after_order_table', 'name_to_processing_customer_email', 10, 2 );
function name_to_processing_customer_email( $order, $is_admin_email ) {
echo '<p><h4>Shipping:</h4> ' . $order->get_billing_first_name() . '</p>';
}
Check this WC3 snippet

Resources