Hướng dẫn thêm tracking order vào website wordpress

Lượt xem: 4456

Hướng dẫn thêm tracking order vào website wordpress

Để thêm tracking order vào website wordpress. Trong file  functions.php các bạn thêm đoạn code như sau:


add_action( 'wp_head', 'dandev_add_tracking_order' );
function dandev_add_tracking_order(){
    if(is_page('thanh-toan')):
        $order_id = wc_get_order_id_by_order_key( $_GET['key'] );
        $order  = wc_get_order( absint( wp_unslash($order_id) ) ); 
        if($order ){
            $items_list =	$order->get_items();
			?>
			<script>
				window.dataLayer = window.dataLayer || [];
				dataLayer.push({         
				'transactionId': '<?php echo $order->get_order_number();?>',
				'transactionTotal': <?php echo $order->get_total();?>,
				'transactionShipping': 0,
                'event': 'purchase_sucess',
				'transactionProducts': [
			<?php
			if($items_list):
			foreach ($items_list as $item_id => $item ):
                $product = wc_get_product( $item->get_product_id() );
				$cat_name ='';
				$terms = get_the_terms ( $item->get_product_id(), 'product_cat' );
					if(	$terms){
						foreach ( $terms as $term ) {
						$cat_name = $term->name;
						break;
					}
				}
				?>
				{
					'sku': '<?php echo ($product->get_sku())?$product->get_sku():$item->get_id(); ?>', 				
					'name': '<?php echo  $item->get_name(); ?>',
					'category': '<?php echo $cat_name ?>',
					'price': <?php echo $item->get_subtotal()/$item->get_quantity() ?>,
					'quantity': <?php echo $item->get_quantity(); ?>
				},
            <?php endforeach;
         endif;?>
				]
				});
		</script>
        <?php
        }
    endif;
}

Video hướng dẫn cụ thể