Magento: get block of product view - magento

I instanciate the following block by using the operator new but when I am going to use getProduct() it does not return anything because it doesn't have the ID of the product. How can I add it?
$block = new Mage_Catalog_Block_Product_View_Options();
Thanks in advance.

Try this:
$product_id = Mage::registry('current_product')->getId();

Related

Add products to order programmatically magento admin

I have customized my create order page and in that after selecting customer and store id I have information of the product(Ex product A), I want that after admin reaches the sales_order_create then there must be that product(Product A) in the 'Items Ordered' list by default.
Thanks in advance
One of the solutions is to add an observer for the event called "create_order_session_quote_initialized". In the observer you need to add something like this:
$session = $observer->getSession();
if ($session->getProductAdded()) {
return $this;
}
$product = Mage::getModel('catalog/product')->load($productId);
$session->getQuote()->addProduct($product);
$session->setProductAdded(true);
return $this;
It's not the exact code which already works. But I hope that this will help.
First we need to initialize the quote with product(Product A).
$quoteItem = Mage::getModel('sales/quote_item')
->setProduct($product)
->setQuote($this->getQuote())
->setQty($qty)
->setPrice($product->getPrice())
->save();
Convert this quote item to order item.
$orderItem = Mage::getModel('sales/convert_quote')
->itemToOrderItem($quoteItem)
->save($this->getOrder->getId());

How to add custom product on catalog search collection?

Currently i am trying to add custom product on CatalogSearch product collection . Currently i am working on Mage_CatalogSearch_Model_Layer. I know that its core part but after working my code properly i manage with override CatalogSearch_Model.
Currently i am working on this method
public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
// my custom code start
$collection2 = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array(1,2));
$collectiondata=$collection2->getData();
foreach($collectiondata as $customdata)
{
$collection->addItem($customdata);
}
return $this;
}
this code add my custom product to collection but issue start when when in search result no product found.Means if in search product found then it add my custom product otherwise not.
I want to know where i am making mistake ? or any step missing. Any help will be appreciated
Thank you
here the issue:
$collection2 = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array(1,2));
foreach($collection2 as $customdata)
{
$collection->addItem($customdata);
}
$collection2->getData(); is give not proper object for $customdata.
$customdata is give proper object of Mage_Catalog_Model_Product

magento get custom attribute label

I've a problem, I want to show the label of a custom attribute in the product page.
I explain me better, starting from this link because is what I want to do:
http://www.customy.com/blog/how-to-display-video-on-magento-product-page/
I want a video product in the sidebar of the product page, so I create a new custompage.phtml and i put this in the sidebar from catalog.xml, in my custompage.phtml I put this code to have the custom label:
getResource()->getAttribute('video')->getStoreLabel();?>
but I have this error:
"Fatal error: Call to a member function getResource() on a non-object in ..path//"
I have try different code but still have this problem.
I think that I forget to put something in my .phtml but I'm new of Magento and I don't know what!
Thank in advance!
Since $_product didn't work then you'll need to load the object before attempting to access the attribute. Try this:
$product_id = Mage::registry('current_product')->getId();
$_product=Mage::getModel('catalog/product')->load($product_id);
echo $_product->getResource()->getAttribute('video')->getStoreLabel();
If you don't have access to the product model, I wrote a small query to get it from DB. This could be done better, but should be a decent starting point for your class:
protected $_dbConn;
public function __construct()
{
$this->_dbConn = Mage::getSingleton('core/resource')->getConnection('core_read');
}
public function getAttributeLabel($code)
{
$query = "
SELECT b.value
FROM eav_attribute a
JOIN eav_attribute_label b
ON a.attribute_id = b.attribute_id
WHERE a.attribute_code = '".$code."'";
return $this->_dbConn->fetchOne($query);
}

How to update an attribute value in Magento?

I've created an attribute called "popularity" which will be used to organize the catalog by most viewed products. The idea is get its value on product view template (view.phtml on my theme) and increment that value and update it, however I don't know how to make this update, I mean, how to save the new value incremented into database. I've put this on the product view page, take a look:
$popularity = $_product->getData('popularity');
$popularity++;
The point is that $popularity is not being saved with the new value into database. How can I save that value?
I put the code below on product page template (mytheme/template/catalog/product/view.phtml) but is not working:
$from_date = '2010-01-01';
$to_date = now();
$product_ids = $_product->getId();
$viewed = Mage::getResourceModel('reports/product_collection')->addViewsCount($from_date, $to_date)->addFieldToFilter('entity_id', $product_ids);
foreach($viewed as $view) {
$count_views = $view->getData('views');
$product_model->setData('popularity', $count_views)->getResource()->saveAttribute($product_model, $count_views);
}
Thanks for help!
Try this:
$popularity = $_product->getData('popularity');
$popularity++;
Mage::getSingleton('catalog/product_action')
->updateAttributes(array($_product->getId()), array('popularity', $popularity), 0);
This will work, if the scope of your attribute is GLOBAL. If it's something else then you can use this:
Mage::getSingleton('catalog/product_action')
->updateAttributes(
array($_product->getId()),
array('popularity', $popularity),
Mage::app()->getStore()->getId()
);

How get cart content ? (prestashop)

I'd like get cart content on prestashop os-commerce. How can do it ?
You should take a look at the Cart class, located in classes/Cart.php. There's a method called getProducts().
/**
* Return cart products
*
* #result array Products
*/
public function getProducts($refresh = false, $id_product = false)
{
// code...
}
Hope this helps,
Br,
Simply use
Context::getContext()->cart
refer this link
https://www.prestashop.com/forums/topic/440516-how-to-get-all-product-ids-in-current-cart/
It's Working: Enjoy
in module hook you can use this:
$products = $params['cart']->getProducts(true);
If you are using a hook on Shopping Cart page you can use:
$products = $params['products'];

Resources