I have tuned the answer that you did find faulty. The change is minor but hopefully it resolve your issue.
public function aroundExecute(
\Magento\Catalog\Controller\Product\Compare\Add $subject,
callable $proceed
) {
$count = $this->helper->getItemCount();
if ($count >= self::$LIMIT_TO_COMPARE_PRODUCTS) {
$this->messageManager->addErrorMessage(
sprintf('Only up to %s items can be added to the comparison list', self::$LIMIT_TO_COMPARE_PRODUCTS)
);
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setRefererOrBaseUrl();
}
return $proceed();
}
if($count > self::LIMIT_TO_COMPARE_PRODUCTS) { $this->messageManager->addErrorMessage( 'You can add the compared products under 3 item(s)' );