Get specific attribute on magento category view.phtml - php

I am trying to create a Asos style header on my Magento category page.
In this box I have pulled in the category title and the category description, I am also some how needing to pull in a specific attribute from the layered navigation into the category view.phtml page.
At the moment I have
<?php $prod = Mage::getModel('catalog/product')->load($productId);
$att = $prod->getResource()->getAttribute('product')->getFrontend()->getValue($prod);
echo $att;
?>
But it is just pulling in the word No instead of the attributes that it is showing in the layered navigation for this specific category.

Try this:
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Thanks to #Daniel Kocherga for the original answer here.

From code/core/mage/catalog/block/product/view/attributes.php
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
// if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
if (!$product->hasData($attribute->getAttributeCode())) {
$value = Mage::helper('catalog')->__('N/A');
} elseif ((string)$value == '') {
$value = Mage::helper('catalog')->__('No');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value)) {
$data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
}
}
}
return $data;
}
}
Pretty sure this is the responsible section for displaying a 'No' or 'N/A' with your attributes

I am not sure but see below URL. I think it is help full to you.
How to add attributes to product grid in category
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/add-attributes-to-product-grid
Magento Attributes: Using Different Filterable Attributes Per Category
http://www.human-element.com/Blog/ArticleDetailsPage/tabid/91/ArticleID/68/Magento-Attributes-Using-Different-Filterable-Attributes-Per-Category.aspx
Get Data for Magento Category Attribute on Product Page
http://spenserbaldwin.com/magento/get-data-for-new-magento-category-attribute

Related

Magento 2 Bundle price

I would like to ask you for some help or even just a comparison.
On my site I have bundles that are dimanically priced with select products, some with radio and some with default.
Magento 2 however shows two different prices between listing and product page. I have checked at code level and on the product page there is a manipulation via js that changes the price. In fact when the page loads you see a price before the update. Have you ever implemented some solution to make the same prices appear? This is the piece of code I am trying to implement but without success as it returns the same price because the products are set to default even though they are not selected on the product page.
$product = $this->productRepository->get($product_sku);
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$children_ids = $this->bundleType->getChildrenIds($product->getId(), true);
$bundle_items = [];
$optionsCollection = $product->getTypeInstance(true)
->getOptionsCollection($product);
foreach ($optionsCollection as $options) {
$optionArray[$options->getOptionId()]['option_title'] = $options->getDefaultTitle();
$optionArray[$options->getOptionId()]['option_type'] = $options->getType();
if ($options->getType() !== 'checkbox' && $options->getRequired() === "1") {
foreach ($selectionCollection as $selection) {
foreach ($children_ids as $bundle_id) {
if ((array_values($bundle_id)[0] === $selection->getEntityId())
&& $options->getId() === $selection->getOptionId()) {
$price = $selection->getPrice();
$qty = $selection->getSelectionQty();
$bundle_items[] = $price * $qty;
}
}
}
}
}
$finale_price = array_sum($bundle_items);
I finally solved it by creating a function that would return the tier price. Mine is a bit more customised because I have different prices depending also on the customer group. I'll leave the code.
public function getBundlePrice($product_sku, $product_sal)
{
$product = $this->productRepository->get($product_sku);
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$children_ids = $this->bundleType->getChildrenIds($product->getId(), true);
$bundle_items = [];
$optionsCollection = $product->getTypeInstance(true)
->getOptionsCollection($product);
foreach ($optionsCollection as $options) {
$optionArray[$options->getOptionId()]['option_title'] = $options->getDefaultTitle();
$optionArray[$options->getOptionId()]['option_type'] = $options->getType();
if ($options->getType() !== 'checkbox' && $options->getRequired() === "1") {
foreach ($selectionCollection as $selection) {
foreach ($children_ids as $bundle_id) {
if ((array_values($bundle_id)[0] === $selection->getEntityId())
&& $options->getId() === $selection->getOptionId()) {
$price = $selection->getPrice();
$qty = $selection->getSelectionQty();
if ($qty > 1 || $selection->getTierPrice()) {
$price = $this->getCustomPrice($selection);
} else if ($this->customerSession->getCustomer()->getGroupId() === "2") {
$price = $selection->getPartnerPrice();
}
$bundle_items[] = $price * $qty;
}
}
}
}
}
return $this->calculator->getAmount(array_sum($bundle_items), $product_sal);
}

