Come posso ottenere il prodotto semplice Qtà nel carrello?
-
12-12-2019 - |
Domanda
Sto cercando di ottenere la quantità di prodotti semplici nel carrello, ma restituisce solo la quantità configurabile.
Come posso ottenere il prodotto semplice Qtà nel carrello?
Sto usando questo:
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getQuote();
foreach ($cart->getItemsCollection() as $item) {
if($item->getProduct()->getData('type_id') == 'simple'){
$theqty = $this->escapeHtml($item->getQty());
}
}
. Soluzione
Qui, questo dovrebbe fare ciò di cui hai bisogno, non è necessario accedere all'oggetto prodotto, ed è solo il prezzo necessario dalla linea di prodotti configurabile nella quotazione.
$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$valor = [];
foreach ($quote->getAllItems() as $item) {
if ('simple' != $item->getProductType()) continue;
$valor[]= array (
'id' => $item->getSku(),
'quantity' => $item->getQty(),
'price' => $item->getParentItemId() ? $item->getParentItem()->getPrice() : $item->getPrice()
);
}
$valor_json = json_encode($valor);
. Altri suggerimenti
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getQuote();
$ids = array();
foreach ($cart->getAllItems() as $item) {
$sku = $item->getProduct()->getSku();
$preco = $item->getProduct()->getPrice();
$theqty = $this->escapeHtml($item->getQty());
if($item->getProduct()->getData('type_id') == 'configurable'){
$sku = Mage::getModel('catalog/product')->load($item->getProductId());
$valor .= '{ id: "'.$sku->getSku().'", price:'.$preco.', quantity: '.$theqty .'},';
$sku = $item->getProduct()->getSku();
}elseif($item->getProduct()->getData('type_id') == 'simple'){
if (!(in_array($sku, $ids))) {
$valor .= '{ id: "'.$sku.'", price:'.$preco.', quantity: '.$theqty .'},';
}
}
array_push($ids, $sku);
}
. You can get product quantity without looping all items:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$product = Mage::getModel('catalog/product')->load($product_id);
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();
use $cart->getAllItems()
to get the simple items too.
You can get product quantity without looping all items:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange