carlosmdh

dataLayer.push purchase dinamizada

Mar 3rd, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.    
  3. // Envia el evento purchase cuando se carga la página /gracias-por-su-compra/
  4.  
  5. add_action( 'wp_head', 'carlosmdh_purchase_datalayer_push' );
  6. function carlosmdh_purchase_datalayer_push() {
  7.  
  8.     $pagina    = 'gracias-por-su-compra'; // cambiar el valor de esta variable por el slug, la id, o el nombre de la página
  9.  
  10.     if ( is_page ( $pagina )  ) {
  11.  
  12.              
  13. //    global $wp;
  14. //    if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
  15.  
  16. ?>
  17. <script>
  18. window.dataLayer = window.dataLayer || [];
  19. window.dataLayer.push({
  20.   event: 'purchase',
  21.   ecommerce: {
  22.     currency: '<?php echo $order_currency; ?>',
  23.     value: <?php echo $order_total; ?>, // Total (+ impuestos pero sin gastos de envío)
  24.     tax: <?php echo $order_total_tax; ?>,
  25.     shipping: <?php echo $order_shipping_total; ?>,
  26.     affiliation: 'afiliado3ed3455', // ID del afiliado
  27.     transaction_id: '<?php echo $order_id ; ?>',
  28.     coupon: '<?php echo $coupon_codes; ?>',
  29.     items: [<?php $i = 0; foreach ($order->get_items() as $item_key => $item ): $i++; ?>{
  30.           <?php $item_data= $item->get_data(); $product_name= $item_data['name']; $product_id= $item_data['product_id']; $variation_id = $item_data['variation_id']; $quantity = $item_data['quantity']; $product = $item->get_product(); $product_type = $product->get_type();$product_price = $product->get_price();
  31.             $categories = '';
  32.             $terms = get_the_terms( $product_id, 'product_cat' );
  33.             $categories_counter = 0;
  34.             foreach ( $terms as $term ) {
  35.                 $categories_counter++;
  36.                 $limiter = '';
  37.                 if(count($terms) != 1 && $categories_counter != count($terms)): $limiter = ','; endif;
  38.                 $categories .= $term->name . $limiter;
  39.             }
  40.           ?>
  41.       item_name: '<?php echo $product_name; ?>',
  42.       item_id: '<?php echo $product_id; ?>',
  43.       price: '<?php echo $product_price; ?>',
  44.       item_brand: '',
  45.       item_category: '<?php echo $categories; ?>',
  46.       item_variant: '<?php echo $variation_id; ?>',
  47.       quantity: '<?php echo  $quantity; ?>'
  48.     }
  49.     <?php
  50.        
  51.         if($product_number != 1 && $i != $product_number): ?>,<?php endif; ?><?php endforeach ?>]
  52.   }
  53. });
  54. </script>
  55.  
  56. <?php }
  57.     }
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment