答案很大程度上取决于您想要如何处理父/子类型产品(如果需要)。
如果您只处理简单的产品或者您有父/子类型的产品并且您需要测试子 ID,那么:
$productId = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote();
if (! $quote->hasProductId($productId)) {
// Product is not in the shopping cart so
// go head and show the popup.
}
或者,如果您正在处理父/子类型产品并且只想测试父 ID,则:
$productId = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$foundInCart = false;
foreach($quote->getAllVisibleItems() as $item) {
if ($item->getData('product_id') == $productId) {
$foundInCart = true;
break;
}
}
EDIT
在评论中提出了为什么要设置注册表值的问题controller_action_predispatch_checkout_cart_add
无法在 cart.phtml 中检索。
本质上,注册表值仅在单个请求的生命周期内可用 - 您发布到 checkout/cart/add,然后被重定向到 checkout/cart/index - 因此您的注册表值会丢失。
如果您想在这些之间保留一个值,那么您可以使用会话:
在你的观察者中:
Mage::getSingleton('core/session')->setData('your_var', 'your_value');
检索值
$yourVar = Mage::getSingleton('core/session')->getData('your_var', true);
传递给 getData 的 true 标志将为您从会话中删除该值。