I'm looking to create "Addon Products" in Magento. What I mean is: If the product were a Greeting Card, and you were to add that to your cart, you might be presented with options on the checkout page to add an "Envelope" or "Stamps". These would be separate products with their own prices, however, they would not be available otherwise in the store. In other words, some sort of "Linked / Child Products" that only become available once the "Parent" product has been added.
Does this sort of product configuration exist within Magento? Doesn't seem to be a Bundled Product.
Yes Magento does this out of the box.
Against each product assign 'cross sell' products, using the cross sell tab on the left. When you first get to it, if the list is empty press the 'reset filter' button to show all the products in the store. Find the ones you want and put a tick next to them, then click save.
The block you are after is;
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
Which most template load within "content" in the layout/checkout.xml layout file of your theme,
<checkout_cart_index translate="label">
////
<reference name="content">
///// HERE
</reference>
/////
</checkout_cart_index>
And then add this to the checkout/cart.phtml template (if it's not there already);
<?php echo $this->getChildHtml('crosssell') ?>
But you can add it to wherever you wish.
To handle these products not appearing elsewhere, set their visibility to 'catalog' and either put them in a category that isnt visible or dont add them to a category.
TO ANSWER YOUR SECOND QUESTION...
You asked how you could remove all 'add on' items from the basket if there are no 'main product' items in the basket. Here is a quick solution that will do this.
Create a custom select product attribute, give it ID code 'product_type_var' and give it 2 options 'Main Product' and 'Addon Product'. Add it to your product attribute set and set the values against the appropriate products.
You can then run the following code against the basket. Ideally you would create a module with an event observer - but for the sake of this example, you could also place this code at the top of
app/design/frontend/XXX/YYY/template/checkout/cart.phtml
Here's the code;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$needsAction = true;
$toRemove = array();
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$productLoad = Mage::getModel('catalog/product')->load($product->getId());
$customVariable = $productLoad->getResource()->getAttribute('product_type_var')->getFrontend()->getValue($productLoad);
if($customVariable == 'Main Product') {
$needsAction = false;
break; // No need to do anything
}
if($customVariable == 'Addon Product') {
$toRemove[] = $productLoad->getId(); // Build list of addon IDs
}
}
if($needsAction && (!empty($toRemove))) {
// There are no Main Products and 1 or more Addons
foreach($toRemove as $removeId) {
$quote->removeItem($removeId)->save();
}
}
Revision 3
To make sure any 'addon' products only remain in the cart if they relate to a specific 'main product' found in the cart, try this;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$allowedUpsells = array();
$upsellsInCart = array();
$allIdsInCart = array();
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$productLoad = Mage::getModel('catalog/product')->load($product->getId());
$customVariable = $productLoad->getResource()->getAttribute('product_type_var')->getFrontend()->getValue($productLoad);
if($customVariable == 'Main Product') {
$allIdsInCart[] = $productLoad->getId(); // Build list of all products in the cart
$upsells = $productLoad->getUpSellProductCollection(); // Get this products available upsells
foreach($upsells as $upsell) {
$allowedUpsells[] = $upsell->getId(); // Build full list of allowed addon IDs
}
}
if($customVariable == 'Addon Product') {
$allIdsInCart[] = $productLoad->getId(); // Build list of all products in the cart
$upsellsInCart[] = $productLoad->getId(); //Build full list of addon IDs
}
}
if(!empty($upsellsInCart)) { // Upsells might need attention
$allowedVsInCart = array_intersect($allowedUpsells, $allIdsInCart); // Remove other upsells that are avaiable to the product but not in the cart
$toBeRemoved = array_diff_assoc($allowedVsInCart, $upsellsInCart); // Now find the products in the cart that shouldnt be
if(!empty($toBeRemoved)) {
foreach($toBeRemoved as $removeId) {
$quote->removeItem($removeId)->save();
}
}
}
Related
I have a custom made menu, that contains products from specific category (category id=3). In my menu there is a block:
{{block type="catalog/product_list" name="product_list" mode="grid" template="catalog/product/category_list_1.phtml"}}
I can normally see the products on main site and inside the category, but on product page the getBlockTemplateProcessor() doesn't return any data.
The code inside file is the same as for list.phtml, i just made some html modifications, located in:
\catalog\product\list.phtml
Why exactly is my code not rendering? Is it a problem that the files can't be located from product site?
Why all is good inside a category site and on product site not?
I suggest you use your own block extending the product list block, so that you can properly set the product collection, something like:
class YourNamespace_YourModule_Block_List extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$prodIdArray = array(1,2,3,4,5);
$collection->addAttributeToFilter('entity_id', $prodIdArray);
$collection->addStoreFilter();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
You can filter to add or remove any products that you want from the listing.
I am programming an extension which adds relevant products to the cart once you add an item to the cart. Example if you are buying a pen I am going to add paper to the cart. Bundled packages are not an option since I need to match certain conditions. I tried the following:
I set up an event listener "sales_quote_item_collection_products_after_load" scan all the products in cart and add the relevant products. Sadly you have to reload the cart in order to make the products appear.
I used this code in, my event listener, to add products to cart:
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
// Add a product (simple); id:12, qty: 3
$cart->addProduct(12, 3);
$cart->save()
The strange thing is that removing products using the cart helper works (without refreshing):
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $productId) {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
}
}
Is there a way to tell Magento to "requote" or what would you recommend? I also thought of adding the product, at the add to cart listener. But in that case I will need to implement it as well on update and remove, so it will work correctly. Using sales_quote_item_collection_products_after_load after load seemed to be the best option, since I have everything in one place.
You need to change in your code
$product_model = Mage::getModel('catalog/product');
$product_id =35;
$my_product = $product_model->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => 1));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
I want to show count for all the products under category and also sub categories.I can't use toolbar for that purpose.I know default magento toolbar have code but i want to use custom code to only get count.i try to get collection by calling block method getTotalNum() but didn't succeeded.Thanks in advance
In list.phtml you can try this code:
//get the current layer
$layer = Mage::getSingleton('catalog/layer');
//get the current category
$currentCategory = $this->getLayer()->getCurrentCategory();
//get the subcategories
$categoriesChildren = $currentCategory->getChildrenCategories(); //means get your children
$categoriesSiblings = $currentCategory->getParentCategory()->getChildrenCategories(); //means get your siblings
//(decide if you want to use siblings or children, here we use children)
//get the product collection for the current layer
$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer->prepareProductCollection($productCollection);
//count up
$productCollection->addCountToCategories($categoriesChildren); //note the function accepts a collection of categories and counts them all
//display
echo("<ol>".PHP_EOL);
foreach ($categoriesChildren as $key=>$_category){
if($_category->getIsActive()){
echo("<li>".PHP_EOL);
echo("<a href=\"".$this->getCategoryUrl($_category)."\">".$this->htmlEscape($_category->getName()));
echo("(".$_category->getProductCount().")</a>".PHP_EOL);
echo("</li>".PHP_EOL);
}
}
echo("</ol>".PHP_EOL);
//done
I think you might want to customise it a little.
I want show custom options on cart page i tried using $_options = $this->getOptionList() but it only shows the selected option only , i want to retrieve all options.
To get product custom option value at cart page which are set at 'AddtoCart' time try with following code.
$cart = Mage::helper('checkout/cart')->getCart()->getQuote()->getAllItems();
/* cart item loop */
foreach($cart as $item) {
/* This will get custom option value of cart item */
$_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
/* Each custom option loop */
foreach($_customOptions['options'] as $_option){
echo $_option['label'] .'=>'. $_option['value']."<br/>";
// Do your further logic here
}
}
Already replied at
Display Magento Custom Option Values in Shoping Cart
You can get only selected options from using this function $this->getOptionList(), but if you actually want to retrieve all custom options then first of all you need to retrieve product id
You can get product like this on cart page
$_item = $this->getItem();
$_item->getProduct();
This will retrieve product from this you can get product and load product.
$product = Mage::getModel('catalog/product')->load($_item->getProduct()->getData('entity_id'));
$product = Mage::getModel('catalog/product')->load($product);
foreach ($product->getOptions() as $o) {
print_r($o); // show all product options
}
}
I'm working on a Magento website, and what I would like to do is the following: I created an attribute to hide certain products. On a grid view page, I'm using the following code to exclude them from the list:
<?php if ($_product->getAttributeText('hideproduct')):?>
<?php else: ?>
Basically, it's just saying that when 'hideproduct' shows up, don't show anything.
This works for simple products, but for configurable products, it's a bit more complex, and it doens't seem to work with this. Let's say that I want to hide a product with a certain color, it always keeps appearing in the dropdown menu of the configurable product.
Does anyone have a solution for this?
This is what i did for one of my task (if there is a better way please let me know)
You have to extend Mage_Catalog_Block_Product_View_Type_Configurable for this purpose.
In it
public function getAllowProducts()
{
if (!$this->hasAllowProducts()) {
$products = array();
$allProducts = $this->getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->getProduct());
foreach ($allProducts as $product) {
if ($product->isSaleable()) {
if(!$product->getEcoReport())
{
$products[] = $product;
}
}
}
$this->setAllowProducts($products);
}
return $this->getData('allow_products');
}
eco_report is my attribute label.
So this is how it works... if for a simple product ( of a particular configurable product) if attribute eco_report is set then that product wont show in the configurable product's drop down list (on view page).
So all simple product's eco_report attribute has to be set so that it wont be shown on the configurable product's dropdown...