Skip to content

PR by Loberon: support promotions and vouchers #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: b-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ protected function modifyByThankYouController($order, $user, $basket)
}

$this->activePageEntity->setBoughtProducts($basketProducts);

// Promotion - Benutzerdefinierte dimensionen
foreach ($basket->getVouchers() as $voucherId => $voucher) {
$this->activePageEntity->addPromotion(
$voucherId,
$voucher->sVoucherNr,
$voucher->dVoucherdiscount
);
}
}

/**
Expand Down
33 changes: 33 additions & 0 deletions Component/Tracking/ActivePageEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class ActivePageEntity implements ActivePageEntityInterface
*/
private $email = '';

/**
* @var array
*/
private $promotions = null;

/**
* @return string
*/
Expand Down Expand Up @@ -469,4 +474,32 @@ public function getEmail()
{
return $this->email;
}

/**
* @return array
*/
public function getPromotions()
{
return $this->promotions;
}

/**
* @param string $voucherId oxvoucher.oxid
* @param string $voucherCode Eingelöster Gutscheincode
* @param float $discount Eingelöster Gutscheinwert
*
* @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html
*/
public function addPromotion($voucherId, $voucherCode, $discount)
{
if ($this->promotions === null) {
$this->promotions = [];
}

$this->promotions[] = [
'voucherId' => $voucherId,
'voucherCode' => $voucherCode,
'discount' => $discount
];
}
}
14 changes: 14 additions & 0 deletions Component/Tracking/ActivePageEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,18 @@ public function setEmail($email);
* @return string
*/
public function getEmail();

/**
* @return array
*/
public function getPromotions();

/**
* @param string $voucherId oxvoucher.oxid
* @param string $voucherCode Eingelöster Gutscheincode
* @param float $discount Eingelöster Gutscheinwert
*
* @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html
*/
public function addPromotion($voucherId, $voucherCode, $discount);
}
105 changes: 105 additions & 0 deletions Component/Tracking/SdkExtension/Promotions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Created by LoberonEE.
* Autor: Tobias Matthaiou <tm@loberon.de>
* Date: 10.12.19
* Time: 17:09
*/

namespace OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension;

use Econda\Tracking\TrackingItemInterface;
use Econda\Util\BaseObject;
use OxidEsales\EshopCommunity\Internal\Application\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Common\Database\QueryBuilderFactoryInterface;

/**
* Class Promotions
* @package OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension
* @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html
*/
class Promotions extends BaseObject implements TrackingItemInterface
{
/**
* @var array
*/
protected $data = [];

/**
* @inheritDoc
* @return array
*/
public function getTrackingData()
{
$data = array_map([$this, 'convert'], $this->data);

return [
'promotions' => $data
];
}

/**
* @param array $promotion
*/
protected function convert($promotion)
{
$title = $this->getVoucherserieTitle($promotion['voucherId']);

return $this->econdaFormat(
$title,
$promotion['voucherCode'],
$promotion['discount']
);
}

/**
* @param string(64) $action Gutschein- oder Marketingaktion
* @param string(64) $code Eingelöster Gutscheincode
* @param float $wert Eingelöster Gutscheinwert
*/
protected function econdaFormat($action, $code, $wert)
{
$action = $this->truncate($action, 64);
$code = $this->truncate($code, 64);

return [$action, $code, $wert];
}

/**
* @param string $string
* @param integer $length
* @return string
*/
protected function truncate($string, $length)
{
if (strlen($string) > $length) {
$string = substr($string, 0, $length - 3) . '...';
}

return $string;
}

/**
* @param string $voucherId OXID of Voucher
* @return string
*/
protected function getVoucherserieTitle($voucherId)
{
$container = ContainerFactory::getInstance()->getContainer();

$qb = $container->get(QueryBuilderFactoryInterface::class)->create()
->select('oxserienr')
->from(getViewName('oxvoucherseries'), 'vs')
->join('vs', 'oxvouchers', 'vo', 'vo.OXVOUCHERSERIEID = vs.OXID')
->where('vo.OXID = :oxid')
->setParameter('oxid', $voucherId);

$name = $qb->execute()->fetchColumn(0);

if (empty($name)) {
return 'Voucher name no longer exists in the database';
}

return $name;
}
}
12 changes: 12 additions & 0 deletions Component/Tracking/TrackingCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Econda\Tracking\TransactionProduct;
use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\Email;
use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\PageView;
use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\Promotions;

/**
* Class responsible for generating Econda tracking script.
Expand Down Expand Up @@ -51,6 +52,7 @@ public function generateCode()
$this->addCurrentOrderProcessToPageView();
$this->addUserRegistrationDataToPageView();
$this->addTransactionDataToPageView();
$this->addPromotionsDataToPageView();
$this->addSearchQueryDataToPageView();
$this->addLoginUserDataToPageView();
$this->addCartDataToPageView();
Expand Down Expand Up @@ -101,6 +103,16 @@ private function addTransactionDataToPageView()
}
}

/**
* @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html
*/
private function addPromotionsDataToPageView()
{
if ($this->activePageEntity->getPromotions()) {
$this->pageView->add(new Promotions(['data' => $this->activePageEntity->getPromotions()]));
}
}

private function addCurrentProductDataToPageView()
{
if (!empty($this->activePageEntity->getProductData() && empty($this->activePageEntity->getProductToBasket()))) {
Expand Down