Skip to content

Commit 1dea454

Browse files
author
Taras Kubiv
committed
Merged in APITOCART-16387-demo-module-fix-price-updating (pull request #40)
APITOCART-16387-demo-module-fix-price-updating [MASTER] Approved-by: Oleg Syvak
2 parents fd07c33 + ad65675 commit 1dea454

File tree

7 files changed

+133
-215
lines changed

7 files changed

+133
-215
lines changed

run_me.sh

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@ docker-compose down
4141
#run containers in background
4242
docker-compose up -d
4343

44-
#run once for the first time
45-
#if [ $? -eq 0 ];
46-
#then
47-
# echo "Looks all ok, update related dependencies..."
48-
# docker-compose run app composer update
49-
# docker-compose run app php artisan migrate
50-
# docker-compose run app php artisan db:seed
51-
#else
52-
# echo "missing start please contact support"
53-
# exit
54-
#fi
44+
if [ $? -eq 0 ];
45+
then
46+
echo "Looks all ok, update related dependencies..."
47+
docker-compose run --rm composer update
48+
docker-compose run app php artisan migrate
49+
docker-compose run app php artisan db:seed
50+
else
51+
echo "missing start please contact support"
52+
exit
53+
fi
5554

5655
if ! [ -x "$(command -v xdg-open)" ]; then
5756
open https://demo-module.api2cart.local.com:8443

src/app/Http/Controllers/BusinessCases/AutomaticPriceUpdatingController.php

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Http\Requests\BusinessCases\AutomaticPriceUpdateRequest;
7+
use Carbon\Carbon;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Log;
910
use Illuminate\Support\Facades\Mail;
1011

11-
1212
use App\Services\Api2Cart;
1313

1414

@@ -24,139 +24,108 @@ public function __construct(Api2Cart $api2Cart)
2424

2525
public function index()
2626
{
27-
28-
// $result = $this->api2cart->getProductList( "1316ad9a66ac871ce46a3d59005acc9c", null, null, null, null , null );
29-
// session()->put('automatic_price_updating', collect( $result['result']['product'] ) );
30-
//
31-
// $products = session()->get('automatic_price_updating');
32-
//
33-
// print_r($products);
34-
3527
return view('business_cases.automatic_price_updating.index');
3628
}
3729

3830

3931
public function create(Request $request)
4032
{
4133
if ( $request->ajax() ){
42-
return response()->json(['data' => view('business_cases.automatic_price_updating.form')->render(), 'log' => $this->api2cart->getLog() ]);
34+
$carts = collect($this->api2cart->getCartList());
35+
$isCreate = true;
36+
return response()->json(['data' => view('business_cases.automatic_price_updating.form', compact('isCreate', 'carts'))->render(), 'log' => $this->api2cart->getLog() ]);
4337
}
4438

4539
return redirect(route('business_cases.automatic_price_updating'));
4640
}
4741

4842
public function store(AutomaticPriceUpdateRequest $request)
4943
{
50-
Log::debug( $request->all() );
51-
52-
$products = [];
53-
$carts = collect($this->api2cart->getCartList());
54-
5544
$formData = $request->except('_hash');
5645
$formData['model'] = $formData['sku'];
5746

58-
foreach ($carts as $item){
59-
60-
$result = $this->api2cart->addProduct($item['store_key'], $formData);
47+
list($returnCode, $result) = $this->api2cart->addProduct($formData['cart_id'], $formData);
6148

62-
// add image for each product
63-
if ( isset($result['result']['product_id']) && $result['result']['product_id'] != '' ){
49+
if ($returnCode == 0) {
50+
// add image to product
51+
if ( isset($result['result']['product_id']) && $result['result']['product_id'] != '' ) {
6452

65-
$this->api2cart->addProductImage( $item['store_key'], $result['result']['product_id'],
53+
$this->api2cart->addProductImage($formData['cart_id'], $result['result']['product_id'],
6654
// image data goes here
6755
[
6856
'type' => 'base',
6957
'image_name' => '0011ff.png',
7058
'url' => 'https://dummyimage.com/600x400/ffffff/0011ff.png'
7159
]
7260
);
73-
74-
}
75-
76-
if ($result['result']['product_id'] != ''){
77-
$products[] = [
78-
'store_key' => $item['store_key'],
79-
'product_id' => $result['result']['product_id'],
80-
'sku' => $request->get('sku'),
81-
];
8261
}
8362

63+
return response()->json([ 'log' => $this->api2cart->getLog(), 'success' => true, 'item' => $this->api2cart->getProductInfo( $formData['cart_id'], $result['ptoduct_id'] ) ]);
64+
} else {
65+
return response()->json([ 'log' => $this->api2cart->getLog(), 'success' => false, 'errormessage' => $result ]);
8466
}
85-
86-
87-
// Log::debug( print_r($products,1) );
88-
89-
session()->put('automatic_price_updating', $products);
90-
91-
return response()->json(['items' => $products,'log' => $this->api2cart->getLog() ]);
92-
93-
9467
}
9568

9669

9770
public function products(Request $request)
9871
{
99-
$products = [];
100-
$products_ids = session()->get('automatic_price_updating');
101-
102-
// Log::debug( print_r($products_ids,1) );
103-
104-
if ( !$products_ids ) return response(null, 404);
105-
106-
foreach ($products_ids as $item){
107-
$tmp = $this->api2cart->getProductInfo( $item['store_key'], $item['product_id'] );
108-
if ($tmp) {
109-
$tmp['store_key'] = $item['store_key'];
110-
$products[] = $tmp;
72+
$products = collect([]);
73+
$limit = 5;
74+
$carts = $request->get('store_keys', []);
75+
76+
foreach ($carts as $storeKey) {
77+
$productsInfo = $this->api2cart->getProductList($storeKey, null, 'create_at', 'desc', $limit);
78+
79+
if ($productsInfo) {
80+
foreach ($productsInfo['result']['product'] ?? [] as $productInfo) {
81+
$productInfo['store_key'] = $storeKey;
82+
$productInfo['create_at']['value'] = Carbon::parse($productInfo['create_at']['value'])->setTimezone('UTC')->format("Y-m-d\TH:i:sO");
83+
$products->push($productInfo);
84+
}
11185
}
11286
}
11387

114-
// Log::debug( print_r($products,1) );
115-
88+
$items = [];
11689

117-
return response()->json(['items'=>$products]);
90+
foreach ($products->sortBy('create_at.value', null, true)->toArray() as $item) {
91+
$items[] = $item;
92+
}
11893

94+
return response()->json(['items' => $items]);
11995
}
12096

12197
public function edit(Request $request)
12298
{
12399
$this->api2cart->debug = false;
100+
$isCreate = false;
101+
$storeKey = $request->get('store_key', '');
102+
$productId = $request->get('id', '');
124103

125-
$products = [];
126-
$products_ids = session()->get('automatic_price_updating');
104+
$product = [];
127105

128-
foreach ($products_ids as $item){
129-
$tmp = $this->api2cart->getProductInfo( $item['store_key'], $item['product_id'] );
130-
if ($tmp) {
131-
$tmp['store_key'] = $item['store_key'];
132-
$products[] = $tmp;
133-
}
134-
}
106+
if ($storeKey && $productId) {
107+
$res = $this->api2cart->getProductInfo($storeKey, $productId);
135108

136-
if ( count($products) != count($products_ids) ){
137-
// looks stores reseting
138-
// Log::debug( print_r($products,1) );
139-
session()->flash('automatic_price_updating');
140-
return response('Please create new product scope',404);
109+
if ($res) {
110+
$res['store_key'] = $storeKey;
111+
$product = $res;
112+
}
141113
}
142114

143-
$product = $products[0];
144-
145115
if ( $request->ajax() ){
146-
return response()->json(['data' => view('business_cases.automatic_price_updating.form', compact('products','product'))->render(), 'log' => $this->api2cart->getLog() ]);
116+
return response()->json([
117+
'data' => view('business_cases.automatic_price_updating.form', compact('productId', 'storeKey', 'product', 'isCreate'))->render(),
118+
'log' => $this->api2cart->getLog()
119+
]);
147120
}
148121
}
149122

150123

151124
public function update(AutomaticPriceUpdateRequest $request)
152125
{
153-
$products_ids = session()->get('automatic_price_updating');
154126
$formData = $request->except('_token','_method');
155-
$formData['model'] = $formData['sku'];
156127

157-
foreach ($products_ids as $item){
158-
$this->api2cart->updateProduct( $item['store_key'], $item['product_id'], $formData );
159-
}
128+
$this->api2cart->updateProduct($formData['cart_id'], $formData['product_id'], $formData);
160129

161130
return response()->json(['log' => $this->api2cart->getLog()]);
162131

src/app/Http/Requests/BusinessCases/AutomaticPriceUpdateRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function rules()
2525
{
2626
return [
2727
'name' => ['required'],
28-
'sku' => ['required'],
2928
'price' => ['required'],
3029
'description' => ['required'],
3130
'quantity' => ['required'],
31+
'cart_id' => ['required']
3232
];
3333
}
3434
}

src/app/Services/Api2Cart.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,6 @@ public function getProductInfo($store_id=null, $product_id=null)
952952
return null;
953953
}
954954

955-
956-
957955
} catch (\Exception $e){
958956

959957
// Log::debug( $e->getMessage() );
@@ -1012,9 +1010,6 @@ public function updateProduct($store_id=null, $product_id=null, $fields=[])
10121010
null
10131011
);
10141012

1015-
// Log::debug('edit_product_result');
1016-
// Log::debug( print_r($result,1));
1017-
10181013
$this->logApiCall( 'product.update.json', $result->getReturnCode(), $this->product->getConfig(), null, null, null, $result->getReturnMessage(), ['product_id' => $product_id, 'fields' => $fields] );
10191014

10201015
if ( $result->getReturnCode() == 0 ){
@@ -1025,19 +1020,14 @@ public function updateProduct($store_id=null, $product_id=null, $fields=[])
10251020
return null;
10261021
}
10271022

1028-
10291023
} else {
10301024
return null;
10311025
}
10321026

1033-
10341027
} catch (\Exception $e){
1035-
1036-
// Log::debug( $e->getMessage() );
10371028
$this->logApiCall( 'product.update.json', $e->getCode(), $this->account->getConfig(), null, null, null, $e->getMessage(), ['product_id' => $product_id, 'fields' => $fields] );
10381029
return false;
10391030
}
1040-
10411031
}
10421032

10431033
public function addProduct( $store_id=null, $fields=[] ){
@@ -1056,13 +1046,13 @@ public function addProduct( $store_id=null, $fields=[] ){
10561046

10571047
$this->logApiCall( 'product.add.json', $result->getReturnCode(), $this->product->getConfig(), null, null, null, $result->getReturnMessage(), $fields );
10581048

1059-
if ( $result->getReturnCode() == 0 ){
1060-
1061-
return $result;
10621049

1050+
if ($result->getReturnCode() == 0) {
1051+
return [0, $result];
10631052
} else {
1064-
if ($this->debug) Log::debug( print_r($result,1) );
1065-
return null;
1053+
if ($this->debug) Log::debug(print_r($result, 1));
1054+
1055+
return [$result->getReturnCode(), $result->getReturnMessage()];
10661056
}
10671057

10681058

src/resources/views/business_cases/automatic_email_sending/index.blade.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,14 @@ function(data, type, row, meta){
367367
},
368368
{ data: null, render:
369369
function ( data, type, row, meta ){
370+
console.log(data);
370371
let imgName = data.cart_id.cart_info.cart_name.toLowerCase().replace(/ /g,"_");
371-
return '<img class="cartImage" src="https://api2cart.com/wp-content/themes/api2cart/images/logos/'+imgName+'.png"><br>' +
372-
'<a href="'+data.cart_id.url+'">'+data.cart_id.url+'</a><br>'+
373-
'<small>'+data.cart_id.stores_info.store_owner_info.owner+'</small><br>'+
374-
'<small>'+data.cart_id.stores_info.store_owner_info.email+'</small>';
372+
return '<div style="float: left"><span class="cartImage circle-int ' + imgName + '"></span></div>' +
373+
'<div class="cartInfo">' +
374+
'<a href="'+data.cart_id.url+'">'+data.cart_id.url+'</a><br>'+
375+
'<small>'+data.cart_id.stores_info.store_owner_info.owner+'<br>'+
376+
data.cart_id.stores_info.store_owner_info.email+'</small>' +
377+
'</div>';
375378
}
376379
},
377380
{ data: null, render: 'email' },

src/resources/views/business_cases/automatic_price_updating/form.blade.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
<div id="_form_errors" class="text-left"></div>
1212
</div>
1313

14+
@if (isset($carts))
15+
<div class="form-group row">
16+
<label for="name" class="col-4 col-form-label">Store Type</label>
17+
<div class="col-8">
18+
<select class="custom-select" id="cart_id" name="cart_id" required >
19+
<option selected disabled value="">Choose your cart...</option>
20+
@foreach($carts as $item)
21+
<option value="{!! $item['store_key'] !!}">{{ $item['cart_id'] }} [ {{ $item['url'] }} ] </option>
22+
@endforeach
23+
</select>
24+
<div class="invalid-feedback"></div>
25+
</div>
26+
</div>
27+
@elseif (isset($storeKey))
28+
<input type="hidden" name="cart_id" value="{{ $storeKey }}" required>
29+
<input type="hidden" name="product_id" value="{{ $productId }}" required>
30+
@endif
31+
1432
<div class="row">
1533
<div class="col-12">
1634
<div class="form-group row">
@@ -23,6 +41,7 @@
2341
</div>
2442
</div>
2543

44+
@if ($isCreate)
2645
<div class="row">
2746
<div class="col-12">
2847
<div class="form-group row">
@@ -34,6 +53,7 @@
3453
</div>
3554
</div>
3655
</div>
56+
@endif
3757

3858
<div class="row">
3959
<div class="col-12">

0 commit comments

Comments
 (0)