Prestashop get product attributes from its id - php

In Prestashop I want to get product's all attribute from its product id. Lets say I have a product with id as 3. Now from product id 3 I want to get all of its attribute like name, price, stock, category, product link.. etc.
So far I have tried
$product_id = $result['id_product'];
$id_product = (int)$product_id;
$product = new Product(Tools::getValue($id_product));
var_dump($product);
But its giving me an array with all attributes in blank. The array can be seen here
So can someone tell me how to get its all attribute from id? Any help and suggestions will be really appreciable. Thanks

Well, one obvious mistake is
Tools::getValue($id_product)
Which is basically
$_GET[$id_product]
So in your case that would probably evaluate to
$product = new Product($_GET['3']);
So remove Tools::getValue and try again:
$product_id = $result['id_product'];
$id_product = (int)$product_id;
$product = new Product($id_product);
var_dump($product);

or you can safe some lines, which is more simple:
$product = new Product( (int)$result['id_product']);
var_dump($product);
Good luck!

Related

Add Product Thumbnail to My Account - Recent Orders - Woocommerce

I was wondering if there is a way to add a product thumbnail into the buyer's 'Recent Orders' page in 'My Account' in Woocommerce frontend.
I've being trying to find some sort of solution, but no luck whats so ever.
I haven't tried anything to give you a code, just because I have no idea how to
actually go about this.
Would someone be able to point me to the right direction ?
You'll need to edit templates/myaccount/my-orders.php. Add the following code to where you want the thumbnails to show.
<?php
// Get a list of all items that belong to the order
$products = $order->get_items();
// Loop through the items and get the product image
foreach( $products as $product ) {
$product_obj = new WC_Product( $product["product_id"] );
echo $product_obj->get_image();
}
?>

After Update product attribute, vanishes tier prices - magento

I tried to update product's attribute called barcode as follows.
It update the product but cleared tier price of that product. Please help me.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $prod_sku);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
if ($product) {
$product->setBarcode($bar_code);
$product->save();
}
if(! $product->save()){
makes little sense as the ! is telling if NOT TRUE product must have been saved....but a successfull saved product is returning $this which is seen as true for the IF-clause without the expression mark.
I guess your problem is completely related to another technical problem...
EDIT: There seems to be some weird problem have a look at this...sounds strange but give it a try Magento product tier prices are deleted on product images update
I got an answer from Marius for my question in magento statckoverflow. Thanks Marius.I'm going to add it here for reference.
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$id = Mage::getModel('catalog/product')->getIdBySku($prod_sku);
$product = Mage::getModel('catalog/product')->load($id);
if ($product) {
$product->setBarcode($bar_code);
if(! $product->save()){
$productId = $product->getId();
echo "product_Id :: ".$productId." - Product sku :: ".$product->getSku()."<br />";
}else{
echo "not saved";
}
}

Using magento product model in footer

I am trying to display the Swatch attributes of all the associated simple products to the configurable product that the user is viewing.
I need to do this in the footer which is proving more difficult than I thought as a lot of methods etc are not available in the footer.
I have this code which just shows the Swatch attribute for the configurable product, I need this modified to show the Swatch attribute for all the simple products associated to this configurable.
<?php
$SKU = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
echo $product->getSwatch();
?>
$sku = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product->getTypeId() == "configurable"){
$childs = $product->getTypeInstance()->getUsedProducts();
}
Hope this helps! Just iterate over the childs and fetch the values.
cheers!

get quote's product attribute

Hi I'm trying to get a product attribute admin value or id (since it's multilanguage) from the cart items. I've tried many versions of code like this one:
$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
$_product = Mage::getModel('catalog/product')->load($item->getId());
$attribute = $_product->getAttribute('producttype');
}
But I only ever get false or null. Also how can I be sure to not get the store specific language value, but the attributes admin value/id? Maybe there's an even better way to read out the item attributes directly from the quote items without having to load the product first? Thanks in advance!
Solved with:
$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllVisibleItems() as $_item)
{
$_product = Mage::getModel('catalog/product')->load($_item->getProductId());
$attributeId = $_product->getProducttype();
}
and comparing by value ID instead of text.
If you need to get the product's value for a specific shop, while the items in the quote belong to a different store view, you can do the folowing:
$_product = Mage::getModel('catalog/product')
->setStoreId($adminStoreId)
->load($item->getId());
$value = $_product->getData('producttype');

get basket product id in observer

I've set an observer for sales_quote_add_item in order to clear the cart whenever a certain product is going to be added (it's only supposed to be ordered alone).
I'm just not sure on how to get the product ID of the product that is about to get added. With some trial & error I've come up with this:
$tmp = $observer->getEvent()->getQuoteItem()->getData();
echo $tmp['product_id'];
Which seems to be quite an ugly solution. I'm sure there is some shortcut or proper function to call for this, any ideas?
Your solution is good enough
$productId = $observer->getEvent()->getQuoteItem()->getProductId();
You may load product after if is needed
$product = Mage::getModel('catalog/product')->load($productId);
Use the following code to get product id of quote item data
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item)
{
$productId = $item->getProduct()->getId();
}
Hope this helps to you.

Categories