PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / utils / pixel.php

pixel.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/utils/pixel.php

118 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Pixel Utils.
4 *
5 * @version 1.0.0
6 * @since StoreEngine 1.7.4
7 */
8
9 namespace StoreEngine\Utils;
10
11 use StoreEngine\Classes\Countries;
12 use StoreEngine\Classes\Price;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 final class Pixel {
19 public static function get_single_product_data(): array {
20 global $product;
21 $data = [];
22
23 if ( ! Helper::is_product() ) {
24 return $data;
25 }
26
27 $data = [
28 'item_id' => $product->get_id(),
29 'item_name' => $product->get_name(),
30 'price' => 0,
31 ];
32
33 if ( 'variable' !== $product->get_type() ) {
34 $data['price'] = $product->get_prices() ? $product->get_prices()[0]->get_price() : 0;
35 } elseif ( $product->get_variants() ) {
36 try {
37 $variant = $product->get_variants()[0];
38 $price = $variant->get_pricing_id() ? new Price( $variant->get_pricing_id() ) : 0;
39 $data['price'] = $variant->get_price() + $price->get_price();
40 $data['item_variant'] = $variant->get_name();
41 } catch ( \Exception $e ) {
42 // NoOp.
43 }
44 }
45
46 $data = [
47 'product' => $data,
48 'products' => [],
49 ];
50
51 // @XXX upsell ids are used as related.
52 // @TODO make proper related product feature.
53 $related_product_ids = get_post_meta( $product->get_id(), '_storeengine_upsell_ids', true );
54 if ( ! empty( $related_product_ids ) ) {
55 foreach ( $related_product_ids as $related_product_id ) {
56 $related_product = storeengine_get_product( $related_product_id );
57 if ( ! $related_product ) {
58 continue;
59 }
60
61 $data['products'][] = [
62 'item_id' => $related_product->get_id(),
63 'item_name' => $related_product->get_name(),
64 'item_list_id' => 'related_products',
65 'item_list_name' => __( 'Related Products', 'storeengine' ),
66 ];
67 }
68 }
69
70 return $data;
71 }
72
73 public static function get_product_archive_data(): array {
74 $products = [];
75
76 if ( ! Helper::is_shop() ) {
77 return $products;
78 }
79
80 if ( have_posts() ) {
81 $doc_title = wp_get_document_title();
82 while ( have_posts() ) {
83 the_post();
84 global $product;
85 $products[] = [
86 'item_id' => $product->get_id(),
87 'item_name' => wp_strip_all_tags( $product->get_name() ),
88 'item_list_id' => 'shop',
89 'item_list_name' => $doc_title,
90 ];
91 }
92 wp_reset_postdata();
93 }
94
95 return [ 'products' => $products ];
96 }
97
98 public static function get_cart_info(): array {
99 $cart = Helper::cart();
100
101 return [
102 'is_empty' => $cart->is_cart_empty(),
103 'total' => (float) $cart->get_total( 'edit' ),
104 'cart_items' => array_map( function ( $item ) {
105 return [
106 'item_id' => $item->product_id,
107 'item_name' => $item->name,
108 'currency' => Formatting::get_currency(),
109 'discount' => 0,
110 'price' => $item->price,
111 'quantity' => $item->quantity,
112 'item_variant' => implode( ' / ', $item->variation ),
113 ];
114 }, array_values( $cart->get_cart_items() ) ),
115 ];
116 }
117 }
118