Categories
WooCommerce WordPress

WooCommerce – How to add custom tracking code for the thank you page

<?php
/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'cs_custom_tracking' );

function cs_custom_tracking( $order_id ) {

	// Lets grab the order
	$order = wc_get_order( $order_id );

	/**
	 * Put your tracking code here
	 * You can get the order total etc e.g. $order->get_total();
	 */
	 
	// This is the order total
	$order->get_total();
 
	// This is how to grab line items from the order 
	$line_items = $order->get_items();

	// This loops over line items
	foreach ( $line_items as $item ) {
  		// This will be a product
  		$product = $order->get_product_from_item( $item );
  
  		// This is the products SKU
		$sku = $product->get_sku();
		
		// This is the qty purchased
		$qty = $item['qty'];
		
		// Line item total cost including taxes and rounded
		$total = $order->get_line_total( $item, true, true );
		
		// Line item subtotal (before discounts)
		$subtotal = $order->get_line_subtotal( $item, true, true );
	}
}
Categories
Imagick PHP WordPress

Imagick – Generate Preview Image of PDF

<?php

$file          = get_attached_file( $attachment_id );
$output_format = "jpeg";
$antialiasing  = "4";
$preview_page  = "1";
$resolution    = "300";
$output_file   = 'srv/www/chetan/doc/' . $attachment_id . '_imagick_preview.jpg';
$exec_command  = "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=" . $output_format . " ";
$exec_command  .= "-dTextAlphaBits=" . $antialiasing . " -dGraphicsAlphaBits=" . $antialiasing . " ";
$exec_command  .= "-dFirstPage=" . $preview_page . " -dLastPage=" . $preview_page . " ";
$exec_command  .= "-r" . $resolution . " ";
$exec_command  .= "-sOutputFile=" . $output_file . " '" . $file . "'";

exec( $exec_command, $command_output, $return_val );