| 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 |
|