Magento getAttribute is not working in list.phtml? - php

This is magento code that i have used to get attribute.
echo $_product->getResource()->getAttribute('attrcode')->getFrontend()->getValue($_product);
This code is working fine in view.phtml it return to attribute code value.
when i write same code in list.phtml file this code is return blank.
where i do mistake. please help.

<?php echo $_product->getAttributeText('attrcode');?>
please use that code on list page and also check attribute setting in ('Used in Product Listing': "Yes")

Here is the code get attribute name and value that that doesn't belongs to any product
$attributeCode = 'YOUR_ATTRIBUTE_CODE';
$product = Mage::getModel('catalog/product');
$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeCode);
$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes
$options = $attribute->getSource()->getAllOptions(false);
print_r($options);

If you want to display all the values of a specific attribute in Magento 2 phtml file, then use following code
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product','attribute_code');
$values = $attribute->getSource()->getAllOptions();

Related

Magento duplicating a product programmatically is not displaying in the frontend

I am new to magneto I have tried to duplicate a product programmatically and I have succeeded problem is the duplicated product is showing in magneto admin side while in the frontend the product is not displaying below is my code could you please tell me what is the issue it will be very helpful for me. I have created a separate module for it below is my code.
class Magentotutorial_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$final = $_POST['value'];
$obj = Mage::getModel('catalog/product');
$_product = $obj->load($final);
$newProduct = $_product->duplicate();
$newProduct->setStatus(1);
$newProduct->setSku('value'.$final);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
$newProduct->getResource()->save($newProduct);
}
}
This function posted by you very well creates Duplicate product. However, it does not set the below attribute (due to which it's not VISIBLE in the frontend):
Navigate to Catalog > Manage Products > Duplicated Product > Inventory
Qty is 0 and Stock is "Out of Stock" - You will need to write below piece of code in the function to set this product to Stock: "In Stock" and Qty: [some default value] say, 100.
After the line which calls out $newProduct->setWebsiteIds($_product->getWebsiteIds());, you can insert the below lines:
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($newProduct->getId());
if ($stockItem->getId() > 0 && $stockItem->getManageStock())
{
$qty = 100; //set a default max value
$stockItem->setQty($qty);
$stockItem->setIsInStock((int)($qty > 0));
$stockItem->save();
}
You will need to run a Re-Index either Manually or Automate it
The product will display in the frontend. See Screenshot below:
[EDIT]
Use the below code and let me know if it works for you:
public function indexAction()
{
$productId = $this->getRequest()->getParam('value');
$productObject = Mage::getModel('catalog/product');
$_product = $productObject->load($productId);
$newProduct = $_product->duplicate();
$newProduct->setStatus(1);
//$newProduct->setName('Duplicate-' . $_product->getName());
$newProduct->setSku('value' . $productId);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
$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);
$indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexers as $indexer)
{
$indexer->reindexEverything();
}
}
Hope this helps.
Happy Coding...

Magento update product attribute programmatically on add to cart event

