Magento 购物车项目检查

Magento cart items check

如何检查是否所有购物车商品都具有我的自定义属性?如果 item/items 都具有自定义属性,则打印一条消息,但如果其中一个购物车 item/items 没有自定义属性,它会在结帐时打印一条错误消息?

请尝试以下代码

<?php
$productModel = Mage::getModel('catalog/product');
$cart = Mage::getModel('checkout/cart')->getQuote();
$error = "";
foreach ($cart->getAllItems() as $item) {
    $product = $productModel->load($item->getProduct()->getId());
    if($product->getData('your_attribute_code')){
        $message = "your message for items have attribute";
        $error = 0;
    }else{
        $message = "your message for items have no attributes";
        $error = 1;
        break;
    }
}
if($error == 1){
    Mage::getSingleton('core/session')->addError($message);
}else{
    Mage::getSingleton('core/session')->addSuccess($message);   
}

?>