Magento Using Observer To Split Collection Unless Better Method - magento

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.
Thank you in advance for any help!

To be able to get value from your collection
(CommerceExtensions_AmazonAffiliate_Model_Mysql4_Quote_Collection), you have to use your own template and block.
Make own template on checkout, may be commerceextension/checkout.phtml. And Do like $this->amazonItemCollection()
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
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.

Related

Displaying single product through list.phtml edit

So what i'm trying to achieve is that the product with the attribute 'promotion' set to 'yes' is displayed on the frontpage of the website. This is working, but the .phtml file i'am using with this is the regular list.phtml. This is currently showing all the items I have set to promotion but I only want to show 1.
So in short: How do I edit the list.phtml to only show 1 product instead of everything?
Change how you are pulling your collection. Clone/Rename your list.phtml, for example promotion.phtml. Then change this line, from this:
$_productCollection=$this->getLoadedProductCollection();
To this:
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('promotion', 1)
->addAttributeToSort('updated_at', 'DESC')
->clear()->setPageSize(1)->load();
And it should load only one item with promotion set to yes. Make sure you set the new template in either your CMS Page Content or XML, depending on which method this is added.
Explanation
Mage::getModel('catalog/product')->getCollection(): Gets the Product Collection. You can get other collections by changing the model, such as "catalog/category" and "cms/page".
->addAttributeToSelect('*'): Adds All Product Columns. This can be exchanged for things like ('name', 'url'). I'm assuming it's faster than loading all but I haven't benchmarked it. Since you're using the full template it's probably best to leave this set to all.
->addFieldToFilter('promotion', 1): Filters out products by attributes. Here we have the products filtered for all those with the 'promotion' attribute set to 1(yes/true). Products use this one, while categories use ->addAttributeToFilter() oddly enough. Definitely give Alan Storm's Collection Explanation(link below) a read through to know what all you can do with this one. You can add multiple filters to your collection, either by adding another ->addFieldToFilter(), or storing your filters in nested arrays.
->addAttributeToSort('updated_at', 'DESC'): Sorts the product collection by specific attribute and direction. Here I have the "updated_at" date set to descending order, "ASC" is ascending. You can add multiple sorting attributes, pay attention to the order you add them in, of course.
->clear()->setPageSize(1)->load(): These three are needed to make adjustments to how much the collection pulls. ->clear() must be called before it will allow changing how many products are pulled. The ->setPageSize() bit is where you specify how many products you would like to return, and ->load() of course loads the collection. Note that if you do not limit the size of the collection returned, you do not need this entire line, the products will iterate without having to call ->load().
Resources
Alan Storm said it best, give this a read and you should be a pro at manipulating collections: http://alanstorm.com/magento_collections

Remove a Magento attribute from configurables and associated simple products

I have some configurable products that have many associated simple products, each with many custom options. The configurables use three attributes. My client has now decided that they want to remove one of the attributes. I have used the SQL method to remove one of the attributes, but this is now affecting the custom options when the remaining attributes are selected. The custom options no longer show up. I am assuming this because the attribute is still part of the associated simple products.
Is there a way to remove the attribute from the associated simple products as well, so that the custom options will show correctly when the remaining attributes are chosen?
The attribute, which you are trying to remove is part of each configurable super product, not associated products, as far as I know. Check 'catalog_product_super_attribute' table in db, remove all rows, which have 'attribute_id' set to id, which you want to remove.
Clean the cache, reindex and see, if it works.
To remove one super product attribute from all configurable products, you could execute this SQL query in the database:
DELETE FROM catalog_product_super_attribute WHERE attribute_id = <id>;
The table catalog_product_super_attribute links products to super product attributes.

Magento Price Alerts - Trigger on Tiered Price change

I've never set a store up to use Price Alerts before, and now that I've gone through and gotten it to work, it appears as though the alert is only triggered when a product's 'Price' attribute changes.
This is nice, but what about tiered pricing? Looking through the ProductAlert core module, I am not sure where this attribute is being filtered.
I'm guessing that an additional model, say 'productalert/tierprice' will need to be created, followed by a new method in Observer.php, likely _processTierprice(). Just not sure if this concept is correct... could use a little guidance.
Thanks!
This shouldn't be too difficult; lots of options (it's Magento, after all)!
I'd add new methods to deal with tier price alerts. For products with tier pricing, you'd need to capture the relevant tier data along with the final price (either in a new table or in new columns on the existing product alert table). The alerts are sent out via scheduled job. So, in a nutshell (overrides where necessary):
Alter the product alert schema as necessary, and amend the frontend form fields to suit
In the overridden Mage_ProductAlert_AddController add a tierPriceAction() method to process the alert subscription, or adjust its existing priceAction() to handle the data being posted in / tier condition
Override Mage_ProductAlert_Model_Observer as needed; I'd include a _processesTierPrice method() similar to _processPrice().
Add a resource model method to get the tier-related prices
Add a tier price email block and template (reference Mage_ProductAlert_Block_Email_Abstract)
Profit!
As a reference for your price logic, see Mage_Catalog_Model_Product_Type_Price.

Magento 1.4.0.1 create and populate new attribute value for almost all products

I am trying to figure out how I might be able to do this - possibly in the mysql database, but I'm not sure of all the tables involved, so daren't go much further?
I have added an attribute called "condition" to the defaul attribute set.
I need to populate that attribute value for every product, but the admin system requires me to fill in many other attributes if I try to bulk update them using the admin form. This can't happen as "description" is different for every product, obviously.
So, can someone tell me how I might populate this attribute value with the value "new" for every product in my database?
Realistically, I can change this attribute value for the number of products that need a different value "used", but if the update could be filtered on SKU, I can make it work right first time. I think!
The programming approach is to save the following script in your Magento folder and execute it by visiting the address directly. It might be slow but that doesn't matter if it's only going to be run once and then deleted afterwards.
<?php
require 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getResourceModel('catalog/product_collection');
foreach ($products as $product) {
// replace IS_NEW with a test perhaps using $product->getSku()
$product->setCondition(IS_NEW ? 'new' : 'used')
->save();
}
Not a programming answer so perhaps not suitable for Stack Overflow but there are a number of workarounds.
You could export all products to Excel, bulk update many rows at once then re-inport. Use Magmi if you have many products.
There are some extensions which let you update many attributes at once. This Mass Actions is expensive but gives you an idea what might be available if you search.
As much as I loathe desktop applications for web services there is Store Manager which specialises in bulk actions.

Magento: Add a "fake" product to cart/quote

I understand how to programmatically create a product and also add to cart. I know this might sound dumb but is it is possible to generate a product on the fly and add that to the cart/quote but never actually save it in the database.
We want to create a made to order interface and I was thinking at the end it could add a bundle product with all the selections but that bundle product wouldn't actually exist in the backend.
I figured as long as you can make sure the quote and order has what it needs in terms of the product it would be ok, but obviously there is probably a lot that is tied to looking up stuff in the db on a specific sku or ID. I know that if you delete a product and then look at an order in the admin that causes issues, at least it did for this one scenario I was dealing with.
I was thinking of creating a giant bundle product that had like 6 different bundle items and each item could potentially have like 500 products and then based on what the user selects I programmatically add the bundle to cart. But then I wasn't sure if there would be a negative affect with having a gigantic bundle product like that as well.
UPDATE:
I don't think this will work, obviously there are a lot of information tied to the product in the database and we setup a test and right away we get an error for $item->getProduct(). We are moving forward with creating a giant bundle product and also the generic product with adding custom options on the fly, which Anda pointed out below. Any other suggestions will be greatly appreciated.
I'm not sure that clockworkgeek's approach is going to work. On every page load, Magento loads the items from the cart to make sure that they are still valid (in-stock, prices correct, etc), and amends the cart to reflect those values. My understanding of the system in the past has been that a product in the cart needs to have a corresponding database value to survive this process.
The "giant bundle product" approach is a pain, but in the past has been the best approach I have found. Attempting to change the values of the product (such as price or attributes) will be overridden by the cart checks, so you need a product w/ maximal flexibility, such as an overly-customized bundle product or configurable product.
Hope that helps!
Thanks,
Joe
Why not create a generic product in db and then set the product customization as custom options (additional_options) on the fly depending on the user selection. You can add custom options to the product (actually to the quote item) without having to save them in the database. I did this once for a website that sells glasses with prescription. The prescription was added as an option.
You can programmatically create Mage_Sales_Model_Quote_Items and add them to the cart. You've noticed it needs a product to match it's product ID but it needn't be a useful one. It could be a blank, disabled product, also created in code. All that's needed is a stub.
The necessary stuff for the cart is stored in the quote item - fields like name, value and quantity. Those fields are then copied directly to the order without using a product.
Mage::getModel('catalog/product')
creates a new product. you can add it to a cart, by doing something like this:
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product')
->setStoreId($storeid)
->setTypeId($type_id)
->setQty($quantyty)
->setWhatAttributYouWant($attribute);
$cart->addProduct($product);
product attributes you can find in the DB in tables that start like catalog_product_... or take an already created product, and see what attributes it has in the _data array (with debugger or just print_r($product->getData))

Resources