show custom options in admin

I cannot seem to get the custom options field to display in my magento code below - can any of you good people help me out?
public function getProductName($product)
{
$value = '<b>'.$product->getname().'</b>';
if ($product->getproduct_type() == 'configurable')
{
//add sub products
$collection = mage::getModel('sales/order_item')
->getCollection()
->addFieldToFilter('parent_item_id', $product->getitem_id());
foreach ($collection as $subProduct)
{
$value .= '<br><i>'.$subProduct->getname().'</i>';
//add product configurable attributes values
$attributesDescription = mage::helper('ProductReturn/Configurable')->getDescription($subProduct->getproduct_id(), $product->getrp_product_id());
if ($attributesDescription != '')
$value .= '<br>'.$attributesDescription;
}
}
return $value;
}
There are issue in $product->getproduct_type() is not wrong it should try it $product->getTypeId()

Woocommerce - single product - show different content for different products

I'd like to show different content on single product page for different categories.
Sample code
<?php if(in_category('62')){ ?>Text for category 62<?php }?>
<?php if(in_category('21')){ ?>Text for category 21<?php }?>
Make the new template and name it: taxonomy-product_cat-Your_category_product_slug.php
Path: /wp-content/your-theme/woocommerce/taxonomy-product_cat-Your_category_product_slug.php
Another way to do this:
$cat1 = 62;
$cat2 = 21;
$terms = wp_get_post_terms($post->ID, 'product_cat');
if (!empty($terms)) {
foreach ($terms as $t) {
if ($t->term_id == $cat1) {
woocommerce_get_template_part('single-product-product1');
} else if ($t->term_id == $cat2) {
woocommerce_get_template_part('single-product-product2');
}
}
}

Mag custom options issue

I am using the following code to get and process the custom options
$orderIncrementId = $order->getIncrementId();
$orderReal = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$orderItems = $orderReal->getAllItems();
$sk = "";
foreach ($orderItems as $item)
{
$options = $item->getProductOptions();
foreach ($options as $option) {
if (is_array($option)) {
$firstElement = array_shift($option);
foreach ($option as $firstElement){
if (isset($firstElement['label']) && isset($firstElement['option_value'])){
if ($firstElement['label'] == 'Delivery Date') {
$deliveryDate = $firstElement['option_value'];
}
}
}
}
}
}
and my usage is to write it to CSV like this
if (isset($deliveryDate)) {
$outputString.= '"'.$deliveryDate.'",';
} else {
$outputString.= '"deliverydate error",';
}
now, I have made sure that
Label Delivery Date is available in data base "Catalog Product Option Title Table"
custom option Delivery Date has values in "Catalog Product Option
Table"
it looks like Delivery date is not being accessed via the script so my custom message 'deliverydate error' saves in CSV
in fact I am trying to debug mag extension that is not working properly...
to me, code is okay, but not sure why its not working....
can anybody guide me?
While it's not the perfect solution, this will return your custom option value for the ordered item, assuming that your product custom option is a type "field".
foreach ($orderItems as $item) {
$optionsArray = $item->getProductOptions();
if(isset($optionsArray['options'])) {
foreach ($optionsArray['options'] as $option) {
if($option['label'] == 'Delivery Date') {
$deliveryDate = $option['value'];
}
}
}
}

Add active class to current subcategory

With the code blow I retrieve the current parent category and his children. Is it possible to add an active class for the current child?
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
$loadCategory = $currentCat;
}
else
{
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<li>'.$cat->getName().'</li>';
}
}
?>
You already have the current category, so you can check it's ID against the IDs of the categories being looped through.
Where you have
echo '<li>'.$cat->getName().'</li>';
Change it to do the check for the ID and then add the "active" class when found
$class = '';
if ($currentCat->getId() == $cat->getId())
{
$class = ' class="active"';
}
echo '<li'.$class.'>'.$cat->getName().'</li>';
To set the category as active we have to set the current category in catalog layer.
$_categoryName=YourCategoryName;
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', $_categoryName);
Mage::getSingleton('catalog/layer')->setCurrentCategory($_category);
This looks better than ugly hack of changing in phtml filter..

Categories