Magento Limit Number of Products in Home page - magento

I have added this code {{block type="catalog/product_list" category_id="25" template="catalog/product/list.phtml"}} in cms home page
I want to limit no of products to display to nine to this category only.How can i do that?

I don't think there is a value you can pass into the block tag to limit it. I would suggest making a new list.phtml file that limits it there.
Let me look at the code real quick.
Ok. If you were to copy the file /app/design/frontend/default/default/template/catalog/product/list.phtml
to
/app/design/frontend/default/default/template/catalog/product/list-limit.phtml
and then edit it as follows:
LINE49: After the foreach
<?php if($_iterator >=9) { break; } ?>
LINE94: Where $_collectionSize is assigned change to:
<?php $_collectionSize = main(9, $_productCollection->count()) ?>
Line97: After the foreach
<?php if($i >= 9) { break; } ?>
It should achieve what you desire regardless of Grid or List view.
... shortly an alternative method ...
The other way would be to edit the List.php file that loads the product list that the phtml file presents. Block Type of 'catalog/product_list' means you need the file:
/app/code/core/Mage/Catalog/Block/Product/List.php
In there you will see the method getLoadedProductCollection, which calls _getProductCollection. That code could be edited to filter/limit the number of returned products. You would want to make a copy of that file though, and update the block link in your page. Don't add underscores to the name, as that will require the file be put in a subdirectory.
Hope this helped.

Following on from the previous answer, I seem to have acheived this by editing the List.php by adding the following after line 96.
return $this->_productCollection
->setPageSize($this->getProductsCount());
}
/**
* Set how much product should be displayed at once.
*
* #param $count
* #return Mage_Catalog_Block_Product_New
*/
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
/**
* Get how much products should be displayed at once.
*
* #return int
*/
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
and adding this after line 43
/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 100;
/**
* Products count
*
* #var null
*/
protected $_productsCount;
I got the codes from new.php

Related

Add module to subpage in Joomla

I have a module which I add from admin panel to some subpage. After that some subpages show properly content with this module but some subpages after click on it open blank, white page with no content inside. I don't know what caused that problem. Why some subpages with this module work properly and some show blank page?
This is what I see on page:
Fatal error: Cannot redeclare class ModProductsMenuHelper in /opt2/data-dev/modules/mod_products_menu/helper.php on line 15
Thank you for help!
This is my code
<?php
/**
* Slajder class for Hello World! module
*
* #package Joomla.Tutorials
* #subpackage Modules
* #link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
* #license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
class ModProductsMenuHelper
{
/**
* Retrieves the hello message
*
* #param array $params An object containing the module parameters
*
* #access public
*/
public function getProducts($params)
{
$lang = JFactory::getLanguage();
$langTag = $lang->getTag();
$app = JFactory::getApplication();
$isSMB = $app->get('isSMB');
$parentMenuId = $langTag == 'pl-PL' ? 107 : 103;
$results = $this->getChildren($parentMenuId, $langTag);
return $results;
}
private function getChildren($parentId, $langTag){
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select(array('id', 'title', 'path', 'alias'))
->from($db->quoteName('#__menu'))
->where("(language = '*' OR language= ".$db->quote($langTag).") AND published = 1 AND parent_id=".$parentId)
->order($db->quoteName('lft') . ' ASC, '.$db->quoteName('id') . ' ASC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
foreach ($results as $key=>$val){
$results[$key]->children = $this->getChildren($val->id, $langTag);
}
return $results;
}
}
From what I can gather you have created a module and assigned it to specific pages. You haven't mentioned what the contents of the module are (custom html etc).
Have you assigned the module to the correct pages in the 'module assignment' tab? Have a look at this question and answer as it explains how to do that.
If you are seeing a white page, i'd suggest enabling error reporting in Joomla. This should provide you with additional useful information about the error.
If you have a link to your website that would be helpful, and the version of Joomla you are using.

Custom Tier Price not working in checkout page magento

