2023-08-03 14:57:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace D3\GoogleAnalytics4\Modules\Application\Controller;
|
|
|
|
|
|
|
|
use OxidEsales\Eshop\Application\Component\BasketComponent;
|
|
|
|
use OxidEsales\Eshop\Application\Model\Article;
|
|
|
|
use OxidEsales\Eshop\Application\Model\ArticleList;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use oxSystemComponentException;
|
|
|
|
|
|
|
|
class BasketController extends BasketController_parent
|
|
|
|
{
|
|
|
|
/**
|
2024-01-17 15:36:43 +01:00
|
|
|
* @return string
|
2023-08-03 14:57:26 +02:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$return = parent::render();
|
|
|
|
|
|
|
|
$this->d3GA4getRemovedArticlesListObject();
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @throws oxSystemComponentException
|
|
|
|
*/
|
|
|
|
public function d3GA4getRemovedArticlesListObject() :void
|
|
|
|
{
|
|
|
|
$this->addTplParam('hasBeenReloaded', false);
|
|
|
|
// collecting items to add
|
|
|
|
$aProducts = Registry::getRequest()->getRequestEscapedParameter('aproducts');
|
|
|
|
|
|
|
|
// collecting specified item
|
2024-01-17 15:36:43 +01:00
|
|
|
$sProductId = Registry::getRequest()->getRequestEscapedParameter('aid');
|
2023-08-03 14:57:26 +02:00
|
|
|
if ($sProductId) {
|
|
|
|
// additionally fetching current product info
|
2024-01-17 15:36:43 +01:00
|
|
|
$dAmount = Registry::getRequest()->getRequestEscapedParameter('am');
|
2023-08-03 14:57:26 +02:00
|
|
|
|
|
|
|
// select lists
|
2024-01-17 15:36:43 +01:00
|
|
|
$aSel = Registry::getRequest()->getRequestEscapedParameter('sel');
|
2023-08-03 14:57:26 +02:00
|
|
|
|
2024-01-17 15:36:43 +01:00
|
|
|
/** @var BasketComponent $oBasketComponent */
|
|
|
|
$oBasketComponent = $this->getComponent('oxcmp_basket');
|
2023-08-03 14:57:26 +02:00
|
|
|
|
2024-01-17 15:36:43 +01:00
|
|
|
$aPersParam = $oBasketComponent->__call('getPersistedParameters', []);
|
2023-08-03 14:57:26 +02:00
|
|
|
|
|
|
|
$sBasketItemId = Registry::getRequest()->getRequestEscapedParameter('bindex');
|
|
|
|
|
2023-08-18 14:07:53 +02:00
|
|
|
$aProducts[$sProductId] = [
|
|
|
|
'am' => $dAmount,
|
2023-08-03 14:57:26 +02:00
|
|
|
'sel' => $aSel,
|
|
|
|
'persparam' => $aPersParam,
|
|
|
|
'basketitemid' => $sBasketItemId
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($aProducts) && count($aProducts)) {
|
|
|
|
$toRemoveArticleIdList = [];
|
|
|
|
$artIdOnArtAmountList = [];
|
|
|
|
|
2023-08-18 14:07:53 +02:00
|
|
|
if ($this->isArticleRemovedState($aProducts)) {
|
2023-08-03 14:57:26 +02:00
|
|
|
//setting amount to 0 if removing article from basket
|
|
|
|
foreach ($aProducts as $sProductId => $aProduct) {
|
|
|
|
if ((isset($aProduct['remove']) && $aProduct['remove']) or intval($aProduct['am']) === 0) {
|
|
|
|
if (!in_array($aProduct, $toRemoveArticleIdList)) {
|
|
|
|
$toRemoveArticleIdList[] = $aProduct['aid'];
|
|
|
|
$artIdOnArtAmountList[$aProduct['aid']] = $aProduct['am'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$aProducts[$sProductId]['am'] = 0;
|
|
|
|
#for GA4 Event
|
|
|
|
$this->addTplParam('hasBeenReloaded', true);
|
|
|
|
} else {
|
|
|
|
unset($aProducts[$sProductId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$oArtList = oxNew(ArticleList::class);
|
|
|
|
$oArtList->loadIds($toRemoveArticleIdList);
|
|
|
|
|
|
|
|
#dumpVar($this->getBasketArticles());
|
|
|
|
|
|
|
|
/** @var Article $item */
|
|
|
|
foreach ($oArtList->getArray() as $item){
|
|
|
|
foreach ($artIdOnArtAmountList as $artId => $artAmount){
|
|
|
|
if ($item->getId() === $artId){
|
|
|
|
$item->assign(['d3AmountThatGotRemoved' => $artAmount]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addTplParam('toRemoveArticles', $oArtList);
|
|
|
|
}
|
|
|
|
}
|
2023-08-18 14:07:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* checks, if we're in the short state of "an Article has been removed"
|
|
|
|
* We check by looking for the "'removeBtn' is not null", as a sign for it has been triggered/ clicked
|
|
|
|
* if that doesn't work, we check if there's an Article in the Products array, that has "'am' = 0"
|
|
|
|
* Which also shows we're in that state rn
|
|
|
|
*/
|
|
|
|
protected function isArticleRemovedState(array $productsArray) :bool
|
|
|
|
{
|
|
|
|
if (Registry::getRequest()->getRequestEscapedParameter('removeBtn')
|
|
|
|
or Registry::getRequest()->getRequestEscapedParameter('updateBtn')
|
|
|
|
){
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
foreach ($productsArray as $aProduct) {
|
|
|
|
if (intval($aProduct['am']) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-22 11:13:05 +01:00
|
|
|
}
|