After Update product attribute, vanishes tier prices - magento - php

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";
}
}

Related

How to change product name in the cart in Prestashop?

I want to change the names of some products in the cart. I was able to change the price of some products in the cart. But i can't change the names of some products in the cart.
How can i do that? Is it possible. Thanks for helps.
For your better understanding, below is some of the result from cart->getProducts() action.
The name of the products, as long as they are in the cart, is read from the product object, so you will have to override the cart->getProducts() method if you want to change their names dynamically.
But keep in mind that once a cart becomes an order, product name is copied / stored inside the orderDetail object, where you can freely intervene by renaming the "product_name" field once you know the original id_cart / id_order.
I solved the problem with the code below. If you encounter this problem, you can edit the code below for yourself. And you must do these actions after validateOrder function or validated the order.
$order = Order::getByCartId($cart->id);
$order_details = OrderDetail::getList($order->id);
foreach ($order_details as $order_detail) {
if ($order_detail['product_name'] === 'Installment' && (string)$order_detail['product_price'] == (string)$installment_fee) {
$order_detail_id = $order_detail['id_order_detail'];
}
}
if (!is_null($order_detail_id)) {
$order_detail = new OrderDetail($order_detail_id);
$order_detail->product_name = 'Changed product name';
$order_detail->save();
}

Prestashop get product attributes from its id

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!

Get product id and product type in magento?

I am creating magento store. I am beginner in magento. I want to get product id and product input type in my phtml file is this possible? please guide me..
I am trying to this way to get product type. but its not working for me
$product=Mage::getModel('catalog/product')->load($product_id);
$productType=$product->getTypeID();
Please guide me...
Try below code to get currently loaded product id:
$product_id = $this->getProduct()->getId();
When you don’t have access to $this, you can use Magento registry:
$product_id = Mage::registry('current_product')->getId();
Also for product type i think
$product = Mage::getModel('catalog/product')->load($product_id);
$productType = $product->getTypeId();
<?php if( $_product->getTypeId() == 'simple' ): ?>
//your code for simple products only
<?php endif; ?>
<?php if( $_product->getTypeId() == 'grouped' ): ?>
//your code for grouped products only
<?php endif; ?>
So on.
It works! Magento 1.6.1, place in the view.phtml
you can get all product information from following code
$product_id=6//Suppose
$_product=Mage::getModel('catalog/product')->load($product_id);
$product_data["id"]=$_product->getId();
$product_data["name"]=$_product->getName();
$product_data["short_description"]=$_product->getShortDescription();
$product_data["description"]=$_product->getDescription();
$product_data["price"]=$_product->getPrice();
$product_data["special price"]=$_product->getFinalPrice();
$product_data["image"]=$_product->getThumbnailUrl();
$product_data["model"]=$_product->getSku();
$product_data["color"]=$_product->getAttributeText('color'); //get cusom attribute value
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId) ->load($_product->getId());
$product_data["rating"]=($summaryData['rating_summary']*5)/100;
$product_data["shipping"]=Mage::getStoreConfig('carriers/flatrate/price');
if($_product->isSalable() ==1)
$product_data["in_stock"]=1;
else
$product_data["in_stock"]=0;
echo "<pre>";
print_r($product_data);
//echo "</pre>";
Item collection.
$_item->product_type;
$_item->getId()
Product :
$product->getTypeId();
$product->getId()
You can also try this..
$this->getProduct()->getId();
When you don’t have access to $this you can use Magento registry:
$cpid=Mage::registry('current_product')->getId();
IN MAGENTO query in the database and fetch the result like.
product id, product name and manufracturer
with out using the product flat table
use the eav catalog_product_entity and its attribute table
product_id product_name manufacturer
1 | PRODUCTA | NOKIA
2 | PRODUCTB | SAMSUNG
$product=Mage::getModel('catalog/product')->load($product_id);
above code not working for me. its throw exception;
This is working for me for get product details.
$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product_id);
So use for for product type.
$productType = $_product->getTypeId();
This worked for me-
if(Mage::registry('current_product')->getTypeId() == 'simple' ) {
Use getTypeId()

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.

Checking if a Magento product is a child of a configurable product

I have the following code to grab a list of Products
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name')
->addAttributeToFilter("category_ids", array('finset'=>$this->category_id));
foreach($collection as $product) {
echo $product->getName();
}
My question is, how can I NOT echo products that are 'simple' but belong to a parent 'configurable' product. (for example don't show "Red Shirt Medium" as it belongs to "Red Shirt")
I have worked out that this association lives in 'catalog_product_super_link' but I have only just started with Magento and unfortuantely don't know how to do the filtering :)
Cheers guys,
Chris.
I don't know a direct way to add this condition to the collection, I'd be interested in such a solution too. But you can always check inside the loop for each product:
if (empty(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId())))
{
echo $product->getName();
}
I've done something similar for our google feed.
This excerpt of code is what I use to check the products inheritance:
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('price', array('gt' => 0) );//price not 0
//$products->addAttributeToFilter('visibility', 4); //catalog, search - comment out to show all items (configurable products simple product breakdowns)
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
$prodIds=$products->getAllIds();
try {
foreach($prodIds as $productId) {
$product = Mage::getModel('catalog/product');
$product->load($productId);
// SIMPLE PRODUCTS
if($product->getTypeId() == 'simple' ) {
$prodName = trim($product->getName());
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($productId);
if(!$parentIds)
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);
if($parentIds) {
$parentProd = Mage::getModel('catalog/product')->load($parentIds[0]);
/*
* do something if this product has a parent or do some checks against $parentProd
*/
} // end parent check
}//if SIMPLE
} // foreach
} catch(Exception $e) {
die($e->getMessage());
}
There's a function called isConfigurable in the product class.
That may be of help to you.
$product->isConfigurable();
// if its the parent object it'll be true, if its the child it'll be false.
The quickest way might be to check if the product's visibility is set to "Not Visible Individually", since simple products associated with configurable products are typically set to this. Unfortunately I don't know the precise syntax but hopefully someone else who's willing to chime in does!
Since simple products that are part of configurable products usually have a visibility value of Not Visible Individually, it probably suffices to add a visibility filter to the collection that checks for visibility of the products in the catalog:
$collection->setVisibility(Mage::getModel('catalog/product_visibility')->getVisibleInCatalogIds());
In the unlikely circumstance that the resulting products are part of a configurable product, you can use the method Mage_Catalog_Model_Product_Type_Configurable::getParentIdsByChild to check if a product is used as part of a configurable product.

Categories