I have developed a custom module to meet my project requirements using Alan Storms tutorial for creating modules in magento.
I had the requirement of changing the price attribute dynamically on frontend based on a livefeed. Everysecond the feed is updated so every time the page refreshes a new price must be displayed for each product on the site.
I have override the product module and the price modules for this purpose. The issue is with tier pricing. When tier pricing comes into place I need to calculate the tier-price based on the live price.
For this also I managed to change using the price_type class override.
Now whenever an item is added to cart the tier-pricing was not working for that I wrote event_trigger ie an Observer which updates the tier_pricing on the event "checkout_cart_save_before" and here's my code
class My_Custom_Model_Observer extends Varien_Event_Observer
{
public function __construct()
{
}
public function updateCartBasedOnLiveFeed($observer)
{
foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* #var $item Mage_Sales_Model_Quote_Item */)
{
$tierPrices = array();
$tierPrices = $item->getProduct()->getTierPrice();
$itemPrice = $item->getProduct()->getPrice();
$i=0;
foreach($tierPrices as $key => $tierPrice)
{
if(!is_numeric($key))
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrice['price']/100));
$tierPrices[$key]['price'] = $updatedTierPrice;
$tierPrices[$key]['website_price'] = $updatedTierPrice;
}
else
{
if($tierPrice['price'] > 0)
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrice['price']/100));
$tierPrice['price'] = $updatedTierPrice;
$tierPrice['website_price'] = $updatedTierPrice;
$tierPrices[$i] = $tierPrice;
$i++;
}
}
}
$item->getProduct()->setData('tier_price',$tierPrices);
}
}
}
The above code works excellently in cart page. But when it comes to checkout page. It works for a single item and when tier-pricing comes into play it does apply cart prices.
Please help me with this.
I also tried using other events along with the above event.
Event: sales_quote_save_before
public function updateQuoteLive($observer)
{
$tierPrices = array();
$quote_item = $observer->getEvent()->getQuote;
$itemPrice = $quote_item->getProduct()->getPrice();
$tierPrices = $quote_item->getProduct()->getTierPrice();
$tierPricesSize = sizeof($tierPrices);
for($i=0;$i<$tierPricesSize;$i++)
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrices[$i]['price']/100));
$tierPrices[$i]['price'] = $updatedTierPrice;
$tierPrices[$i]['website_price'] = $updatedTierPrice;
}
$quote_item->getProduct()->setData('tier_price',$tierPrices);
}
When I tried to print the getQuote() function available in Quote.php I find that the tier prices there are not the ones which I updated using the first event. So I think I need to update the price before saving the quote. Please any one help me and show the correct direction.
Please help me with this I am missing some important step. Any help is greatly appreciated.
Thanks in advance.
It might be better off "saving" the new price in to the database when you update.
Try something along the lines of:
$product = $observer->getProduct();
$procuct->setPrice($updatedPrice);
This way when it comes to checkout it will be pulling in the correct price from the database (and avoids the headache of correcting it "mid-flight"
i realized such a project like you. I have no sales_quote_save_before Observer. I only use the checkout_cart_save_before. Based on the session the price will be setted.
I realized that like this way:
public function updatePrice( $observer )
{
try {
$cart = $observer->getCart();
$items = $cart->getItems();
foreach($items as $item)
{
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
}
} catch ( Exception $e )
{
Mage::log( "checkout_cart_save_before: " . $e->getMessage() );
}
}
I calcute the tierprices on the fly and with this Observer. All prices will be set up correct in the qoute.
Maybe you should try this way.
Regards boti
At last figured out the issue and got the solution.
The problem was that in cart page or checkout page when the getTierPrice() function is called, which is present in /app/code/core/Mage/Catalog/Product.php. It takes one parameter named $qty which is by default null. This function in turn calls the function getTierPrice which is present in /app/code/core/Mage/Type/Price.php file which takes two parameters $qty and $productObject. By default $qty is null and when it is null the function returns an array of tier_prices. But when the $qty value is passed then the function returns a single for that particular quantity.
So, I wrote my own custom function which calculates the tier prices based no my requirements like
I overridden both the core files with my custom module following Alan Storm's tutorials.
I've extended Mage_Catalog_Model_Product with My_CustomModule_Model_Product class and
then
Mage_Catalog_Model_Product_Type_Price with My_CustomModule_Model_Price
And then in /app/code/local/My/Custommodule/Model/Product.php
I added my custom code like
public function getTierPrice($qty=null)
{
if($qty)
{
return $this->getPriceModel()->getCustomTierPrice($qty, $this);
}
return $this->getPriceModel()->getTierPrice($qty, $this);
}
Then in /app/code/local/My/Custommodule/Model/Price.php
<?php
public function getCustomTierPrice($qty = null, $product)
{
$allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
$prices = $product->getData('tier_price');
if (is_null($prices)) {
$attribute = $product->getResource()->getAttribute('tier_price');
if ($attribute) {
$attribute->getBackend()->afterLoad($product);
$prices = $product->getData('tier_price');
}
}
foreach($prices as $key => $customPrices)
{
if($prices[$key]['price'] < 1)
{
$prices[$key]['price'] = abs($product->getPrice() - ($productPrice * ($customPrices['price']/100)));
$prices[$key]['website_price'] = $prices[$key]['price'];
}
}
}
which retured a customized value when $qty is passed and voila it worked.
I just posed this answer so that any one else who has similar requirement may get benefited with this.
?>

Outside link for Add to Cart

We want to get an outside link for a Magento-store page, that would add to cart an item that's linked from a PDF link (it's a technical drawing with some parts that can be ordered separately)
I see that Magento uses JavaScript onclick="productAddToCartForm.submit(this)", but it can not be triggered to the specific item like this.
Is there any way this can be solved?
BR-:g
This is the basic url to call:
www.example.com/checkout/cart/add?product=[id]&qty=[qty]
If you want more details on how to do it with options etc. take a look here:
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_a_product_to_the_cart_via_querystring
You can give add to cart url like:
<?php echo $this->helper('checkout/cart')->getAddUrl($_product);?>
You can always loop over your product collection and ask the checkout/cart helper for the url:
$collection = Mage::getResourceModel('catalog/product_collection');
/* #var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
//... add filters to collection as appropriate
$cartHelper = Mage::helper('checkout/cart');
/* #var $cartHelper Mage_Checkout_Helper_Cart */
foreach( $collection as $product ){
/* #var $product Mage_Catalog_Model_Product */
$atcUrl = $carthelper->getAddUrl($product);
//... do what you need to with the above value (echo, fwrite, etc);
}
Note that you can also pass in product-type-specific options as a second param.

