Pagination class in Custom Module in joomla - joomla

I am developing one custom search module in joomla. so I am using joomla's in-built database connection and function.
Problem is that I want to use pagination class in this module but don't know any hint.
Please help me on this.
Thanks.

Step 1: Get the total number of items in your database
ex: select count(*) from #__some_table where ....
Step 2: Import joomla pagination and create pagination object
jimport('joomla.html.pagination');
$pagination = new JPagination($total, $limitstart, $limit);
where
$total = total number of items that you count in step 1
$limit = total number of items you want to display on a page
$limitstart = index of the first item in the page. For example if you have 20 items per page, 0 is start index for 1 page, 20 is start index for second and so on.
Step 3: Show pagination on your page
echo $pagination->getPagesLinks();
echo $pagination->getPagesCounter();

Related

Using Magento API to add all products to root category

I want to set all the products to the root category (next to the current category they are in).
I know the best way to do this is using the Magento API, but can somebody get me started on this?
Just to be clear, i don't want to change the category id, i just want to add another category id to the product.
EDIT
It takes a lot time to check all 3000+ products to see in what other category it is. Example: Root_catId = 175 Product_1 cat = 3 (needs to be: 3, 175) Product_2 cat = 9 (needs to be: 9, 175)
You can do this without using the API but if you want to use the API try following.
You can use following first you need to create a user with permissions to access soap requests in Magento admin
<?php
$proxy = new SoapClient('http://yourmagento/api/v2_soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$result = $proxy->catalogCategoryAssignProduct($sessionId, '3', '10'); // 3 is category id and 10 is product id
You can loop through every product to assign the product to a specific category.
Replace the root category id with the category id which you require.

Can you Clone a collection row in Magento?

I'm trying to build a custom grid in Magento, where I have a row for every product ordered including qty. For example, if a customer ordered 3 of a specific product - I need to have 3 rows that all have matching details. Is this possible?
This is for a custom product where each row represents a manufacturing run. I'm not necessarily looking for code examples, but I'm not sure if I can do this with the sql style collection process? Or if I need modify the query and loop through once I've colelcted all the data from the database?
Thanks
Instead of showing the product order item grid, I just added an export to xml button. Then, in the grid just modified the "_exportIterateCollection" method as the following:
foreach ($collection as $item) {
for($i = 0; $i < $item->getQtyOrdered(); $i++ ){
call_user_func_array(array($this, $callback), array_merge(array($item), $args));
}
}
Hope this helps future questions!

Codeigniter Pagination deleted row ~ index shifting issue

I'm showing 50 records per page. If 30 records have been deleted (or will not show for any other reasons) 1st page end index will 80 right? But second page index will be created 50 from pagination. So records between 50-80 indexes are shown both 1st and 2nd page.
How can I achieve this problem? Any ideas?
Feed the $config['total_rows'] variable in your pagination config the actual rows you want to show in view and it will calculate automatically, In other words count the total rows what you want to show, change the function of counting.
You must to recalculate current page based on max pages allowed after deletes:
Example, if your current page is $page, starting in 1:
$rowsByPage = 20;
$nrRows = ...count()...;
$maxPage = intval($nrRows / $rowsByPage) + 1;
then, adjust your page:
if($page > $maxPage) {
$page = $maxPage;
}

Magento - Get total number of items of a category in view.phtml

How can I get the total number of Items, I want to show it in the category view.phtml file. Usually this value is in the Toolbar.phtml.
I have tried something like this, but I think I am pretty far away:
$this->helper('catalog/output')->$_productCollection->count()
Magento version 1.7.0.2
The expected result should be something like this:
Items in this category: 17
The 17 should be the total number. If possible should include subcategory items.
Assuming that you want to display it in view.phtml you already have the current category object there so you can use $_category->getId()
$products_count = Mage::getModel('catalog/category')->load($_category->getId())
->getProductCount();
echo($products_count);
If you want to use it in list.phtml you can use
echo($_productCollection->count());
Try this,
<?php $_helper = $this->helper('catalog/output');?>
<?php $_category_detail = Mage::registry('current_category');?>
<?php $_category_detail->getName();?>
<?php $_category_detail->getId(); ?>
<?php $products_count = Mage::getModel('catalog/category')->load($_category_detail->getId())
->getProductCount();
echo($products_count);
?>
Basically, you can't show the total amount of filtered items in your view.phtml.
the reason is, that the logic, which gets the total amount, is not present in the $this context of the view.phtml.
But the logic is available in the Mage_Catalog_Block_Product_List_Toolbar block which is a child block of Mage_Catalog_Block_Product_List, though.
that basically means that you can actually get the total amount of filtered items by instantiating a toolbar and list block.
After doing that, the collection of the toolbar block has to be set with the value of the collection of the list block.
the following code is used in the view.phtml file to get the filtered total amount of items from the toolbar block:
$toolbar = new Mage_Catalog_Block_Product_List_Toolbar();
$list = new Mage_Catalog_Block_Product_List();
$toolbar-> setCollection($list -> getLoadedProductCollection());
$products_count = $toolbar -> getTotalNum();
There can be two ways we can find product count of a category.
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('level')
->addAttributeToSelect('entity_id');
foreach($collection as $cat)
$cat->getProductCount();
This will give you the product count for deepest category only. So for example, you have following categories. considering tree like structure.
Clothes(6)
->Cotton(3)
->Women(2)
The result returned from the piece of code given above.
Clothes(3)
Cotton(1)
Women(2)
There are three products directly associated with Clothes only, 1 with Cotton only and 2 with Women only. So it simply ignores the subcategories.
Another way is getting product count from products perspective.
$current_category = Mage::getModel('catalog/category')->load($cat->getEntityId());
$productCount = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('manufacturer',$this->manufacturer["id"])
->addFieldToFilter('visibility',4)
->addFieldToFilter('status',1)
->addCategoryFilter($current_category)->getSize();
This gives us the added benefit of filtering product attributes. However in the above scenario, the count returned will be slightly different.
It will return Clothes (6), as it has 3 products associated to itself and 3 more products to its sub categories. Similarly
Cotton(3)
Women(2).
So for efficient results it would be nice to use mix of both.
It's not right to load additional model inside view because you already have model instance from which you can retrieve Product collection.
$this->getCurrentCategory()->getProductCollection()->count();
it is very simple and it worked well for me its just one line of code
<?php echo Mage::registry('current_category')->getProductCount();?>
It will display the count of the products of current category

restricting items in cart and calculating

I'm currently trying to create a 'Sample' functionality on my Magento site.
The samples are free but when there more than 5 in the cart, then the total needs to be $10.
I can add the 'sample' products to the cart programatically that's no problem.
My main problem is:
Checking how many Sample products are in the cart (all have a value of $0)
If there are 5 or more, then the total needs to be $10+tax (or add this to the current total)
There cannot be more than 10 samples in the cart at one time(so no more than 10 $0 products)
Many thanks
To loop through the items in your cart you can loop through
foreach (Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems() as $item) {
if ($item->getPrice() == 0) {
//this item is a sample
}
}
To prevent things from adding to the cart you can overwrite the addProduct method in app/code/core/Mage/Checkout/Model/Cart.php to add your own custom logic to prevent products from being added to the cart when you don't want them. To make sure you are overriding correctly, you can learn more at http://alanstorm.com/magento_upgrade_rewrite_override

Resources