WooCommerce Conversion Tracking Script for two Pixel - woocommerce

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!

Related

How to get woocommerce order information inside of wp_head

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' );

How to Add custom price on single product without affecting to related product?

I have code to changes the custom message on all product with 2 different messages. Please take a look.
add_filter('woocommerce_empty_price_html', 'show_alert_info_if_no_price');
function show_alert_info_if_no_price ($product){
if (is_product()) {
global $product;
$price = $product->get_price();
if ($price == '') {
ob_start();
// return for the product page
return '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
} else {
// otherwise return short text as kontak kami
return 'Contact us';
}
}
}
Now I have a problem on related product. I need the related product price will appear like :
//short text as kontak kami
Now I am stuck how to add another code when using hooked.
$woocommerce_loop['name'] != 'related').
any help will appreciated!
There is an mistake in your code, because this hook is only executed for empty prices. So going to check in the hook again for an empty price and if it isn't, running an else condition is useless.
To display a different text on the single product page for related products you can use global $woocommerce_loop
So you get:
function filter_woocommerce_empty_price_html( $html, $product ) {
global $woocommerce_loop;
// True on a single product page
if ( is_product() ) {
$html = '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
// Related
if ( $woocommerce_loop['name'] == 'related' ) {
$html = __( 'Some other text', 'woocommerce' );
}
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );

WooCommerce Display Product Variations SKU

I am using WooCommerce and I'm trying to display product variation SKUs on the product page below the product title. I managed to find this code, which works but displays the SKU in the wrong place:
// Display product variations SKU and GTIN info
add_filter( 'woocommerce_available_variation', 'display_variation_sku_and_gtin', 20, 3 );
function display_variation_sku_and_gtin( $variation_data, $product, $variation ) {
$html = ''; // Initializing
// Inserting SKU
if( ! empty( $variation_data['sku'] ) ){
$html .= '</div><div class="woocommerce-variation-sku">' . __('SKU:') . ' ' . $variation_data['sku'];
}
// Using the variation description to add dynamically the SKU and the GTIN
$variation_data['variation_description'] .= $html;
return $variation_data;
}
Can anyone help with changing the order of this code so the SKU shows below the product title, or help me with some new code?
Many thanks!
WooCommerce does not provide a specific action hook that will let you add anything right after the product title, that also makes use of the variation data like the hook in your current code. You can work around this by adding an element after the product title via JavaScript/jQuery.
You want this element to dynamically change based on the selected variation. Since you do not have access to the variation data directly in the action hook you will have to check the hidden input variation_id that WooCommerce uses to store the selected variation id. Then use AJAX every time that input changes to retrieve the variation SKU belonging to this variation id.
add_action( 'woocommerce_before_single_product', 'show_variation_sku_underneath_product_title' );
function show_variation_sku_underneath_product_title() {
global $product;
if ( $product->is_type('variable') ) {
?>
<script>
jQuery(document).ready(function($) {
$('input.variation_id').change( function(){
if( '' != $('input.variation_id').val() ) {
jQuery.ajax( {
url: '<?php echo admin_url( 'admin-ajax.php'); ?>',
type: 'post',
data: {
action: 'get_variation_sku',
variation_id: $('input.variation_id').val()
},
success: function(data) {
$('h1.product_title').siblings('.variation-sku').remove();
if(data.length > 0) {
$('h1.product_title').after('<p class="variation-sku">' + data + '</p>');
}
}
});
}
});
});
</script>
<?php
}
}
add_action('wp_ajax_get_variation_sku' , 'get_variation_sku');
add_action('wp_ajax_nopriv_get_variation_sku','get_variation_sku');
function get_variation_sku() {
$variation_id = intval( $_POST['variation_id'] );
$sku = '';
if ( $product = wc_get_product( $variation_id ) ) $sku = $product->get_sku();
echo $sku;
wp_die(); // this is required to terminate immediately and return a proper response
}
These code snippets should be added to the functions.php of your child theme or via a plugin like Code Snippets.

Woocommerce add img tag on order admin details page

I have a wordpress website where customers make an image with text and icons, once processed thru woocommerce and payed for that image name 12345.png is saved as Customer_product_image
function add_order_item_meta($item_id, $values) {
$key = 'customer_product_image'; // Define your key here
$value = $values['user_img']; // Get your value here
woocommerce_add_order_item_meta($item_id, $key, $value);
}
And i works great, but now i'm banning my head against the wall! When the purchased image is displayed on the Order admin detail page, it shows up as CUSTOMER_PRODUCT_IMAGE: 1234.png how on earth would i go about wrapping that within an image tag so the image is displayed there?
I've searched high and low on google but haven't been able to find anything, its probably that i dont know what do actually search for....
This did the trick for me!
First i added this snippet for removing the custom meta item on order detail render:
add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );
function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}
second i added it back with this, and with the desired text and image tag:
add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
function order_meta_customized_display( $item_id, $item, $product ){
$all_meta_data=get_metadata( 'order_item', $item_id, "", "");
$useless = array(
"_qty","_tax_class","_variation_id","_product_id","_line_subtotal","_line_total","_line_subtotal_tax","_line_tax","_line_tax_data"
);// Add key values that you want to ignore
$customized_array= array();
foreach($all_meta_data as $data_meta_key => $value) {
if(!in_array($data_meta_key,$useless)){
$newKey = ucwords(str_replace('_'," ",$data_meta_key ));//To remove underscrore and capitalize
$customized_array[$newKey]=ucwords(str_replace('_'," ",$value[0])); // Pushing each value to the new array
}
}
if (!empty($customized_array)) {
foreach($customized_array as $data_meta_key => $value){
echo "<div class='product_container'><span>Produkt Billede: </span><img src='".wp_upload_dir()['baseurl'].'/flapper/'. $value ."' /> </div>";
}
}
}
i found the answer to this question on this page
You can use the filter woocommerce_order_item_display_meta_value to output the image. Place this code in your functions.php file, you'll need to modify the src attribute of the img tag to include the appropriate URL before the filename value. You can also modify the display label with the filter woocommerce_order_item_display_meta_key
add_filter( 'woocommerce_order_item_display_meta_value', 'modify_order_item_display_value' , 10, 3 );
function modify_order_item_display_value( $display_value, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return '<img src="' . $meta_data['value'] . '">';
}
return $display_value;
}
add_filter('woocommerce_order_item_display_meta_key', 'modify_order_item_display_key', 10, 3);
function modify_order_item_display_key( $display_key, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return 'Customer Image';
}
return $display_key;
}

