장바구니에서 제품 간단한 수량을 어떻게 얻을 수 있습니까?
-
12-12-2019 - |
문제
카트에서 간단한 제품을 얻으려고하지만 구성 가능한 수량 만 반환합니다.
장바구니에서 제품 간단한 수량을 어떻게 얻을 수 있습니까?
이 (가) 사용하고 있습니다 :
$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());
}
}
. 해결책
여기에서는 필요한 것을 수행해야합니다. 제품 개체에 액세스 할 필요가 없으며, 인용문의 구성 가능한 제품 라인에서 필요한 가격 만 제공됩니다.
$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);
. 다른 팁
$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();
제휴하지 않습니다 magento.stackexchange