0

Magento version: 2.0.0

I added a product into cart, and then print the $_SESSION:

[checkout] => Array
        (
            [last_added_product_id] => 5
        )

    [message] => Array
        (
            [default] => Magento\Framework\Message\Collection Object
                (
                    [messages:protected] => Array
                        (
                            [success] => Array
                                (
                                    [0] => Magento\Framework\Message\Success Object
                                        (
                                            [text:protected] => Magento\Framework\Phrase Object
                                                (
                                                    [text:Magento\Framework\Phrase:private] => You added %1 to your shopping cart.
                                                    [arguments:Magento\Framework\Phrase:private] => Array
                                                        (
                                                            [0] => Demo Product
                                                        )

                                                )

                                            [identifier:protected] => default_message_identifier
                                            [isSticky:protected] => 
                                            [data:protected] => 
                                        )

                                )

                        )

                    [lastAddedMessage:protected] => Magento\Framework\Message\Success Object
                        (
                            [text:protected] => Magento\Framework\Phrase Object
                                (
                                    [text:Magento\Framework\Phrase:private] => You added %1 to your shopping cart.
                                    [arguments:Magento\Framework\Phrase:private] => Array
                                        (
                                            [0] => Demo Product
                                        )

                                )

                            [identifier:protected] => default_message_identifier
                            [isSticky:protected] => 
                            [data:protected] => 
                        )

                )

        )

Add to Cart Form Code:

<?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                        <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
                                                            <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
                                                            <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                            <input type="hidden" name="form_key" value="<?php echo $block->getFormKey(); ?>" />
                                                            <?php echo $block->getBlockHtml('formkey')?>
                                                            <button type="submit"
                                                                    title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                                    class="action tocart primary">
                                                                <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                            </button>
                                                        </form>

I checked the Magento\Checkout\Block\Cart:

/**
 * @codeCoverageIgnore
 * @return int
 */
public function getItemsCount()
{
//      echo '<pre>';
//      print_r($_SESSION);
//      echo '</pre>';
//      exit();
    return $this->getQuote()->getItemsCount();
}

I open the cart page is empty and found this $this->getQuote()->getItemsCount() is still empty.

So, I don't know where is wrong.

1 Answer 1

0

The getItemsCount method is defined under Magento/Quote/Model/Quote:

public function getItemsCount()
{
    return $this->_getData(self::KEY_ITEMS_COUNT);
}

It retrieves the data of the quote based on the constant const KEY_ITEMS_COUNT = 'items_count'; of the quote class.

3
  • yes, i have found this function. i have added the product into cart and the product data is added into $_SESSION. i don't know why it is still empty.
    – Mike Zhang
    Commented Mar 25, 2016 at 8:43
  • @MikeZhang well that's not exactly the question you asked. Create a new question with the code you used to add the product to cart and we'll be able to help you. Commented Mar 25, 2016 at 8:45
  • I have updated this question, could check it again, thanks.
    – Mike Zhang
    Commented Mar 25, 2016 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.