简体   繁体   中英

Magento cart items check

How can I check if all the cart items have my custom attribute? And if item/items all have the custom attribute prints a message but if one of the cart item/items doesn't have the custom attribute, it prints an error message in the checkout?

Please try below code

<?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);   
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM