-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpricespy-products.php
235 lines (214 loc) · 9.18 KB
/
pricespy-products.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Plugin Name: PriceSpy Products
* Plugin URI: https://storepro.io
* Author: StorePro
* Version: 1.0
* Author URI: https://storepro.io
* Description: Similar Products from PriceSpy
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Enqueue scripts and styles.
*/
add_action( 'wp_enqueue_scripts', 'sp_wp_enqueue_scripts' );
function sp_wp_enqueue_scripts() {
wp_enqueue_style( 'pricespy-style', plugins_url('', __FILE__) . '/css/pricespy-style.css', false, '1.0.0' );
wp_enqueue_script( 'pricespy-jquery', plugins_url('', __FILE__) . '/js/pricespy-jquery.js', array( 'jquery' ) );
}
//===== Get Access Token ===========
function getAccessToken() {
$prspy_token_url = "https://api.schibsted.com/prisjakt/partner-search/token";
$prspy_client_id = "40fd38daf4db4ebeb771acb2c97fa98d";
$prspy_client_secret = "9B8CDfd800D6471EBC8A0e919D026Ef5";
$content = "grant_type=client_credentials&scope=client&client_id=$prspy_client_id&client_secret=$prspy_client_secret";
// $authorization = base64_encode("$prspy_client_id:$prspy_client_secret");
// $header = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
$header = array("Content-Type: application/x-www-form-urlencoded");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $prspy_token_url,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $content
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response)->access_token;
}
// step B - with the returned access_token we can make as many calls as we want
function getResource($access_token, $product_id) {
$prspy_api_url = "https://api.schibsted.com/prisjakt/partner-search/products/$product_id?ref=60025&market=gb";
error_log("API=>");
//error_log($prspy_api_url );
// $header = array("Authorization: Bearer {$access_token}");
// Add the Authorization and X-Client-ID headers
$header = array(
"Authorization: Bearer {$access_token}",
"client-id: 40fd38daf4db4ebeb771acb2c97fa98d"
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $prspy_api_url,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($curl);
//error_log(print_r($response, true));
curl_close($curl);
return json_decode($response, true);
}
// ===== PRICESPY PRODUCTS (SHORTCODE) =====
add_shortcode( 'pricespy_products', 'sp_pricespy_products' );
function sp_pricespy_products($atts) {
// Params extraction
extract(
shortcode_atts(
array(
'product_id' => ''
),
$atts
)
);
ob_start();
if(!empty($product_id)){
$access_token = getAccessToken();
//error_log("access token=>>");
//error_log($access_token );
$resource = getResource($access_token, $product_id);
if(!empty($resource) && empty($resource['error'])){ ?>
<div class="prspy-price-div">
<table>
<thead>
<tr>
<th></th>
<th>Store</th>
<th>Product</th>
<!-- <th>Stock</th> -->
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($resource['offers'] as $offer) {
if($i < 10){
$i++;
}else{
break;
}
?>
<tr class="prspy-main-prices">
<td>
<span><a href="#!"><i class="prspy-arrow down"></i></a></span>
</td>
<td>
<div class="prspy-main-logo">
<?php if(!empty($offer['shop']['logo']['small'])){ ?>
<img src="<?=$offer['shop']['logo']['small']?>">
<?php }else{
echo '<span class="prspy-company-name">'.$offer['shop']['name'].'</span>';
} ?>
</div>
</td>
<td>
<?=$resource['productName'] ?>
</td>
<td class="prspy-price">
<?=getCurrencySymbol($offer['price']['currency']).$offer['price']['value']?>
<div class="prspy-price-delivery">
<?=getDeliveryData($offer['price'])?>
</div>
</td>
<td class="prspy-btn">
<?php if(!empty($offer['url'])){ ?>
<a class="button" href="<?=$offer['url']?>" target="_blank">View in Shop</a>
<?php } ?>
</td>
</tr>
<?php
if(!empty($offer['alternativePrices'])){
foreach ($offer['alternativePrices'] as $alt_price) { ?>
<tr class="prspy-atv-prices">
<td></td>
<td colspan="2">
<?=$alt_price['name']?>
</td>
<td class="prspy-price">
<?=getCurrencySymbol($alt_price['price']['currency']).$alt_price['price']['value']?>
<div class="prspy-price-delivery">
<?=getDeliveryData($alt_price['price'])?>
</div>
</td>
<td class="prspy-btn">
<?php if(!empty($alt_price['url'])){ ?>
<a class="button" href="<?=$alt_price['url']?>" target="_blank">View in Shop</a>
<?php } ?>
</td>
</tr>
<?php }
}
?>
<?php } ?>
</tbody>
</table>
</div>
<?php }else{ ?>
<div class="prspy-error"><?= ($resource['error']) ? $resource['error']['message'] : 'Cannot fetch PriceSpy data!!' ?></div>
<?php }
}else{ ?>
<div class="prspy-error">Error!. No Product Id found.</div>
<?php }
return ob_get_clean();
}
function getStockStatusText($stock) {
$stock_status_text = '';
// $img_stock_staus = plugins_url('', __FILE__).'/images/in-stock.png';
if($stock['status_text'] == ''){
if($stock['status'] == 'in_stock'){
$stock_status_text = "In stock";
}else if($stock['status'] == 'not_in_stock'){
$stock_status_text = "Not in stock";
}else if($stock['status'] == null){
$stock_status_text = "Unknown stock";
}
}else{
$stock_status_text = $stock['status_text'];
}
return $stock_status_text;
}
// function getStockStatusImage($stock) {
// if($stock['status'] == 'in_stock'){
// $img_stock_staus = plugins_url('', __FILE__).'/images/in-stock.png';
// }else if($stock['status'] == 'not_in_stock'){
// $img_stock_staus = plugins_url('', __FILE__).'/images/out-of-stock.png';
// }else {
// $img_stock_staus = plugins_url('', __FILE__).'/images/unknown-stock.png';
// }
// return $img_stock_staus;
// }
function getCurrencySymbol($currencyCode, $locale = 'en_US')
{
$formatter = new \NumberFormatter($locale . '@currency=' . $currencyCode, \NumberFormatter::CURRENCY);
return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
}
function getDeliveryData($price_array) {
// Initialize delivery data as 'Not Available' by default
$delivery_data = '';
// Check if 'includingShipping' exists and is not null
if (isset($price_array['includingShipping']) && $price_array['includingShipping'] !== null) {
// Check if 'value' exists and is equal to 'includingShipping'
if (isset($price_array['value']) && $price_array['value'] == $price_array['includingShipping']) {
$delivery_data = 'Free delivery';
} else {
// Use 'includingShipping' for delivery cost if different from 'value'
$delivery_data = getCurrencySymbol($price_array['currency']) . $price_array['includingShipping'] . ' Incl. delivery';
}
}
return $delivery_data;
}