I have a product that get values from another script and I have a product attribute name custom_value, I want to set the values from another script programmatically. I tried different methods but could not get it done. I tried this, it didn't worked
$quoteItem = $observer->getItem();
if ($additionalOptions = $quoteItem->getOptionByCode('stamp_design')) {
$orderItem = $observer->getOrderItem();
$options = $orderItem->getProductOptions();
$options['stamp_design'] = unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
I also tried this and it also didn't worked.
$product = $observer->getEvent()->getProduct();
// Write a new line to var/log/product-updates.log
$name = $product->getName();
$sku = $product->getSku();
$orderItem = $observer->getEvent()->getItem();
$options = $product->getProductOptions();
var_dump($options);
$options['custom_value'] = $_SESSION['generated_value'];
$product->setProductOptions($options);
$options = $product->getOptions();
They are giving Fatal error: Call to a member function xxxx on a non-object.
Can everybody provide me some solution, Magento version is 1.7 thanks.

Magento get available attribute set id's and names from external script

I have tried all sorts of things and I'm not getting anywhere. Please would someone show me how to get the name and id of all available product attribute sets? One will be 'Default'...
I'm building a custom quoting system and need to pull in the attribute sets so that users can select that first then load up the products that are assigned to that set.
Many thanks for your help.
You can load the attribute sets with:
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection') ->load();
Iterate:
foreach ($attributeSetCollection as $id=>$attributeSet) {
$entityTypeId = $attributeSet->getEntityTypeId();
$name = $attributeSet->getAttributeSetName();
Mage::log("ATTRIBUTE SET :".$name." - ".$id);
}
You can then load your collection by attribute set.
So As you are trying to get the attributeSets which are showing in manage attribute set section of admin you can follow the below coding:
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();//->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType);
$allSet = array();
foreach($collection as $coll){
$attributeSet['name'] = $coll->getAttributeSetName();
$attributeSet['id'] = $coll->getAttributeSetId();
$allSet[] = $attributeSet;
}
echo "<pre>";
print_r($allSet);
echo "</pre>";
?>
Click here! For more reference.
If you want to get an Attribute set selector in your Magento Admin System > Configuration this class will be useful:
class CompanyName_ModuleName_Model_System_Config_Source_Catalog_Product_Attributeset
{
protected $_options = array();
public function toOptionArray()
{
if (!count($this->_options)) {
$entityTypeId = Mage::getResourceModel('catalog/product')->getTypeId();
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter($entityTypeId);
foreach ($attributeSetCollection as $_attributeSet) {
$this->_options[] = array(
'value' => $_attributeSet->getId(),
'label' => $_attributeSet->getAttributeSetName()
);
}
}
return $this->_options;
}
}
These attribute sets are limited by catalog_product entity type.
Indeed you will need the field in your system.xml like this:
<select_attribute_set translate="label">
<label>Default Attribute Set for new importing products</label>
<frontend_type>select</frontend_type>
<source_model>companyname_modulename/system_config_source_catalog_product_attributeset</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
</select_attribute_set>

How to display customer attributes in a drop down list using a custom module

I have created a custom tab in admin which I need to manage customer attributes.
I would like to load all customer attributes in that section and keep a check box against each attribute.
Such that when ever a check box is checked or unchecked, based on the selection, the columns will be displayed/hidden in Manage Customer Grid.
I would like to know how do I get to display all customer attributes in that section with a checkbox each?
You can use getAttributes() which returns an array of Mage_Eav_Model_Entity_Attribute.
<?php
$attributes = Mage::getModel('customer/customer')->getAttributes();
foreach ($attributes as $attr) :
?>
<input type="checkbox" name="attributes[]" value="<?php echo $attr->getId() ?>" id="attribute-<?php echo $attr->getId() ?>" />
<label for="attribute-<?php echo $attr->getId() ?>"><?php echo $attr->getStoreLabel() ?></label>
<?php endforeach; ?>
Much improved technique for config:
First we need to create a source model in your module, in the following you obviously have to rename it to match your actual module.
class Your_Module_Model_Source_Customer_Attribute
{
public function toOptionArray()
{
$attributes = Mage::getModel('customer/entity_attribute_collection')
// remove filter to allow default address ID, etc.
->addVisibleFilter();
$result = array();
foreach ($attributes as $attribute) {
if (($label = $attribute->getFrontendLabel()))
$result[$attribute->getId()] = $label;
}
return $result;
}
}
Then we need a single new field in your module's system.xml.
<fieldname translate="label">
<label>Customer Attributes</label>
<frontend_type>checkboxes</frontend_type>
<source_model>yourmodule/source_customer_attribute</source_model>
<show_in_default>1</show_in_default>
</fieldname>
This works surprisingly well, surprising because these classes are not used in the core. Instead of checkboxes the type can also be radios, that works too.
Figured it out!
That's the final code. Obviously BIG thanks to you for your help with the code at the first place.
$attributes = Mage::getModel('customer/entity_address_attribute_collection');
$result = array();
foreach ($attributes as $attribute)
{
if (($label = $attribute->getFrontendLabel()))
$result[$attribute->getId()] = $label;
}
$attributes1 = Mage::getModel('customer/entity_attribute_collection');
$result1 = array();
foreach ($attributes1 as $attribute1)
{
if (($label1 = $attribute1->getFrontendLabel()))
$result1[$attribute1->getId()] = $label1;
}
$final = array_merge($result, $result1);
return $final;

Get Customer Name for Coupon Codes in Magento

I have searched on the net and on this site for a solution but couldn't find any.
I would like to get the customer name associated to a coupon code in Magento. I found some code for the times the coupon code was used but I couldn't get the customer name.
I tried the following code:
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app( 'admin' );
$coupon = Mage::getModel('salesrule/coupon/usage');
$coupon->load('dd_38WX9N', 'code');
if($coupon->getId()) {
$timesUsed = $coupon->getTimesUsed();
$customer = $coupon->getCustomerId();
echo $timesUsed;
echo $customer;
}
?>
Could someone please help me with that and point me to the right direction.
Thanks a lot for your help.
Daniel
Here is the good solution. you need to use the resource and not the model to get access to the table salesrule_coupon_usage and get the customer_id list
/*#var $coupon Mage_SalesRule_Model_Coupon */
$coupon = Mage::getModel('salesrule/coupon');
$coupon->load('dd_38WX9N', 'code');
if($coupon->getId()){
/* #var $resourceCouponUsage Mage_SalesRule_Model_Mysql4_Coupon_Usage */
$resourceCouponUsage = Mage::getResourceModel('salesrule/coupon_usage');
$read = $resourceCouponUsage->getReadConnection();
$select = $read->select()
->from($resourceCouponUsage->getMainTable())
->where('coupon_id = ?', $coupon->getId());
$data = $read->fetchAll($select);
print_r($data);
foreach($data as $couponUsage){
$customer = Mage::getModel('customer/customer')->load($couponUsage['customer_id']);
echo $customer->getName();
}
}
When you are able to get the ID of the customer, you also can get the whole customer object:
$customer_data = Mage::getModel('customer/customer')->load($customer_id);
print_r($customer_data);
There you can get the name as well.

Categories