Export.php 3.19 KB
<?php

namespace common\modules\product\models;

use common\modules\product\helpers\ProductHelper;
use yii\base\Model;

class Export extends Model {
    public $errors = [];
    public $output = [];

    public function process($dirName, $filename = null, $use_not_enables = false) {
        if (is_null($filename)) {
            $filename = 'products_'. date('d_m_Y_H_i') .'.csv';
        }
        setlocale(LC_ALL, 'ru_RU.CP1251');
        $handle = fopen($dirName .'/'. $filename, "w");
        $products = Product::find()->joinWith(['variants'])->where(['!=', ProductVariant::tableName() .'.stock', 0])->select('product.product_id')->all();
        $i = 0;
        foreach ($products as $product_id)
        {
            $product = Product::findOne($product_id);
            $i++;
            /*if ($i>1e2) {
                break;
            }*/
            $mods = [];

            foreach ($product->enabledVariants as $variant)
            {
                $size = $color = '';
                if ($product->product_variant_type_id) {
                    $productVariantType = ProductVariantType::findOne($product->product_variant_type_id);
                    if ($productVariantType) {
                        if ($productVariantType->product_variant_type_id == ProductHelper::PRODUCT_VARIANT_TYPE_COLOR) {
                            $color = $product->name;
                        } elseif ($productVariantType->product_variant_type_id == ProductHelper::PRODUCT_VARIANT_TYPE_SIZE) {
                            $size = $product->name;
                        }
                    }
                }
                $mods[] = $variant->sku . '=' . $size . '=' . $color . '=' . $variant->imageUrl;
            }

            $fotos = [];

            foreach ($product->images as $image)
            {
                $fotos[] = $image->imageUrl;
            }

            $filters = $product->properties;

            $list = [
                $product->category->name,
                $product->brand->name,
                $product->name,
                '',
                ((! empty($product->description)) ? $product->description : ''),
                ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET]) : ''),
                '',
                ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_SEX])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_SEX]) : ''),
                ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR]) : ''),
                $product->price_old,
                $product->price,
                intval($product->akciya),
                '',
                intval($product->new),
                intval($product->top),
                '',
                $product->video,
                implode (',', $fotos),
            ];

            $to_write = array_merge ($list, $mods);
            foreach($to_write as &$cell) {
                $cell = iconv("UTF-8", "WINDOWS-1251", $cell);
            }

            fputcsv($handle, $to_write, ';');
        }

        fclose ($handle);

        return $dirName .'/'. $filename;
    }
}