In my website, some products are not for sale in California. While the user check开发者_开发技巧outs, I have to do a validation like if the cart has items not for sale in California and user's shipping address is in CAlifornia, prevent user from checking out.
The sale in CA is set from admin side using an 'avl' attribute.
here is the code I use to iterate through the cart and check for the attribute
.....................
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $_item){
$_product = $_item->getProduct();
//If atleast one product has availability status set to California, then set the flag and break.
if($_product->getResource()->getAttribute('avl')->getFrontend()->getValue($_product) == 'NC'){
$flag = true;
break;
}
......................
Well, now the problem is I'm not able to get the 'avl' value as set from the Admin side.
This code is in local//Checkout/Block/Onepage/Shipping.php
Any idea about how to retrieve the attribute value?
Thanks in advance.
To get a product's attributes during checkout, the easiest way is to just completely load the product:
$_product = Mage::getModel('catalog/product')->load($_item->getProduct());
$_avl = $_product->getAvl();
There are of course overheads with loading a product but this is the fastest way to get an attribute value from a cart item.
<?php $_item = $this->getItem()?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId())?>
<?php echo $_product->getResource()->getAttribute('attribute_code')
->getFrontend()->getValue($_product); ?>
app/design/frontend/your_default/your_default/template/checkout/cart/item/default.phtml
精彩评论