eCommerce: Check if at least 1 item in cart has a certain attribute value?

Permalink
Hi,

I'm working on a site where there are different types of products. The products have a "product_type" attribute. On the checkout page, I want to show something only if at least one product in the cart has a product_type attribute value of "event".

How can I do this please?

Thanks
Dave

madesimplemedia
 
madesimplemedia replied on at Permalink Reply
madesimplemedia
Anyone know about this please?
hutman replied on at Permalink Reply
hutman
I have not tested this code, but you should be able to do something like this:

Loader::model('product/model', 'core_commerce');
Loader::model('cart', 'core_commerce');
$hasProductsOfType = false;
if (!isset($cart)) {
   $cart = CoreCommerceCart::get();
}
$items = $cart->getProducts(); 
foreach($items as $it) {
   if($it->getAttribute('product_type') == 'event'){
      $hasProductsOfType = true;
      break;
   }
}
if($hasProductsOfType) {
   // do things, there are event products
madesimplemedia replied on at Permalink Reply
madesimplemedia
Hi, thanks for this.

I'm not getting any output. Using ChromePHP, if I do some logging, I get null for the attributes for the 2 products in my cart:

foreach($items as $it) {
         ChromePhp::log($it->getAttribute('product_type'));
         if($it->getAttribute('product_type') == 'event'){
           $hasProductsOfType = true;
           break;
         }
  }


Doesn't seem to be getting the attribute value??
hutman replied on at Permalink Reply
hutman
Can you tell me where you are placing this code?
Can you include a screenshot of the product attribute setup?
madesimplemedia replied on at Permalink Reply 1 Attachment
madesimplemedia
Please see screenshot attached.

I'm adding the code in the single page in the one page checkout addon:
/packages/one_page_checkout/single_pages/checkout/onepagecheckout.php

Thanks
hutman replied on at Permalink Best Answer Reply
hutman
I think this should work for you, I always forget that OrderProduct is not the same as Product from a Model standpoint.

Loader::model('product/model', 'core_commerce');
Loader::model('cart', 'core_commerce');      
$hasProductsOfType = false;
if (!isset($cart)) {
   $cart = CoreCommerceCart::get();
}
$items = $cart->getProducts(); 
foreach($items as $it) {
   $product = $it->getProductObject();
   if($product->getAttribute('gender') == 'male'){
      $hasProductsOfType = true;
      break;
   }
}
madesimplemedia replied on at Permalink Reply 1 Attachment
madesimplemedia
Sorry still not working, but if I use this ChromePhp::log($product); I can get the console to log all the product properties - attached.

However, if I do this: ChromePhp::log($product->getAttribute('product_type')); there is no output to the console, so it doesn't seem to be getting the attribute?
madesimplemedia replied on at Permalink Reply
madesimplemedia
Got this working, needed a capital E on my event attribute, not lowercase.

Thanks for all your help! :)