Skip to content

Commit f0ab341

Browse files
committed
discounts added
1 parent 145e100 commit f0ab341

File tree

5 files changed

+128
-93
lines changed

5 files changed

+128
-93
lines changed

Diff for: .DS_Store

6 KB
Binary file not shown.

Diff for: includes/.DS_Store

6 KB
Binary file not shown.

Diff for: includes/Frontend.php

+33-18
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,38 @@ function updateCartX() {
6363
// define the woocommerce_thankyou callback
6464
function action_woocommerce_thankyou($order_get_id)
6565
{
66-
$code = '';
67-
$order_id = absint($order_get_id);
68-
if ($order_id > 0) {
66+
if ($_GET['justuno-debug'] == true) {
67+
$order_id = absint($order_get_id);
68+
echo $order_id . ':::';
6969
$order = wc_get_order($order_id);
70-
$code .= '
70+
print_r($order);
71+
exit;
72+
}
73+
try {
74+
$code = '';
75+
$order_id = absint($order_get_id);
76+
if ($order_id > 0) {
77+
$order = wc_get_order($order_id);
78+
$code .= '
7179
juapp("order", "' . $order->get_id() . '", {
7280
total:' . floatval($order->get_total()) . ',
7381
subtotal:' . floatval($order->get_subtotal()) . ',
7482
tax:' . floatval($order->get_total_tax()) . ',
7583
shipping:' . floatval($order->get_shipping_total()) . ',
7684
currency: "' . $order->get_currency() . '"
7785
});';
78-
foreach ($order->get_items() as $item) {
79-
$tmpCode = '';
80-
foreach ($item->get_meta_data() as $meta) {
81-
if (strpos(strtolower($meta->key), "color") !== FALSE) {
82-
$tmpCode .= 'color:`' . $meta->value . '`,';
83-
$tmpCode .= "\n";
84-
}
85-
if (strpos(strtolower($meta->key), "size") !== FALSE) {
86-
$tmpCode .= 'size:`' . $meta->value . '`,';
86+
foreach ($order->get_items() as $item) {
87+
$tmpCode = '';
88+
foreach ($item->get_meta_data() as $meta) {
89+
if (strpos(strtolower($meta->key), "color") !== FALSE) {
90+
$tmpCode .= 'color:`' . $meta->value . '`,';
91+
$tmpCode .= "\n";
92+
}
93+
if (strpos(strtolower($meta->key), "size") !== FALSE) {
94+
$tmpCode .= 'size:`' . $meta->value . '`,';
95+
}
8796
}
88-
}
89-
$code .= 'juapp("orderItem", {
97+
$code .= 'juapp("orderItem", {
9098
productid:' . $item->get_product_id() . ',
9199
variationid:' . ($item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id()) . ',
92100
sku:`' . $item->get_product()->get_sku() . '`,
@@ -95,10 +103,17 @@ function action_woocommerce_thankyou($order_get_id)
95103
' . $tmpCode . '
96104
price:' . floatval($item->get_total()) . '
97105
});';
106+
}
107+
}
108+
echo '<script type="text/javascript">' . $code . '</script>';
109+
} catch (\Error $e) {
110+
if ($_GET['debug'] == true) {
111+
print_r($e);
112+
exit;
98113
}
99114
}
100-
echo '<script type="text/javascript">' . $code . '</script>';
101-
};
115+
}
116+
;
102117

103118
// add the action
104-
add_action('woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1);
119+
add_action('woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1);

Diff for: includes/JustRESTManager.php

+31-9
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@ public function verifyWooCommerceToken($haders)
2424

2525
public function entertainCall()
2626
{
27-
try{
27+
try {
2828
try {
2929
$request = $_GET;
3030
$data = [];
3131
$isVerifiedToken = $this->verifyWooCommerceToken($_SERVER);
32-
if ($isVerifiedToken === true) {
32+
if ($isVerifiedToken === true || $request['type'] === 'cart') {
3333
if ($request['type'] === 'verbose') {
3434
$this->getVerboseData($request);
3535
http_response_code(200);
3636
exit;
3737
}
38-
39-
if ($request['type'] === 'cart') {
40-
$data = $this->getCartData();
38+
if ($request['type'] === 'discount') {
39+
$return = $this->applyDiscountData();
40+
if ($return === false) {
41+
header("HTTP/1.1 401 Unauthorized");
42+
echo json_encode(['message' => 'Invalid Code.']);
43+
exit;
44+
} else {
45+
$data = ['message' => 'Cuopon Applied successfully.'];
46+
}
47+
} else if ($request['type'] === 'cart') {
48+
$data = $this->JustWooService->getCartData($data);
4149
} else if ($request['type'] === 'order') {
4250
$data = $this->getOrderData($request);
4351
} else {
@@ -53,12 +61,14 @@ public function entertainCall()
5361
exit;
5462
} catch (\Exception $e) {
5563
if ($_GET['debug'] == true) {
56-
print_r($e);exit;
64+
print_r($e);
65+
exit;
5766
}
5867
}
59-
}catch (\Error $e) {
68+
} catch (\Error $e) {
6069
if ($_GET['debug'] == true) {
61-
print_r($e);exit;
70+
print_r($e);
71+
exit;
6272
}
6373
}
6474
}
@@ -89,9 +99,21 @@ public function getOrderData($data)
8999
echo json_encode(['message' => 'No Ecommerce plugin such as WooCommerce is active.']);
90100
exit;
91101
}
92-
102+
103+
public function applyDiscountData()
104+
{
105+
if (class_exists('woocommerce')) {
106+
return $this->JustWooService->applyDiscountData($_GET);
107+
}
108+
header("HTTP/1.1 401 Unauthorized");
109+
echo json_encode(['message' => 'No Ecommerce plugin such as WooCommerce is active.']);
110+
exit;
111+
}
112+
93113
public function getCartData($data)
94114
{
115+
print_r('sdf');
116+
exit;
95117
if (class_exists('woocommerce')) {
96118
return $this->JustWooService->getCartData($data);
97119
}

Diff for: includes/integrations/JustWooCommerce.php

+64-66
Original file line numberDiff line numberDiff line change
@@ -355,56 +355,56 @@ public function get_items($items)
355355
return $return;
356356
}
357357

358+
public function applyDiscountData($data)
359+
{
360+
$cart = \WC()->cart;
361+
if (isset($data["code"]) && $data["code"] !== "") {
362+
if ($cart->apply_coupon($data["code"]) == true) {
363+
return true;
364+
}
365+
}
366+
return false;
367+
}
368+
358369
public function getCartData()
359370
{
360-
if (!is_null($cart)) {
361-
$cart = \WC()->cart;
362-
$totals = $cart->get_totals();
363-
$return = [
364-
'total' => $totals['total'],
365-
'subtotal' => $totals['subtotal'],
366-
'tax' => $totals['total_tax'],
367-
'shipping' => $totals['shipping_total'],
368-
'currency' => "USD",
369-
'items' => [],
370-
];
371+
$cart = \WC()->cart;
372+
$totals = $cart->get_totals();
373+
$return = [
374+
'total' => $totals['total'],
375+
'subtotal' => $totals['subtotal'],
376+
'tax' => $totals['total_tax'],
377+
'shipping' => $totals['shipping_total'],
378+
'currency' => "USD",
379+
'items' => [],
380+
];
371381

372-
$cartItems = $cart->get_cart_contents();
373-
if (count($cartItems) > 0) {
374-
foreach ($cart->get_cart() as $key => $item) {
375-
$cartItem = $cart->get_cart_item($key);
376-
$attrs = '';
377-
if ($cartItem['variation_id'] > 0) {
378-
foreach ($cartItem['variation'] as $key => $value) {
379-
if (strpos(strtolower($key), "color") !== FALSE) {
380-
$attrs .= "color: '`{$value}`, ";
381-
}
382-
if (strpos(strtolower($key), "size") !== FALSE) {
383-
$attrs .= "size: `{$value}`, ";
384-
}
382+
$cartItems = $cart->get_cart_contents();
383+
if (count($cartItems) > 0) {
384+
foreach ($cart->get_cart() as $key => $item) {
385+
$cartItem = $cart->get_cart_item($key);
386+
$attrs = '';
387+
if ($cartItem['variation_id'] > 0) {
388+
foreach ($cartItem['variation'] as $key => $value) {
389+
if (strpos(strtolower($key), "color") !== FALSE) {
390+
$attrs .= "color: '`{$value}`, ";
391+
}
392+
if (strpos(strtolower($key), "size") !== FALSE) {
393+
$attrs .= "size: `{$value}`, ";
385394
}
386395
}
387-
$variationId = $cartItem['variation_id'] > 0 ? $cartItem['variation_id'] : $cartItem['product_id'];
388-
$product = $cartItem['data'];
389-
$return['items'][] = [
390-
"productid" => $cartItem['product_id'],
391-
"variationid" => $variationId,
392-
"sku" => $product->get_sku(),
393-
"quantity" => $cartItem['quantity'],
394-
"price" => $product->get_price(),
395-
"{$attrs}name" => $product->get_name()
396-
];
397396
}
397+
$variationId = $cartItem['variation_id'] > 0 ? $cartItem['variation_id'] : $cartItem['product_id'];
398+
$product = $cartItem['data'];
399+
$return['items'][] = [
400+
"productid" => $cartItem['product_id'],
401+
"variationid" => $variationId,
402+
"sku" => $product->get_sku(),
403+
"quantity" => $cartItem['quantity'],
404+
"price" => $product->get_price(),
405+
"{$attrs}name" => $product->get_name()
406+
];
398407
}
399-
} else {
400-
$return = [
401-
'total' => 0,
402-
'subtotal' => 0,
403-
'tax' => 0,
404-
'shipping' => 0,
405-
'currency' => "USD",
406-
'items' => [],
407-
];
408408
}
409409
return $return;
410410
}
@@ -432,40 +432,38 @@ public function getConversionTrackingCodes()
432432
}
433433

434434
$cart = \WC()->cart;
435-
if (!is_null($cart)) {
436-
$totals = $cart->get_totals();
437-
$code .= 'juapp("cart", {
435+
$totals = $cart->get_totals();
436+
$code .= 'juapp("cart", {
438437
total:' . $totals['total'] . ',
439438
subtotal:' . $totals['subtotal'] . ',
440439
tax:' . $totals['total_tax'] . ',
441440
shipping:' . $totals['shipping_total'] . ',
442441
currency:"USD",
443442
}
444443
);';
445-
$cartItems = $cart->get_cart_contents();
446-
if (count($cartItems) > 0) {
447-
$code .= "juapp('cartItems', [";
448-
foreach ($cart->get_cart() as $key => $item) {
449-
$cartItem = $cart->get_cart_item($key);
450-
$attrs = '';
451-
if ($cartItem['variation_id'] > 0) {
452-
foreach ($cartItem['variation'] as $key => $value) {
453-
if (strpos(strtolower($key), "color") !== FALSE) {
454-
$attrs .= "color: `{$value}`, ";
455-
}
456-
if (strpos(strtolower($key), "size") !== FALSE) {
457-
$attrs .= "size: `{$value}`, ";
458-
}
444+
$cartItems = $cart->get_cart_contents();
445+
if (count($cartItems) > 0) {
446+
$code .= "juapp('cartItems', [";
447+
foreach ($cart->get_cart() as $key => $item) {
448+
$cartItem = $cart->get_cart_item($key);
449+
$attrs = '';
450+
if ($cartItem['variation_id'] > 0) {
451+
foreach ($cartItem['variation'] as $key => $value) {
452+
if (strpos(strtolower($key), "color") !== FALSE) {
453+
$attrs .= "color: `{$value}`, ";
454+
}
455+
if (strpos(strtolower($key), "size") !== FALSE) {
456+
$attrs .= "size: `{$value}`, ";
459457
}
460458
}
461-
$variationId = $cartItem['variation_id'] > 0 ? $cartItem['variation_id'] : $cartItem['product_id'];
462-
$product = $cartItem['data'];
463-
$code .= "
464-
{ productid: '{$cartItem['product_id']}', variationid: '{$variationId}', sku:`{$product->get_sku()}`, quantity: {$cartItem['quantity']}, price: {$product->get_price()}, {$attrs}name: `{$product->get_name()}`},";
465459
}
466-
$code = substr($code, 0, -1);
467-
$code .= "])";
460+
$variationId = $cartItem['variation_id'] > 0 ? $cartItem['variation_id'] : $cartItem['product_id'];
461+
$product = $cartItem['data'];
462+
$code .= "
463+
{ productid: '{$cartItem['product_id']}', variationid: '{$variationId}', sku:`{$product->get_sku()}`, quantity: {$cartItem['quantity']}, price: {$product->get_price()}, {$attrs}name: `{$product->get_name()}`},";
468464
}
465+
$code = substr($code, 0, -1);
466+
$code .= "])";
469467
}
470468
return $code;
471469
}

0 commit comments

Comments
 (0)