I'm trying to list up all the existing values of a newly created attribute in magento 1.7.0.2.
(and make them clickable links, so that on click they list up all of the items with the specific attribute value, but that's not a priority right now)
The attribute code is "artist"
So far i created the file Artist.php in app/code/core/Mage/Catalog/Block/ with the following code:
public function getAllArtists()
{
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'artist');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$artists = $attribute->getSource()->getAllOptions(false);
return $artists;
}
and the file artist.phtml in /app/design/frontend/default/template-name/template/catalog/product with this code:
<ul id="artist_list">
<?php foreach ($this->getAllArtists() as $artist): ?>
<li><?php echo $artist['label'] ?></li>
<?php endforeach; ?>
</ul>
which i then call in a static block with
{{block type="core/template" template="catalog/product/artist.phtml"}}
but nothing appears...
i used the code from this thread: http://www.magentocommerce.com/boards/viewthread/19982/P0/
the attribute is set "Visible on Product View Page on Front-end"
and i call the attribute value of each item in the ../template/product/view.phtml with
<?php echo $_product->getData('artist') ?>
and it correctly displays the value.
any ideas?
$name='whatever_your_attribute_name';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
$attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions(false);
print_r($attributeOptions);
Ref: Magento - get all attribute value
it will be just
$attribute = Mage::getModel('eav/config')->getAttribute(4,'artist');
if($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach($options as $key=>$value) {
if(count($value)==2) {
$artists[] = $value['label'];
}
}
}
Related
In a Magento 2.3.3 store I am trying to ouput the values of a multiselect custom attribute on a category page, but not having any luck.
I have set the attribute to be used in product listing and tried to output it on catalog/product/listing.phtml template page in my custom theme.
I am using the using the following code:
<?php /* #escapeNotVerified */ echo $_product->getResource()->getAttribute('custom_attribute')->getFrontend()->getValue($_product); ?>
This is working for dropdown attributes but not multi select attributes.
Kind of stuck on this...
You can use the below code to get MultiSelect Attribute value
create block in "catalog_product_view.xml"
<referenceBlock name="product.info.main">
<block class="Magento\Catalog\Block\Product\View" name="attribue.name" template="Magento_Catalog::product/view/attribute_name.phtml" after="-" />
</referenceBlock>
create "phtml" file under "Magento_Catalog::product/view/attribute_name.phtml"
<?php $product = $block->getProduct(); ?>
<div>
<?php
$data = explode(',',$product->getData('attribute_code'));
foreach($data as $value):
?>
<?php
$attr = $product->getResource()->getAttribute('attribute_code');
if ($attr->usesSource()):
?>
<?php
$option_value = $attr->getSource()->getOptionText($value);
?>
<p><?php echo $option_value; ?></p>
<?php endif;?>
<?php endforeach;?>
</div>
Here is an example of a code that returns "Multiselect Attribute Values". The attribute belongs to product entity. As it isn't a good idea to get ProductResource model from ProductModel and taking into account that probably you need to get it inside some template, just create a ViewModel and use this example there.
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
...
public function __construct(
...
ProductResource $productResource
)
{
...
$this->productResource = $productResource;
}
public function prepareProductAttributeOptions($product, $attributeCode)
{
$result = [];
$data = $product->getData($attributeCode);
$optionsIds = [];
if ($data) {
$optionsIds = explode(',', $data);
}
foreach ($optionsIds as $optionId) {
$attr = $this->productResource->getAttribute($attributeCode);
if ($attr->usesSource()) {
$option_value = $attr->getSource()->getOptionText($optionId);
$result[] = $option_value;
}
}
return implode(',', $result);
}
how to Show custom attribute dropdown option for a simple product in cart page
my attribute name is meal_time
<?php
$product = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $product->getAttributeText('meal_time');
?>
To get the custom attribute option in the cart , first we need to get quote visible product items and then load the product to get its options.
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
echo $_product->getMealTime();
}
To display all the option of an attribute
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$attribute = Mage::getModel('eav/config')-getAttribute('catalog_product', 'meal_time');
foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
echo $myattribute[$instance['value']] = $instance['label'];
}
}
If you want to display as select dropdown , use select tag including the label and value as options
Hi i am new to magento and i need to get the products from a particular category
for this i have use
<?php
$id1=4;
$category1 = Mage::getModel('catalog/category')->load($id1);
$collection1 = $category1->getProductCollection();
$collection1->addAttributeToSelect('name');
$collection1->addAttributeToSelect('description');
$collection1->addAttributeToSelect('image');
$collection1->addAttributeToSelect('producturl');
$collection1->addAttributeToSelect('prlce');
$products1 = $collection1->getItems();
$_helper1 = $this->helper('catalog/output'); ?>
<?php foreach ($products1 as $product1){ ?>
<?php echo $this->htmlEscape($product1->getPrice()) ?>
<?php } ?>
in this it is showing the name , image and url but when i am trying to echo price it doenot show anyhing.Please suggest me where i am doing mistake
This is how you do it:
<?php
// Test Params
$cat_id = 4;
$store_id = 1;
// Load Category
$category = Mage::getModel('catalog/category')->load($cat_id);
// Load Category Products
$categoryProducts = $category->getProductCollection();
// Iterate Through Product Collection
foreach ($categoryProducts as $categoryProduct)
{
// Load Product At Specified Store Id
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($categoryProduct->getId());
// Debug
print_r($product->getData());
// Get Price (e.g.)
echo 'Product Price: '. $product->getPrice()
}
?>
I am in need to get the Json Config by product id or sku in the product listing page for a particular category.
I can see that getJsonConfig is decalred in Product options block where only one particular product is showing thats why it can show Json Config without saying the product id. But I am in product list page.
Is there any way to get it like below?
$this->getJsonConfig($productId);
$this->getJsonConfig()
The method is part of
class Mage_Catalog_Block_Product_View
and it calls
/* #var $product Mage_Catalog_Model_Product */
$product = $this->getProduct();
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId()) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
return Mage::registry('product');
}
So to use it ( in any other page ) you have to set product in the block instance as below :
Mage::register('product', $_product); // add the product object in the registry
$block = Mage::getBlockSingleton('Mage_Catalog_Block_Product_View'); // Instantiate the product view block
echo $block->getJsonConfig();
I found a simple solution for configurable product view page.
Hope that will help.
if ($_product->isConfigurable())
{ $block1 = Mage::app()->getLayout()->createBlock('catalog/product_view');
$block1->setProduct($_product);
$configPrice = $block1->getJsonConfig(); //var_dump( $config = $block->getJsonConfig`enter code here`() );
$block2 = Mage::app()->getLayout()->createBlock('catalog/product_view_type_configurable');
$block2->setProduct($_product);
$config = $block2->getJsonConfig();
}
<?php if ($_product->isConfigurable()): ?>
<script type="text/javascript">
var optionsPrice = new Product.OptionsPrice(<?php echo $configPrice ?>);
</script>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $config ?>);
</script>
<?php endif; ?>
Within a Magento php Controller, how can I get a Product Collection containing the products listed in the logged in user's (ie current user's) Wishlist.
I am getting the Wishlist using:
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
and this contains the correct number of items.
But I would like to get a Product Collection. I have tried:
$productCollection = $wishList->getProductCollection();
and
$productCollection = $wishList->getProductCollection()->addAttributeToSelect('id')->load();
but the Product Collection I get has a length of 0.
How do I get the Product Collection?
You can use the getWishlistItemCollection (see link for more details) off the wishlist helper to return a collection of items, you then need to get the product from the item.
I have been using the following code to create an associative array of the products, which I then use to determine if a product I am displaying in the list page is in the wishlist...hopefully this will help:
public function getWishList() {
$_itemCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$_itemsInWishList = array();
foreach ($_itemCollection as $_item) {
$_product = $_item->getProduct();
$_itemsInWishList[$_product->getId()] = $_item;
}
return $_itemsInWishList;
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}