1

I've researched all over the place, and being new to Magento development, I am having a hard time wrapping my mind around how to accomplish this task.

Our store has a membership that is set as it's own simple product. The membership item is listed at $5. The issue is that this membership can only be added has added another item (a hat) to the cart.

So, in order to purchase the membership, you must have a hat from x, y or z category in your cart.

Any thoughts on the best approach to coding this up? Is is possible via simple settings OR will I need to dig into some of the core code? Any help would be extremely helpful - especially code examples, specific file names/paths that I should be looking at.

If it helps, I'm working with Magento Enterprise V. 1.12.0.2

Nick Parsons
  • 8,377
  • 13
  • 48
  • 70

1 Answers1

1

You could create an observer for add_to_cart_before, to see what products the user have in their cart

 <add_to_cart_before>
      <observers>
          <add_to_cart_before>
               <class>dispatcher/observer</class>
              <method>hookToAddToCartBefore</method>
          </add_to_cart_before>
      </observers>
 </add_to_cart_before>

In your observer (see how to create an observer)

$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$category_in_cart_ids = array();
foreach ($items as $item) {
    array_push($category_in_cart_ids, $item->getProduct()->getCategoryIds());  // append category array to category_in_cart_ids
}

if(specific item id){
   if(in_array('your category ids', $category_in_cart_ids)){
     // product specific category in cart
   }
   else{
     //dont add this product to cart/ remove
   }
}

See http://inchoo.net/ecommerce/magento/dispatching-before-and-after-events-to-magento-core-actions/

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • I attempted to ad an observer, however it's not capturing the event for some reason. I've read through the magneto documentation, and the provided blog at inchoo.net. Nothing seems to work. My code has is written/structured as follows: **app/etc/modules/Require_Additional_Product.xml** - [link]https://gist.github.com/4470435 **app/code/local/Hatclub/RequireAdditionalProduct/etc/config.xml** -- [link]https://gist.github.com/4470445 **app/code/local/Hatclub/RequireAdditionalProduct/Model/Observer.php** -- [link]https://gist.github.com/4470451 Any thoughts? Your help is much appreciated! – Nick Parsons Jan 06 '13 at 21:44
  • I dont think the module is loading because it not configure correctly ...Should be `Hatclub_RequireAdditionalProduct.xml` NOT `Require_Additional_Product.xml` ... not ... take a look at http://stackoverflow.com/a/12696681/1191288 ... – MagePal Extensions Jan 06 '13 at 22:06
  • renamed file to Hatclub_RequireAdditionalProduct.xml and it's still not catching an event :/ Also cleared cache. Pulling my hair out at this point. – Nick Parsons Jan 06 '13 at 22:19
  • Check to see if you custom module is active by going to `admin -> system -> config -> Advanced -> Advanced ... is 'Hatclub_RequireAdditionalProduct' listed? – MagePal Extensions Jan 06 '13 at 22:33
  • You were correct. It was a combination of the module name, and it being disabled. Thank you for your help. Working now! – Nick Parsons Jan 07 '13 at 05:18