1

I am almost done building an Amazon Affiliate extension. In my catalog, the amazon products use the attribute set named AmazonAffiliate, all of the other normal products in the catalog just use whatever set happens to be assigned to them. People can add both regular products to the cart as well as amazon products to the cart.

The separation of the regular products vs. amazon products comes in the cart screen. What I want on the cart page is 2 item tables. One with Amazon products and one with regular.

Through the use of an observer for the event core_collection_abstract_load_before, I was able to filter out the Amazon products from the cart, the totals, everything. I love the way it pulls them out with just one function.

Here is the observer function:

public function excludeAmazonProductsFromMainCart($observer)
{
  $collection = $observer->getEvent()->getCollection();
  if($collection instanceof Mage_Sales_Model_Resource_Quote_Item_Collection) {
    $attributeSetId = Mage::getModel('eav/entity_setup','core_setup')
                        ->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY,CommerceExtensions_AmazonAffiliate_Model_Module::ATTRIBUTE_SET_NAME);
    $productTable = Mage::getSingleton('core/resource')->getTableName('catalog/product');
    $collection->getSelect()->joinLeft(array('product_table'=>$productTable),'`main_table`.`product_id`=`product_table`.`entity_id`',array('attribute_set_id'));
    $collection->getSelect()->where('`product_table`.`attribute_set_id` != '.$attributeSetId);                   
  }
  return $collection;       
}

Now, the problem is that I want another collection that grabs the items that were excluded in the observer function above. This way, the amazon cart table would have those products and the normal magento product cart table would have my normal catalog products. I just cant seem to split the collection into two.

And in case it can't be split into two, I have the amazon product collection I need created as CommerceExtensions_AmazonAffiliate_Model_Mysql4_Quote_Collection. I just cant seem to set it on my new block.

Here is a screenshot of what I am trying to do. I know it can be done because there are people selling extensions with the same type of functionality.

enter image description here

Thank you in advance for any help!

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Shawn Abramson
  • 701
  • 1
  • 5
  • 18

1 Answers1

0

To be able to get value from your collection (CommerceExtensions_AmazonAffiliate_Model_Mysql4_Quote_Collection), you have to use your own template and block.

  1. Make own template on checkout, may be commerceextension/checkout.phtml. And Do like $this->amazonItemCollection()
  2. You can pass variable to that template from your block (CommerceExtensions_AmazonAffiliate_Block_Checkout_Cart). Write function - get amazonItemCollection(). Within this function you should be able to instantiate your collection
  3. Add following xml in layout/checkout.xml file which connect your block and template

I have not tested this code. However it should work with few trial/error.

Krishna Sunuwar
  • 2,915
  • 16
  • 24
  • the template is not the problem. Its the data being fed into the template that I am trying to change. Im trying to split the collection into 2 usable parts that I can pump into the 2 separate blocks. – Shawn Abramson Feb 22 '14 at 06:58
  • I mean to suggest you write is custom function to get collection of amazon products in your resource, call that from your block and pass those product to your view. What you did for filter out amazon product is good. – Krishna Sunuwar Feb 22 '14 at 07:19
  • What I have done now is create a 2nd collection, and just filtered out all of the regular products and keep the amazon products. However, to make it all the way to the block in order to continue using the same template file, it gets passed through multiple models before it gets to the block. At this point, I am trying to avoid duplicating all of the models it gets passed through. The errors I am getting now are isVisibleInSiteVisibility() on a non-object. I am chasing this thing all over the place. – Shawn Abramson Feb 22 '14 at 14:25
  • I ended up keeping the observer to make sure that the amazon products stay completely out of the magento cart and ended up creating a brand new block to add into the checkout screen. It really only needs to display the amazon products because people who buy those will checkout on Amazon directly. – Shawn Abramson Feb 22 '14 at 18:04