Skip to content

Commit

Permalink
Merge pull request #5330 in SW/shopware from sw-19240/5.3/add-mail-te…
Browse files Browse the repository at this point in the history
…mplate-variables-image-and-rating to 5.3

* commit '1373b89d2be799ce25542c4ac2b6127931f97169':
  SW-19240 - Use different shop context depending on shop referenced in order
  SW-19240 - Add mail template variables for product images and link to rating tab
  • Loading branch information
janbuecker committed Aug 31, 2017
2 parents 06ea108 + 1373b89 commit 0094ceb
Showing 1 changed file with 133 additions and 6 deletions.
139 changes: 133 additions & 6 deletions engine/Shopware/Plugins/Default/Core/CronRating/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* our trademarks remain entirely with us.
*/

use Shopware\Models\Shop\Shop;

/**
* Shopware Cron for article ratings
*/
Expand Down Expand Up @@ -63,11 +65,15 @@ public function onRun(Enlight_Components_Cron_EventArgs $job)

$orderIds = array_keys($orders);
$customers = $this->getCustomers($orderIds);
$positions = $this->getPositions($orderIds);
$orderPositions = $this->getPositions($orderIds);

$shopPositionBaseProducts = $this->structurePositionsArray($orderPositions);

$shopPositionImages = $this->getPositionImages($shopPositionBaseProducts);

$count = 0;
foreach ($orders as $orderId => $order) {
if (empty($customers[$orderId]['email']) || count($positions[$orderId]) === 0) {
if (empty($customers[$orderId]['email']) || empty($orderPositions[$orderId])) {
continue;
}

Expand All @@ -82,17 +88,41 @@ public function onRun(Enlight_Components_Cron_EventArgs $job)
$shop->setCurrency($repository->find($order['currencyID']));
$shop->registerResources();

foreach ($positions[$orderId] as &$position) {
$position['link'] = Shopware()->Container()->get('router')->assemble([
foreach ($orderPositions[$orderId] as &$position) {
$position['link'] = $this->get('router')->assemble([
'module' => 'frontend', 'sViewport' => 'detail',
'sArticle' => $position['articleID'],
]);

$position['link_rating_tab'] = $this->get('router')->assemble([
'module' => 'frontend', 'sViewport' => 'detail',
'sArticle' => $position['articleID'],
'action' => 'rating',
]);

if (!isset($shopPositionImages[$shopId][$position['articleordernumber']]['source'])) {
continue;
}

$position['image_original']
= $position['image_small']
= $position['image_large']
= $shopPositionImages[$shopId][$position['articleordernumber']]['source'];

if (!isset($shopPositionImages[$shopId][$position['articleordernumber']]['thumbnails'])) {
continue;
}

$thumbnails = $shopPositionImages[$shopId][$position['articleordernumber']]['thumbnails'];

$position['image_small'] = isset($thumbnails[0]) ? $thumbnails[0]['source'] : $position['image_original'];
$position['image_large'] = isset($thumbnails[1]) ? $thumbnails[1]['source'] : $position['image_original'];
}

$context = [
'sOrder' => $order,
'sUser' => $customers[$orderId],
'sArticles' => $positions[$orderId],
'sArticles' => $orderPositions[$orderId],
];

$mail = Shopware()->TemplateMail()->createMail('sARTICLECOMMENT', $context);
Expand All @@ -108,6 +138,34 @@ public function onRun(Enlight_Components_Cron_EventArgs $job)
return $count . ' rating mail(s) sent.';
}

/**
* @param array $positions
*
* @return array
*/
public function getPositionImages($shopPositions)
{
$shopPositionImages = [];

foreach ($shopPositions as $shopId => $positions) {
$context = $this->get('shopware_storefront.context_service')->createShopContext($shopId);

$shopPositionImages[$shopId] = $this->get('shopware_storefront.media_service')->getCovers(
$positions,
$context
);

$shopPositionImages[$shopId] = array_map(
function ($mediaStruct) {
return $this->get('legacy_struct_converter')->convertMediaStruct($mediaStruct);
},
$shopPositionImages[$shopId]
);
}

return $shopPositionImages;
}

/**
* @param $sendTime
*
Expand Down Expand Up @@ -288,12 +346,16 @@ public function getPositions($orderIds)
d.esdarticle,
d.taxID,
t.tax,
d.esdarticle as esd
d.esdarticle as esd,
o.subshopID as subshopID,
o.language as language
FROM s_order_details as d
LEFT JOIN s_core_tax as t
ON t.id = d.taxID
LEFT JOIN s_articles_details ad
ON d.articleordernumber = ad.ordernumber
LEFT JOIN s_order o
ON d.orderID = o.id
WHERE d.orderID IN ($orderIds)
AND ad.active = 1
AND d.modus = 0
Expand All @@ -307,4 +369,69 @@ public function getPositions($orderIds)

return $rows;
}

/**
* Transforms the array from order => positions to shop => positions
*
* Input:
*
* [
* [order_1] => [
* position_a,
* position_b,
* ...
* ],
* [order_2] => [
* position_c,
* position_d,
* ...
* ], ...
* ]
*
* Output:
*
* [
* [shop_1] => [
* position_a,
* position_b,
* ...
* ],
* [shop_2] => [
* position_c,
* position_d
* ]
* ]
*
* by using the corresponding shopId for every order's positions.
*
* @param $orderPositions
*
* @return array
*/
private function structurePositionsArray($orderPositions)
{
$shopPositionNumbers = [];

foreach ($orderPositions as $order_id => $positions) {
$firstPosition = $positions[array_keys($positions)[0]];
$shopId = is_numeric($firstPosition['language']) ? $firstPosition['language'] : $firstPosition['subshopID'];

if (!is_array($shopPositionNumbers[$shopId])) {
$shopPositionNumbers[$shopId] = [];
}

$shopPositionNumbers[$shopId] = array_merge(
array_column($positions, 'articleordernumber'),
$shopPositionNumbers[$shopId]);
}

$shopPositionBaseProducts = [];
$baseProductFactory = $this->get('shopware_storefront.base_product_factory');

foreach ($shopPositionNumbers as $shopId => $shopPositions) {
$shopPositionBaseProducts[$shopId] = $baseProductFactory->createBaseProducts($shopPositions);
}

return $shopPositionBaseProducts;
}
}

0 comments on commit 0094ceb

Please sign in to comment.