2

I am using below code for fetching the ordered quantity for products, the value is correctly fetched for simple product. but in case of configurable product the quantity remains 1.

foreach ($quote->getAllItems() as $item){
        $buyrequest = $item->getBuyRequest();
        $qty = $buyrequest->getQty();
}

i even tried $item->getQtyOrdered().

2 Answers 2

2

To get the items from a quote I would suggest using getAllVisibleItems then you can simply call getQty() on each item.

$quote = Mage::getModel('sales/quote')->load(68);
foreach ($quote->getAllVisibleItems() as $item){
    $qty = $item->getQty();
}

The reason for using getAllVisibleItems is that this deals with parent products and so will only give you the items that would show to the user.

1

When configurable product is being added to the quote it is presented by 2 quote items: 1 for product itself and 1 for its option (simple product). So if you want to get ordered quantity you need to do it like this.

foreach ($quote->getAllVisibleItems() as $item){
    if ($item->getHasChildren()) {
        $qty = 0;
        foreach ($item->getChildren() as $child) {
            $qty += $child->getQty();
        }
    } else {
        $qty = $item->getQty();
    }
}

$item->getHasChildren() means the case I described in the start of answer. So in that case you need to get only children qtys

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.