102 lines
3.7 KiB
PHP
102 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace KupShop\ComponentsBundle\Actions;
|
|
|
|
use KupShop\ComponentsBundle\Actions\Frontend\UpsellHandler;
|
|
use KupShop\OrderDiscountBundle\Actions\AbstractAction;
|
|
use KupShop\OrderDiscountBundle\Actions\Frontend\HandlerInterface;
|
|
use KupShop\OrderDiscountBundle\Entity\OrderDiscount;
|
|
use KupShop\OrderDiscountBundle\Util\DiscountUtil;
|
|
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;
|
|
use Query\Operator;
|
|
|
|
class UpsellAction extends AbstractAction
|
|
{
|
|
protected static $type = 'upsell';
|
|
protected $adminTemplate = 'actions/upsell.tpl';
|
|
|
|
protected $translationSection = 'UpsellDiscount';
|
|
|
|
public function __construct(
|
|
public UpsellHandler $upsellHandler,
|
|
public DiscountUtil $discountUtil)
|
|
{
|
|
}
|
|
|
|
protected function getVars($vars)
|
|
{
|
|
$vars['upsell'] = [];
|
|
$upsell = ($vars['data']['upsell'] ?? null);
|
|
if ($upsell) {
|
|
$upsell = array_map(function ($v) {
|
|
if (!is_array($v)) {
|
|
$v = ['id_product' => $v];
|
|
}
|
|
|
|
return $v;
|
|
}, $upsell);
|
|
if (!$upsell) {
|
|
return $vars;
|
|
}
|
|
$upsell_products = sqlQueryBuilder()
|
|
->select('p.id, p.title, p.code, p.in_store, ph.id as id_photo')->fromProducts()
|
|
->leftJoin('p', 'photos_products_relation', 'ppr', 'ppr.id_product=p.id AND ppr.show_in_lead="Y"')
|
|
->leftJoin('ppr', 'photos', 'ph', 'ph.id=ppr.id_photo')
|
|
->where(Operator::inIntArray(array_column($upsell, 'id_product'), 'p.id'))
|
|
->groupBy('p.id');
|
|
if (findModule(\Modules::PRODUCTS_VARIATIONS)) {
|
|
$code = findModule(\Modules::PRODUCTS_VARIATIONS, \Modules::SUB_CODE) ? "COALESCE(pv.code, '')" : "''";
|
|
$field = "GROUP_CONCAT(CONCAT(pv.id, '~', pv.title, '~', {$code}, '~', pv.in_store) SEPARATOR '|') variations";
|
|
$upsell_products->joinVariationsOnProducts()->addSelect($field);
|
|
}
|
|
$upsell_products = sqlFetchAll($upsell_products, 'id');
|
|
|
|
foreach ($upsell as $gift) {
|
|
$id_product = $gift['id_product'];
|
|
if ($upsell_product = $upsell_products[$id_product] ?? null) {
|
|
$upsell_product['id_product'] = $id_product;
|
|
$upsell_product['image'] = getImage($upsell_product['id_photo'], null, null, 4);
|
|
if ($upsell_product['variations']) {
|
|
$selected = $gift['id_variation'] ?? [];
|
|
$variations = explode('|', $upsell_product['variations']);
|
|
foreach ($variations as &$variation) {
|
|
$variation = explode('~', $variation);
|
|
$variation = array_combine(['value', 'label', 'code', 'in_store'], $variation);
|
|
$variation['selected'] = in_array($variation['value'], $selected);
|
|
}
|
|
$upsell_product['variations'] = $variations;
|
|
}
|
|
$vars['upsell'][] = $upsell_product;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $vars;
|
|
}
|
|
|
|
public function handleData($data)
|
|
{
|
|
$data = parent::handleData($data);
|
|
|
|
$upsell = [];
|
|
foreach ($data['upsell'] ?? [] as $upsell_item) {
|
|
if (($upsell_item['delete'] ?? null) == 'on') {
|
|
continue;
|
|
}
|
|
$upsell[] = $upsell_item;
|
|
}
|
|
$data['upsell'] = $upsell;
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function applyResult(PurchaseState &$purchaseState, OrderDiscount $orderDiscount, array $data)
|
|
{
|
|
}
|
|
|
|
public function getFrontendHandler(): ?HandlerInterface
|
|
{
|
|
return $this->upsellHandler;
|
|
}
|
|
}
|