How to add a product to it? - magento

<?php foreach($this->getItems() as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach ?>
the code is form cart.phtml if i want to add a product into $this->getItems() which from a specified category then output it? how do i do? thank you
i should know what information of the product, then i can put it into $this->getItems() then loop out the information?

The way to accomplish this would be to add the item to your cart using either method below.
Adding a Product to the Cart via Querystring read more
/path/to/app/checkout/cart/add?product=[id]&qty=[qty]
Add product to cart programmatically (read more)
`
$params = array(
'product' => 23,
'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product');
$product->load(23); // load product id 23
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
`

Related

Magento - Display product count views?

I am using magento 1.9 and i am trying to display the views of the product in the product view page.
Here is what code i am using right now but it's not displaying anything:
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?PHP echo $_product->getViews();?>
Is it possible to display the number of the views of the displayed product or where i am doing wrong.
Thanks in advance!
You could create an observer for catalog_product_load_after and add views to the $_data array with
$product = $observer->getEvent()->getProduct();
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$select = $read->select()
->from(
$resource->getTableName('log/url_info_table'),
'COUNT(*) views'
)
->where('url LIKE "%catalog/product/view/id/' . $product->getId() . '"');
$result = $read->query($select)->fetch();
$product->setData('views', $result['views']);

Product collection by attriubte base in magento

I want to get the product collection in magento.For that i use some code but i think this code is not what i need.I want to get collection on attribute base.I got some products but it didn't match to those products that are filters from advance results for that attribute.Means different results from my collection and advance search results.Also the product url is not valid one.May be someone know where is the problem ? thanks in advance.My code is :
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter(array(
array('attribute'=>'manufacturer','eq'=>'23'),
));
foreach ($collection as $product) {
?>
<div class="brand_name">
<p>Audi</p>
<?php echo substr($product->getName(),0,10);?>
</div>
<?php } ?>
You have used 2 array in the field to filter. Try with one.
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter('attribute'=>'manufacturer','eq'=>'23');
It should work correctly:
$attrToSelect = '*'; // or Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect($attrToSelect)
->addAttributeToFilter('manufacturer', 23)
;
foreach ($collection as $product) {
echo $product->getProductUrl();
}
Also check in admin - Catalog->Attributes->Manage Attributes - Used in Product Listing set Yes.
please use addAttributeToFilter('manufucture',23)
instead of
adfieldtofilter

unable to get product price in custom file magento

Hi i am new to magento and i need to get the products from a particular category
for this i have use
<?php
$id1=4;
$category1 = Mage::getModel('catalog/category')->load($id1);
$collection1 = $category1->getProductCollection();
$collection1->addAttributeToSelect('name');
$collection1->addAttributeToSelect('description');
$collection1->addAttributeToSelect('image');
$collection1->addAttributeToSelect('producturl');
$collection1->addAttributeToSelect('prlce');
$products1 = $collection1->getItems();
$_helper1 = $this->helper('catalog/output'); ?>
<?php foreach ($products1 as $product1){ ?>
<?php echo $this->htmlEscape($product1->getPrice()) ?>
<?php } ?>
in this it is showing the name , image and url but when i am trying to echo price it doenot show anyhing.Please suggest me where i am doing mistake
This is how you do it:
<?php
// Test Params
$cat_id = 4;
$store_id = 1;
// Load Category
$category = Mage::getModel('catalog/category')->load($cat_id);
// Load Category Products
$categoryProducts = $category->getProductCollection();
// Iterate Through Product Collection
foreach ($categoryProducts as $categoryProduct)
{
// Load Product At Specified Store Id
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($categoryProduct->getId());
// Debug
print_r($product->getData());
// Get Price (e.g.)
echo 'Product Price: '. $product->getPrice()
}
?>

Need to update custom option value in shopping cart page

I have a small problem with my shopping cart page.
In my shopping cart have these fields.
Quantity
Product description. (like 1 product $27)
My problem is,
If, I update the the quantity of my shopping cart item. For example, I will update 1 quantity as 3 quantity. And need to change the product description also. From 1 product $27 to 3 product $81.
This my sample code:
<?php foreach ($_options as $_option) : ?>
<?php $_formatedOptionValue = $this->getFormatedOptionValue($_option) ?>
<?php //if($ii > 2) { ?>
<dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
<dd style="padding-left:0px;" <?php if (isset($_formatedOptionValue['full_view'])): ?> class="truncated"<?php endif; ?>>
<?php $license_val = $_formatedOptionValue['value']; ?>
I get the option value from backend by using this $_formatedOptionValue['full_view']. So only, I much confused.
Now, I am using custom option for the product description. I need to update the custom option value. Is there any possible? Please help me to solve this problem.
Thanks
Isn't it much simpeler to retrieve the qty and the subtotal and echo these values in a translate string, you already have the data after the update for the items.
$this->__('%s product %s', $qty, $subtotal)
Its so simple just override your checkout controller and then edit updateCheckoutPost function for update custom option value, with price.
Add this code only in updateCheckoutPost function 100% working.
you need cart items id's, item's product id, qty or options array of particular product.
foreach($_POST['item_ids'] as $update_product_id){
$product = Mage::getModel('catalog/product')->load($_POST['product_ids'][$update_product_id][0]);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product' => $_POST['product_ids'][$update_product_id][0],
'qty' => $_POST['cart'][$update_product_id]['qty'],
'options' => $_POST['option'][$update_product_id]);
$cart->updateItem($update_product_id,$params);
$cart->save();
}

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

Resources