731adb96
Alex Savenko
remarketing
|
1
2
3
4
5
6
7
8
9
10
|
<?php
/**
* Created by PhpStorm.
* User: Alex Savenko
* Date: 05.12.2016
* Time: 18:36
*/
namespace console\controllers;
|
e71d5d51
Alex Savenko
remarketing
|
11
|
use yii\console\Controller;
|
731adb96
Alex Savenko
remarketing
|
12
|
use Yii;
|
fd068f14
Alex Savenko
remarketing
|
13
|
use yii\helpers\Url;
|
e71d5d51
Alex Savenko
remarketing
|
14
|
use common\modules\product\models\Product;
|
99f3201f
Alex Savenko
remarketing
|
15
|
use common\modules\product\models\ProductVariant;
|
731adb96
Alex Savenko
remarketing
|
16
|
|
8d17a363
Alex Savenko
remarketing
|
17
18
19
20
21
22
23
24
|
use common\modules\product\models\Category;
use frontend\models\ProductFrontendSearch;
use common\models\Page;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
|
731adb96
Alex Savenko
remarketing
|
25
26
27
|
class FeedController extends Controller
{
|
9b454120
Alex Savenko
remarketing
|
28
|
private $idList = [];
|
731adb96
Alex Savenko
remarketing
|
29
30
|
private $count = 1;
|
e71d5d51
Alex Savenko
remarketing
|
31
|
public function getProducts() {
|
99f3201f
Alex Savenko
remarketing
|
32
33
34
|
return Product::find()
->limit(1000)
->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. Product::tableName() .'.product_id')
|
cc601340
Alex Savenko
remarketing
|
35
|
->where(['and', ProductVariant::tableName().'.status = 0', ProductVariant::tableName().'.stock > 0'])
|
99f3201f
Alex Savenko
remarketing
|
36
|
->all();
|
e71d5d51
Alex Savenko
remarketing
|
37
|
}
|
731adb96
Alex Savenko
remarketing
|
38
|
|
9b454120
Alex Savenko
remarketing
|
39
40
|
public function createRow( $product, &$content ){
if ($this->checkId($product->product_id)) {
|
99f3201f
Alex Savenko
remarketing
|
41
|
print $this->count++ . "\r\n";
|
0b1d02d7
Alex Savenko
remarketing
|
42
43
44
45
46
47
48
49
|
$content[] = [
$product->product_id,
$product->category->category_id,
$product->category->name,
$product->name,
$product->description,
$product->price.' UAH',
];
|
9b454120
Alex Savenko
remarketing
|
50
51
52
53
54
55
|
}
}
public function checkId($id){
if(!in_array($id, $this->idList)){
$this->idList[] = $id;
|
e71d5d51
Alex Savenko
remarketing
|
56
57
58
59
60
61
|
return true;
} else {
return false;
}
}
|
731adb96
Alex Savenko
remarketing
|
62
63
64
65
66
67
|
public function actionProcess() {
$dirName = Yii::getAlias('@frontend').'/web';
$filename = 'feed.csv';
setlocale(LC_ALL, 'ru_RU.CP1251');
|
0cd58f97
Alex Savenko
remarketing
|
68
|
$handle = fopen($dirName .'/'. $filename, "w");
|
731adb96
Alex Savenko
remarketing
|
69
|
|
e71d5d51
Alex Savenko
remarketing
|
70
71
72
|
$content[] = ['ID', 'ID2', 'Item Category', 'Item title', 'Item description', 'Price'];
$content[] = ['ID', 'ID2', 'Item Category', 'Item title', 'Item description', 'Price'];
|
050681b5
Alex Savenko
remarketing
|
73
74
75
|
foreach ($this->getProducts() as $product) {
$this->createRow($product, $content);
}
|
e71d5d51
Alex Savenko
remarketing
|
76
77
78
79
|
foreach ($content as $item) {
fputcsv($handle, $item);
}
|
0cd58f97
Alex Savenko
remarketing
|
80
|
fclose($handle);
|
731adb96
Alex Savenko
remarketing
|
81
82
83
84
|
print $dirName .'/'. $filename;
}
}
|