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
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
Anyone know about this please?
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
Viewing 15 lines of 16 lines. View entire code block.
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:
Doesn't seem to be getting the attribute value??
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??
Can you tell me where you are placing this code?
Can you include a screenshot of the product attribute setup?
Can you include a screenshot of the product attribute setup?
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
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
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; } }
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?
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?
Got this working, needed a capital E on my event attribute, not lowercase.
Thanks for all your help! :)
Thanks for all your help! :)