Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Envia el evento purchase cuando se carga la página /gracias-por-su-compra/
- add_action( 'wp_head', 'carlosmdh_purchase_datalayer_push' );
- function carlosmdh_purchase_datalayer_push() {
- $pagina = 'gracias-por-su-compra'; // cambiar el valor de esta variable por el slug, la id, o el nombre de la página
- if ( is_page ( $pagina ) ) {
- // global $wp;
- // if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
- ?>
- <script>
- window.dataLayer = window.dataLayer || [];
- window.dataLayer.push({
- event: 'purchase',
- ecommerce: {
- currency: '<?php echo $order_currency; ?>',
- value: <?php echo $order_total; ?>, // Total (+ impuestos pero sin gastos de envío)
- tax: <?php echo $order_total_tax; ?>,
- shipping: <?php echo $order_shipping_total; ?>,
- affiliation: 'afiliado3ed3455', // ID del afiliado
- transaction_id: '<?php echo $order_id ; ?>',
- coupon: '<?php echo $coupon_codes; ?>',
- items: [<?php $i = 0; foreach ($order->get_items() as $item_key => $item ): $i++; ?>{
- <?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();
- $categories = '';
- $terms = get_the_terms( $product_id, 'product_cat' );
- $categories_counter = 0;
- foreach ( $terms as $term ) {
- $categories_counter++;
- $limiter = '';
- if(count($terms) != 1 && $categories_counter != count($terms)): $limiter = ','; endif;
- $categories .= $term->name . $limiter;
- }
- ?>
- item_name: '<?php echo $product_name; ?>',
- item_id: '<?php echo $product_id; ?>',
- price: '<?php echo $product_price; ?>',
- item_brand: '',
- item_category: '<?php echo $categories; ?>',
- item_variant: '<?php echo $variation_id; ?>',
- quantity: '<?php echo $quantity; ?>'
- }
- <?php
- if($product_number != 1 && $i != $product_number): ?>,<?php endif; ?><?php endforeach ?>]
- }
- });
- </script>
- <?php }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment