Get numbers of completed sales by product ID in Magento - magento

I have a .phtml file and i want to show all products of a category with the number of Completed sales and if possible the price x the number of sales so i can make a total for each product.
Now i have this code where i can see the product in a category and the product ID but i can't get it to work with the number of sales and the price.
<?php
$categoryid = 19;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <?php echo $_product->getName(); ?><?php echo $_product->getId(); ?>
<?php }?>
If someone could help me a little bit futher that would be great!
Thanks!

Take a look at table sales_flat_invoice_item (or sales_flat_invoice_item)
You could create a custom query e.g
SELECT * from sales_flat_invoice_item
WHERE product_id = YOUR_PRODUCT_ID
# GROUP BY order_id # if you have product with custom option

Related

Magento loop inside another loop sub categories with products of that sub category inside

I am trying to build a block in Magento (1.8 comunity edition)
In this block I want to seperate the sub categories
lets say that the parent is Power tools
and it has 3 sub categories
saws
routers
drills
so the outcome would be something like this...
Power tools
saws
routers
drills
I have the parent category set to display a block, and my existing code will show the sub categories just fine. I get the sub categories output along with their wrapping divs along with the links pointing to the corret subcategory page, but the product lists inside... no joy!
So far I have this for code...
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="" style="width: 90%; float:right;margin-right: 5%;">
<?php foreach ($categories as $category): ?>
<div class="col-xs-12" style="border: 2px dotted #3t6; font-size: 2em; margin:1em 0;">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
</div>
<div class="col-xs-12" style="height:200px; border: 1px solid #333;">
<h1>test this</h1>
<?php
$parent_cat = Mage::getModel('catalog/layer')->getCurrentCategory();
$_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php echo $parent_cat->getid() ; ?>//this shows the correct id
<?php foreach ($_productCollection as $_product): ?>// start individual products for the sub category
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<h1><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></
<?php endforeach; ?>// end product loop for the individual products
</div>
<?php endforeach; ?>
</div>
So I thought the the $this-> might be referencing the previous call for the outside loop (Not sure though if that makes sense) so I tried something like this...
<?php
$parent_cat = Mage::getModel('catalog/layer')->getCurrentCategory();
$_productCollection = $parent_cat->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
no errors but still no joy.
I am new to loops in Magento so I am totally flailing here, would like it if someone could set me straight on what I am doing wrong.
Thank you!
So digging around I found some stuff that worked for me, still not sure if this is the best way to do it, but for a novice it seems to work well.
much thans to Adam Moss for his answer on another post on stack
Magento Product Listing by Category ID
The only thing that I did was use the $category-getId(); to pull in the proper Cat id so that the sub category pulled the proper product collection!!! AWESOME!
<?php
$categoryid = $category->getId();
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<?php
// do stuff here, like...
echo $_product->getShortDescription();
?>
<?php } ?>

Display a specific subcategory product using PHTML

what should be the code in phtml file to show one specific subcategory product on home page ?
I have created a file in catalog/product/topSection.phtml with following code-
<?php
$cat_id = 42;
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = $category->getProductCollection();
echo 'total- '.$products->count();
foreach($products as $product){ ?>
<h3>
<img src="<?php echo $product->getImageUrl() ?>" alt="">
<p><?php echo $product->getName() ?></p>
</h3>
<?php }?>
and used
{{block type="core/template" name="top.section.home" template="catalog/product/topSection.phtml"}}
in home page content. but nothing is displaying. what is wrong in my code.
I checked your code it is working and also display product. First please check category id and sure category is enable ?

Magento Add filter by subcategory in tool bar

I'm trying to filter products listing with subcategory, Lets say i'm having a top level category named perfume and to subcategory named for him & for her. In top level category listing page i'm trying to include option named filter results it should show a dropdown as for men and for women
If user clicks for men it should show only products in for men category
To do this i tried with,
<?php $categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->addAttributeToSelect('position')
->addAttributeToSort('position', 'ASC')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load(); ?><?php foreach($categories as $key=>$category): ?><?php if($category->getName() != ''):?> <?php $prodCollection = age::getResourceModel('catalog/product_collection')->addCategoryFilter($category); // Magento product collection ?>
<?php echo $category->getName() ?> (<?php echo $prodCollection->count() ?>)<br/>
But it's showing all categories with product counts.
Did anyone know how to filter this to display subcategories only to current main category?
Thanks,.
<?php
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootCategoryId);
?>
<?php foreach($categories as $category): ?>
<?php echo $category->getName() ?><br/>
<?php endforeach ?>

Display pricing and add to cart button in related products Virtuemart 2.0

I would like to display the related product pricing and have add to cart button along with each of the related products.
Below is the code snippet from the related products page. The $field does not have any pricing available. How can I show the pricing and "add to cart" button? Thanks in advance
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?><div class="product-field product-field-type-<?php echo $field->field_type ?>">
<span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc) ?></span>
</div>
<?php } ?>
I have found solution here and it works for me:
no need to edit core files
It requires copying the "default_relatedproducts.php", "default_showprices.php" and "default_addtocart.php" to your "template/html/com_virtuemart/productdetails" folder. Then replace all of the code in the "default_relatedproducts.php" with the following code:
<?php
// Check to ensure this file is included in Joomla!
defined ( '_JEXEC' ) or die ( 'Restricted access' );
$model = new VirtueMartModelProduct();
$calculator = calculationHelper::getInstance();
$currency = CurrencyDisplay::getInstance();
?>
<div class="product-related-products">
<h4><?php echo JText::_('COM_VIRTUEMART_RELATED_PRODUCTS'); ?></h4>
<div>
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?>
<div class="product-field">
<?php
$product = $model->getProductSingle($field->custom_value,true);
?>
<h2><?php echo JHTML::link ($product->link, $product->product_name); ?></h2>
<a title="<?php echo $product->product_name ?>" rel="vm-additional-images" href="<?php echo $product->link; ?>">
<?php
echo $this->product->images[0]->displayMediaThumb('class="browseProductImage"', false);
?>
</a>
<div class="short_desc"><?php echo $product->product_s_desc; ?></div>
<?php include 'default_showprices.php'; ?>
<?php include 'default_addtocart.php'; ?>
</div>
<?php } ?>
</div>
</div>
Had a same problem. But I had to show only the price.
So the fastest way is to change sql select statement in customfields.php
Path in Joomla 2.5 for Virtuemart 2.0 administrator/components/com_virtuemart/models/customfields.php
line 548 of
public function getProductCustomsFieldRelatedProducts($product)
change only
$query=
with
'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value`
AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` ,
field.`custom_value`, field.`custom_param`, price.`product_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
LEFT JOIN `#__virtuemart_product_prices` AS price ON
field.`custom_value` = price.`virtuemart_product_id`
Where field.`virtuemart_product_id` ='.(int)$product->virtuemart_product_id.' and `field_type` = "R"';
After all on line 559 change
$field->custom_price
to
$field->product_price
And finally...
In template view of product description insert the code below to show the prices of related products
<?php echo $field->product_price ?>
The only problem with the below solution is that it doesn't display the correct images for the related products. It's using the main products image and just repeating it.

Magento getProductCount() on subcategory

the getProductCount() in the second level of category echo count print out 0, i try different way but i diddn't figure out like mage get collection etc, i didn'd find any solution at this question
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<?php foreach ($collection as $cat):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getThumbnailUrl();
?>
<div class="grid_4">
<div class="mineContent_grid_4">
<dl>
<dt>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<?php echo $cat->getName();?>
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>" width="173" height="208"/>
</a>
</dt>
<?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id);
?>
<dd>
<ol>
<?php foreach ($childLevel2Category as $catLevel2) { ?>
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
$count = $cur_category2->getProductCount();
?>
<li> <?php echo $catLevel2->getName();?> <span>(<?php echo $count ?>)</span></li>
<?php } ?>
</ol>
</dd>
</dl>
</div>
</div>
<?php endforeach;?>
You have this code:
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
$count = $cur_category2->getProductCount();
?>
This loads $cur_category2 with $cat->getId(), which is your parent category and not the current category. I think you want this:
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($catLevel2->getId());
$count = $cur_category2->getProductCount();
?>
Here's a snippet that should help you. All my snippet does is get the product count for the category. I hard coded a category ID of 4, but your code should work, getting the current category. You may want to isolate this code into a function to keep it simpler, then just reference it from your existing page. It essentially loads a collection of products, by category, filtering out products that aren't visible.
$_category = Mage::getModel('catalog/category')->load(4);
$collection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($_category);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
echo $collection->count();
try this example
$categories = Mage::getModel('catalog/category')->load(2)->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addCountToCategories($categories);
var_dump($categories);
where 2 - category id
also check php class Mage_Catalog_Block_Navigation in Magento

Resources