This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
InitiateCheckout.php
86 lines (80 loc) · 2.51 KB
/
InitiateCheckout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
*/
namespace Facebook\BusinessExtension\Block\Pixel;
class InitiateCheckout extends Common
{
/**
* @return string
*/
public function getContentIDs()
{
$productIds = [];
$cart = $this->fbeHelper->getObject(\Magento\Checkout\Model\Cart::class);
$items = $cart->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
$product = $item->getProduct();
$productIds[] = $product->getId();
}
return $this->arrayToCommaSeparatedStringValues($productIds);
}
public function getValue()
{
$cart = $this->fbeHelper->getObject(\Magento\Checkout\Model\Cart::class);
if (!$cart || !$cart->getQuote()) {
return null;
}
$subtotal = $cart->getQuote()->getSubtotal();
if ($subtotal) {
$priceHelper = $this->fbeHelper->getObject(\Magento\Framework\Pricing\Helper\Data::class);
return $priceHelper->currency($subtotal, false, false);
} else {
return null;
}
}
/**
* @return string
*/
public function getContents()
{
$cart = $this->fbeHelper->getObject(\Magento\Checkout\Model\Cart::class);
if (!$cart || !$cart->getQuote()) {
return '';
}
$contents = [];
$items = $cart->getQuote()->getAllVisibleItems();
$priceHelper = $this->objectManager->get(\Magento\Framework\Pricing\Helper\Data::class);
foreach ($items as $item) {
$product = $item->getProduct();
$price = $priceHelper->currency($product->getFinalPrice(), false, false);
$content = '{id:"' . $product->getId() . '",quantity:' . (int)$item->getQty()
. ',item_price:' . $price . "}";
$contents[] = $content;
}
return implode(',', $contents);
}
/**
* @return int|null
*/
public function getNumItems()
{
$cart = $this->fbeHelper->getObject(\Magento\Checkout\Model\Cart::class);
if (!$cart || !$cart->getQuote()) {
return null;
}
$numItems = 0;
$items = $cart->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
$numItems += $item->getQty();
}
return $numItems;
}
/**
* @return string
*/
public function getEventToObserveName()
{
return 'facebook_businessextension_ssapi_initiate_checkout';
}
}