从可配置的购物车中获取简单的产品 [英] Get Simple Product from Configurable in Cart
问题描述
我正在尝试加载已添加到客户购物车中的简单产品,但是当我检索这些项目时,它会显示可配置的父项.
I'm trying to load the simple products that have been added to a customer's cart, but when I retrieve the items, it's showing the parent configurable.
$cart = Mage::getSingleton('checkout/cart');
$productIds = array();
foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
$productIds[] = $item->getProduct()->getId();
}
var_dump($productIds);
例如,当我在购物车中添加了小号,中号和大号时,它将返回所有相同的可配置ID.如何获得单个简单产品?我正在尝试检索在简单产品级别设置的属性值.
For instance, this will return all the same configurable id when I've added a small, medium, and large to my cart. How can I get the individual simple products? I'm trying to retrieve an attribute value that's set on the simple product level.
推荐答案
查看了Magento如何在结帐/购物车页面上渲染购物车中的商品后,我可以在app/code/core/Mage/Checkout/Block/Cart/Item/Renderer/Configurable.php
After taking a look at how Magento renders the items in your cart on the checkout/cart page, I was able to find this in app/code/core/Mage/Checkout/Block/Cart/Item/Renderer/Configurable.php
/**
* Get item configurable child product
*
* @return Mage_Catalog_Model_Product
*/
public function getChildProduct()
{
if ($option = $this->getItem()->getOptionByCode('simple_product')) {
return $option->getProduct();
}
return $this->getProduct();
}
因此,将其应用于问题中的代码段,
So, applying it to the snippet in the question, it would be
foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
$productId = $item->getProduct()->getId();
if ($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
}
$productIds[] = $productId;
}
这篇关于从可配置的购物车中获取简单的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!