Skip to content
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

Remove aroundAddProduct in QuotePlugin #1246

Closed
BrocksiNet opened this issue Jan 4, 2019 · 3 comments
Closed

Remove aroundAddProduct in QuotePlugin #1246

BrocksiNet opened this issue Jan 4, 2019 · 3 comments
Assignees

Comments

@BrocksiNet
Copy link

Hello Guys,

normally we should try to avoid around plugins (at least this is written in magento2 docu), because of that i changed the class of the quote plugin on my local project. I just leave the changed content here because maybe you want to merge it into your module.

Path to file
src/vendor/smile/elasticsuite/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php

Changed Content

class QuotePlugin
{
    /**
     * @var \Smile\ElasticsuiteTracker\Api\EventQueueInterface
     */
    private $eventQueue;

    /**
     * @var \Magento\Framework\Stdlib\CookieManagerInterface
     */
    private $cookieManager;

    /**
     * @var \Smile\ElasticsuiteTracker\Helper\Data
     */
    private $trackerHelper;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    /**
     * Constructor.
     *
     * @param \Smile\ElasticsuiteTracker\Api\EventQueueInterface $eventQueue Tracker event queue.
     * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager Cookie manager.
     * @param \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper Tracker helper.
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        \Smile\ElasticsuiteTracker\Api\EventQueueInterface $eventQueue,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->eventQueue    = $eventQueue;
        $this->cookieManager = $cookieManager;
        $this->trackerHelper = $trackerHelper;
        $this->logger = $logger;
    }

    /**
     * Log add to cart events into the event queue.
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     *
     * @param Quote $subject Quote.
     * @param $result
     * @return \Magento\Quote\Model\Quote\Item|string
     */
    public function afterAddProduct(
        Quote $subject,
        $result
    ) {
        try {
            if ($result instanceof QuoteItem) {
                /** @var \Magento\Quote\Model\Quote\Item $result */
                $product = $result->getProduct();
                if ($product !== null) {
                    $this->logEvent($product->getId(), $product->getStoreId());
                }
            }
        } catch ( \Exception $e){
            $this->logger->error($e->getMessage(), []);
        }

        return $result;
    }

    /**
     * Log the event.
     *
     * @param int $productId
     * @param int $storeId
     * @return void
     */
    private function logEvent(int $productId, int $storeId): void
    {
        $pageData = [];
        $pageData['store_id']           = $storeId;
        $pageData['cart']['product_id'] = $productId;

        $eventData = ['page' => $pageData, 'session' => $this->getSessionData()];

        $this->eventQueue->addEvent($eventData);
    }

    /**
     * Read session data.
     *
     * @return string[]
     */
    private function getSessionData(): array
    {
        $cookieConfig = $this->trackerHelper->getCookieConfig();

        $sessionData = [
            'uid' => $this->readCookieValue($cookieConfig['visit_cookie_name']),
            'vid' => $this->readCookieValue($cookieConfig['visitor_cookie_name']),
        ];

        return $sessionData;
    }

    /**
     * Read cookie value.
     *
     * @param string $cookieName Cookie name.
     *
     * @return string|NULL
     */
    private function readCookieValue($cookieName): ?string
    {
        return $this->cookieManager->getCookie($cookieName);
    }
}

Cheers björn

PS: i tested it local and it was working like before.

@romainruaud
Copy link
Collaborator

Hello @bjoern-flagbit

thank you for contributing, always appreciated !

would you mind submitting a PR containing this change ? I'd gladly merge it.

Regards

@romainruaud
Copy link
Collaborator

Done in #1265.

Will be part of next minor release.

Thanks

@BrocksiNet
Copy link
Author

Thank you 👍 Next time will do a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants