I need to get all products where a specified product is listed in the related products. So I'll try to remove unnecessary items from the cart, after removing the main product.
Is there a simplier way then to loop all cart items?
Thanks for advice.
I need to get all products where a specified product is listed in the related products. So I'll try to remove unnecessary items from the cart, after removing the main product.
Is there a simplier way then to loop all cart items?
Thanks for advice.
The only two ways I can think of are:
Since the products seem to be so tightly related and dependent on the main product, you may want to consider creating bundle products instead . It may also be confusing to customer having 3 items in their cart and delete 1 of them (the main product) and now their cart is empty and for some unknown reason they still wanted the 2 related products (that you automatically deleted).
As you mention above - on delete loop through all product in your cart (see code below)
Create In app/local/RWS/AutoDeleteRelatedCartProducts/etc/config.xml
<config>
<global>
<models>
<autodeleterelatedcartproducts>
<class>RWS_AutoDeleteRelatedCartProducts_Model</class>
</autodeleterelatedcartproducts>
</models>
</global>
<frontend>
<events>
<sales_quote_remove_item>
<observers>
<autodeleterelatedcartproducts>
<type>singleton</type>
<class>autodeleterelatedcartproducts/observer</class>
<method>removeQuoteItem</method>
</autodeleterelatedcartproducts>
</observers>
</sales_quote_remove_item>
</events>
</frontend>
</config>
Create in app/local/RWS/AutoDeleteRelatedCartProducts/Model/Observer.php
<?php
class RWS_AutoDeleteRelatedCartProducts_Model_Observer
{
public function removeQuoteItem(Varien_Event_Observer $observer)
{
//get deleted product
$delete_product = $observer->getQuoteItem()->getProduct();
// Get all related products
$related_products = $delete_product->getRelatedProductCollection();
// get all related product id and save in array
$related_product_ids = array();
foreach($related_products as $product){
$related_product_ids[] = $product->getId(); // double check to make sure this product_id
}
foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
{
// if getId is a related product remove it
if(in_array($item->getId(), $related_product_ids))
Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}
}
}
?>
Read more @
Observer for removed items in the cart
Help with Magento and related products
Magento - How to check if a product has already been removed from the cart
if i was you i might not add the products in the first place but instead use an observer for the checkout_cart_product_add_after event. check the product against your expected product. use the method by R.S. to get a handle on the quote and check quantity of the product in you cart. add a custom option to the product that lets the customer know if he's got his free goodies. this might be appropriate Magento - Quote/order product item attribute based on user input
then add an observer to this event sales_convert_quote_to_order. this observer can check for quantity and adapt something like this to give the products to the customer Skip Checkout in Magento for a downloadable product your only getting singletons and using observers so this method is much less expensive than lots of adding and removing during the cart process. also it would look lots nicer.
if you want i'll try and implement it but using a copy of your site and db since i'm too lazy to set up the products.
ps. maybe you need to observe this event too checkout_cart_update_items_before
ps. ps. maybe i should check before i comment lol. do lol's get you banned from here?