I have amazon import script in my magento store. And each product has "Amazon Import Products" inside Product Information (Near General, Prices, Meta Information, Images...).
What I'm trying to get using PHP is located in "Amazon Import Products" And the value of "Amazon Product URL"
Here is my code to choose the product by SKU:
$sku = $id;
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$amazonlink =
Can someone help? The only thing I can find online is how to get product name or images etc but not how to get custom attributes? Also this speed sensitive so I would like to fetch it by the name and not to loop through all the attributes
Try
$sku = $id;
$_product=Mage::getModel('catalog/product')->load($sku, 'sku');
$amazonlink = $_product->getData('custom_attributes_code_here');
// or
$amazonlink = $_product->getCustomAttributesCodeHere();
Below is the safest way to get custom attribute value.
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}
Above code is explained in my blog post here
Getting custom attribute value in Magento
Related
I'm using Magento 2, when I try add product in Backend, in the Configurations tab, I have created Configuration and I saw there three attributes.
How can I get them in a module on frontend?
I see they are stored in eav_attribute table but I dont know which SQL can be do it, because it has no conditional column
Thank so much!
Use can use below code to get attribute in your module frontend.
$attribute = $objectManager->create('\Magento\Eav\Model\Config')->getAttribute('catalog_product', 'color');
$colorAttributeId= $attribute->getAttributeId();
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
$colors[$option['value']] = strtolower($option['label']);
}
print_r($colors);
I've got an instance of magento 1.7 CE running, and a second site that calls it via the SOAP api v2 from php.
I can't seem to find out how to add an array of products (given by productId or SKU) to a cart and then redirect to the cart page.
I've tried adding the items to the cart via shoppingCartProductAdd, which works, however I can't find out how to then open that cart back on magento.
I've also tried directly formulating a link that passes products via GET, however this only works for a single product ( checkout/cart/add?product=[id]&qty=[qty] ), for my purpose a whole array of products needs to be passed before redirecting to magento.
Any ideas?
Figured it out.
Basically one can use a link shaped like
http://example.com/checkout/cart/add?product=1&related_product=2,3,4,5
To fill the shoppingcart with products with id 1 .. 5 and then go to the cart in magento.
In my case I generated the link like this
if(!isset($session)) {
$client = new SoapClient('http://example.com/index.php/api/v2_soap?wsdl=1');
$session = $client->login('username', 'Qq314asdgUScrncfD7VMb');
}
if(!isset($cart)) {
$cart = $client->shoppingCartCreate($session);
}
$ids = array();
foreach($items as $id) {
$result = $client->catalogProductInfo($session, $id." ", null, 'sku');
$ids[] = $result->product_id;
}
$this->Session->delete('Cart');
$this->redirect('http://example.com/checkout/cart/add?product='.$ids[0].'&related_product=' . implode(array_slice($ids, 1), ','));
The question is simple.
If I know the sku of my product and nothing else, how can I retrieve the url to that item. This is commonly useful for third party integration where the unique id at the remote service wont match a product id. Or maybe you want to make a search box to search by sku only.
You can just do this:
$sku = 'ecco'; // SKU you want to load. 'ecco' is a sku in the Magento demo data
$url = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku)->getProductUrl();
echo $url;
Also.... You could add this to a file named 'geturlbysku.php' in magento root dir. This code enables a small amount of json representing the product to be pulled easily, enabling small javascript integration. Product URL is part of the dataset
<?php
require_once '/app/Mage.php';
umask(0);
Mage::app('admin');
$actual_link = "http://$_SERVER[HTTP_HOST]";
$sku = $_GET['sku'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$fullUrl = "/catalog/product/view/id/" . $product->getId();
header("Content-Type: application/json");
echo $_GET['callback'] . '('.json_encode($product->getData()).')';
?>
Use like this...
www.yoursite.com/geturlbysku.php?sku=theskuoftheproduct
Part of the data returned includes "url_path":"your-product-name.html" which corresponds to the real url.
You can use the load method. This is quicker and easier than all other solutions.
Mage::getModel('catalog/product')->load('sku', 'sku');
For more information on loading products Click Here
$product = Mage::helper('catalog/product')->getProduct($this->getData('sku'), Mage::app()->getStore()->getId(), 'sku');
$url = Mage::getUrl($product->getUrlPath());
You can access the full url of the product based on its sku by adding this to a method and then setting the sku attribute on the class to the sku that you wish to search by.
I am trying to write a custom module which is capable of duplicating a product into multiple products with only varying SKU. I have tried using function duplicate() under /app/code/core/Mage/Catalog/Model/Product.php in my custom module. But its not working.
I am using the below code in my custom Obesrever.php file to duplicate, but duplication is not occuring
$product = $observer->getEvent()->getProduct();
$newProduct = $product->duplicate();
can anyone suggest me any links to do this or any code format would be helpful.
Thanks
It would be great if you can post the complete function that you are trying to debug or create duplicate products and config.xml (where you are trying to call the event).
Below code works for me in CE 1.9.2.2 without any problems. This function does the following tasks:
Creates duplicate of the original product
Sets the stock to "In Stock" and Qty to "100" (hard coded for now)
Automates reindexing
public function indexAction() //change the function name
{
$productId = $observer->getEvent()->getProduct()->getId();
$productObject = Mage::getModel('catalog/product');
$_product = $productObject->load($productId);
$newProduct = $_product->duplicate();
//new product status is disabled - to view in the frontend you will need to set the status as enabled
$newProduct->setStatus(1);
$newProduct->setName('Duplicate-' . $_product->getName());
$newProduct->setSku('value-' . $productId);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
//set the product stock and qty to display product in the frontend
//while creating duplicate product, it will update the new product to have qty 0 and out of stock
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($newProduct->getId());
if ($stockItem->getId() > 0 && $stockItem->getManageStock())
{
$qty = 100;
$stockItem->setQty($qty);
$stockItem->setIsInStock((int)($qty > 0));
$stockItem->save();
}
$newProduct->getResource()->save($newProduct);
//automate reindexing - to display the product in the frontend
$indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexers as $indexer)
{
$indexer->reindexEverything();
}
}
Hope this helps.
Happy Coding...
can someone please tell me how to check if an attribute has been created?
I have a block which displays products based on a given attribute.
The thing is if the attribute has not been created, I'm getting an error on the frontend.
Is there a way to check if the attribute exists?
Thanks.
Hopefully you can get lots of info about attributes and attributes set in the following article:
http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/
Thanks
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
Try above code which means check the status that is specified attribute available in magento ( if you know the id ).
Try this as well :
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront()) {
$value = $attribute->getFrontend()->getValue($product);
// do something with $value here
}
}