Automatically add woocommerce product short description on creation

I want to add a product short description by default whenever is the new product is being created. All the products will have the same short description, so there is no point keep copying and pasting it. So it should just be there when I click on the add a new product.
I would appreciate any help.
add_filter( 'woocommerce_short_description',
'single_product_short_description', 10, 1 );
function single_product_short_description( $post_excerpt )
{
global $product;
if ( is_single( $product->id ) )
$post_excerpt = '<div class="product-message"><p>' . __( "Article only
available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
return $post_excerpt;
}
I found the above code but couldn't get it to work :(
Thank you.
Regards,
Emre.
Add this code inside your themes function.php
Change the short description content as per your need - "Here goes your short desc."
add_filter( 'wp_insert_post_data' , 'cdx_add_product_short_desc' , '99', 1 );
function cdx_add_product_short_desc( $data )
{
//only for product post type
if($data['post_type'] == 'product' ) {
//only if short description is not present
if( '' == trim($data['post_excerpt']) ):
$short_desc = 'Here goes your short desc.';
$data['post_excerpt'] = $short_desc ;
endif;
}
// Returns the modified data.
return $data;
}
This will work to automatically override anything put into a product's short description field on the front-end only. It will not add the text to the backend field itself, which is good because it keeps it globalized if you need to change it later.
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );
function filter_woocommerce_short_description( $post_excerpt ) {
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>';
return $post_excerpt;
}

Resources