I'm suggesting you to create a plugin
<type name="\Magento\Checkout\Controller\Index\Index">
<plugin name="before-checkout-index" type="\Cpy\Checkout\Plugin\Controller\Checkout\Index" sortOrder="1" disabled="false"/>
</type>
Then in that plugin you can call you helper
Something like that
/**
* @param $subject
* @param $proceed
* @return mixed
*/
public function aroundExecute($subject, $proceed)
{
$errors = $this->addToCartHelper->checkCart();
if(is_array($errors)) {
foreach($errors as $error) {
$this->messageManager->addErrorMessage($error);
}
return $this->redirectFactory->create()->setPath('checkout/cart');
} else {
return $proceed();
}
}
Where addToCartHelper
is your custom helper and checkCart()
the method that will return you all the errors messages depending on your logic
On the helper, you can reach all the items like below
public function checkCart()
{
$errors = [];
$items = $this->_checkoutSession->getQuote()->getAllVisibleItems();
// ...
if(!count($errors)) {
return true;
} else {
return $errors;
}
}