Magento - Add to Cart Error

I'm getting this error after add/remove item to/from cart. Add to Cart button does ajax call to add item to the cart. This kind of json string which will be used in top cart:
I'm stuck. Can you tell me where should I start from to debug?
the "Add to Cart" button does not work asynchrounisly in Magento's default behaviour. That means that you installed/developped a module to do this. For us to help you, we need to know what is it.
Anyways, this looks like a Zend_Dump or maybe a die: make a search in your files for these strings and see what comes up
Use Netbeans and Xdebug.
http://wiki.netbeans.org/HowToConfigureXDebug
Place a breakpoint on the /app/code/core/Mage/Sales/Model/Quote.php inside this function:
/**
* Adding catalog product object data to quote
*
* #param Mage_Catalog_Model_Product $product
* #return Mage_Sales_Model_Quote_Item
*/
protected function _addCatalogProduct(Mage_Catalog_Model_Product $product, $qty = 1)
{
$newItem = false;
$item = $this->getItemByProduct($product);
if (!$item) {
$item = Mage::getModel('sales/quote_item');
$item->setQuote($this);
if (Mage::app()->getStore()->isAdmin()) {
$item->setStoreId($this->getStore()->getId());
}
else {
$item->setStoreId(Mage::app()->getStore()->getId());
}
$newItem = true;
}
/**
* We can't modify existing child items
*/
if ($item->getId() && $product->getParentProductId()) {
return $item;
}
$item->setOptions($product->getCustomOptions())
->setProduct($product);
// Add only item that is not in quote already (there can be other new or already saved item
if ($newItem) {
$this->addItem($item);
}
return $item;
}
HTH

Update Item Product Options After Order Placed

I'm working on an observer that needs to add (a) serial key(s) to each item in the cart once an order is placed.
I'm listening to the event sales_model_service_quote_submit_success right now.
I've been able to access the order, get a list of the items, iterate through them, and get the product options. My code fails either when I try to setProductOptions or save--I'm not sure which, maybe it's both.
Here is the relevant code:
// Get access to order information
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($lastOrderId);
// Get the items from the order
$items = $order->getAllItems();
foreach ($items as $item)
{
// Pretend here is the call that fetches the serial keys for this item and stores them in $keyString
// If we actually received the keys in a string, store them with the item
if (!empty($keyString))
{
$productOptions = array();
if (count($item->getProductOptions()))
{
$productOptions = $item->getProductOptions();
}
$productOptions['keys'] = $keyString;
$item->setProductOptions($productOptions);
$item->save();
}
}
Any ideas what I have forgotten or done wrong? Thanks a bunch.
no such observer , at least i didn't find it from codebase, here's what you can use
Mage::dispatchEvent('sales_model_service_quote_submit_before', array('order'=>$order, 'quote'=>$quote));
Mage::dispatchEvent('sales_model_service_quote_submit_after', array('order'=>$order, 'quote'=>$quote));
and in your observer method
/**
*
* #param Varien_Event_Observer $observer
*
*/
public function setShippingDefaults(Varien_Event_Observer $observer) {
$order = $observer->getEvent()->getOrder();
}
the idea is that if you do it in before-action then you won't need to call save and if you do it in after-action then you do and if you do it in before action you might just end in endless loop if you are not careful.

Resources