I calling a button proceed to checkout on title page shopping cart. but when cart empty, button still show. how to check if cart empty and hide button proceed to checkout? Please help me!
1 Answer
You can use the cart helper to check if shopping cart is empty.
protected $cartHelper;
public function __construct(
\Magento\Checkout\Helper\Cart $cartHelper
) {
$this->cartHelper = $cartHelper;
}
and then you can add your logic if the cart is empty
if ($this->cartHelper->getItemsCount() === 0) {
//add your logic
}
-
I see protected $_cartHelper; in vendor/magento/module-checkout/Block/Cart.php but I try running if ($this->_cartHelper->getItemsCount() === 0){...} body show only div page-title-wrapper and nothing else. :(– LinhCommented Jun 21, 2018 at 7:30
-
Here $cartHelper is a variable defined for the helper class. you need to check for getItemsCount() function in vendor/magento/module-checkout/Helper/Cart.php $this->cartHelper->getItemsCount() // This will returns your cart count Commented Jun 21, 2018 at 10:03
-
oh. thankyou!. I'm trying other code: if ($block->getItemsCount() != 0) { .................my code..... } and It worked well.– LinhCommented Jun 27, 2018 at 1:54