Limit Product Title Characters before cut-off in Magento - magento

I'm having trouble with my magento store's layout. Some products with long titles would get cut off with a '...' but sometimes the title won't get cut off the title and instead throw out my layout's positioning. I have attached a photo
Photo of issue
I have tried the substr function in app/design/frontend/interface/theme/template/catalog/product/view.phtml
but it just ended up cutting the product's name in the actual page of the product, not the full view like the image above shows. I just need the '...' to happen without as many characters to prevent it going to a new line and ruining the layout.

Use this trick in PHP, use substr function :
Go to template > catalog > products > list.phtml file and update product name code with this code
<?php
// define the maximum length of the product name here
$maxLength = 10;
$productName = $_helper->productAttribute($_product, $_product->getName(), 'name');
echo substr($productName, 0, $maxLength).'...';
?>

Related

Magento - Can you show stock levels when a configurable product selection has been made

When a variation has been selected I want it to then display how many of that selection is in stock. For example for a t-shirt i select colour red and size large it would then say 4 in stock in this variation.
I don't mind whether its editing code in default.phtml or adding an extension.
Thanks.
You could do something like this:
First load the product and stock models respectively:
//Load product Model
/* Via SKU */
$sku = productSKU; //Pass the simple product SKU here
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
// Via ID: $_product = Mage::getModel('catalog/product')->load($id);
//Stock Model
$_stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
And then get the quantity from stock model using this:
$_productQTY = $_stock->getQty();
If you want to show the results instantly you could integrate some AJAX calls.

How to display color and size in a simple product in Magento

I'm developing a Magento store and I need to display the color and size of a simple product in front end, is it possible?
I got it only with configurable product but not with simple product.
Something like this:
http://www.saiajustamodafesta.com.br/loja/vestido-longo-saia-justa.html
Anybody can help me?
Thanks.
in your frontend/[your-package]/[Your-theme]/template/catalog/product/view.phtml file
$color = $_product->getColor();
<img src="<?php echo $this->getSkinUrl('images/'.$color.'.jpg'); ?>">
you need to upload jpg image same as color name ex red.jpg as well as same as size
Get color image attribute
$color_code="color";
$colorValue = $objectManager->get('Magento\Catalog\Model\Product')->load($_product->getId())->getData($color_code);
$swatchHelper=$objectManager->get("Magento\Swatches\Helper\Data");
$swatchData = $swatchHelper->getSwatchesByOptionsId([$colorValue]);
<img class="object-fit-contain w-100 h-100" src="<?=
$objectManager->get('Magento\Store\Model\StoreManagerInterface')
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).
'attribute/swatch'.$swatchData[$colorValue]['value']?>" alt="color"/>
For Magento 2 if you have got quote item or a product object that is doesn't contain color attribute:
$product = $_item->getProduct();
$product->getResource()->load($product, $product->getId(),['color']);
//then you can get attribute data
$value = $product->getColor(); //52
$label = $product->getResource()->getAttribute('color')->getFrontend()->getLabel(); // Color
$text = $product->getResource()->getAttribute('color')->getFrontend()->getValue($product); //Orange
in your view.phtml getColor(); ?> replace with any attribute code, the important here is that the attribute must be on the catalog_product_flat tables, in order to do that, your attribute must be defined as "use_in_product_listing". Greetings.

Magento toolbar not rendering pager

I'm new to Magento and I'm having a hard time trying to figure out how to display the pager inside the toolbar when it is called from the catalog/category/view.phtml file. Here's the code I'm using :
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$pager = $layout->createBlock('catalog/html_pager');
$block = $layout->createBlock('catalog/product_list');
$block->setCategoryId($_category->getId());
$block->setChild('toolbar', $toolbar);
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
echo $toolbar->renderView();
The Sort By, Show items per page and items total show approprietly, but the pager is just not rendering.. Anyone know what I'm doing wrong? Any help would be greatly appreciated.
You've got two problems I can immediately spot
There's no such block type as catalog/html_pager (did you mean page/html_pager)
The toolbar block's getPagerHtml method looks for a child block named product_list_toolbar_pager. You've not inserted, appended, or set this child.
Instantiate pager block with something like this
$pager = $layout->createBlock('page/html_pager');
and insert it into the toolbar with
$toolbar->setChild('product_list_toolbar_pager', $pager);
and you may have better results.
Also, the pager template itself (frontend/base/default/template/page/html/pager.phtml) contains code that will surpress the page if there's only one page of results . Drop in some debugging around this if clause.
<!-- File: app/design/frontend/base/default/template/page/html/pager.phtml -->
<?php if($this->getLastPageNum()>1): ?>
to make sure you're not falling afoul of small category listings.

why don’t my (Configurable Product) options/attributes show on product page?

Magento ver. 1.5.1.0
I have an attribute set “clothing”
There are two attributes in the set: “size” and “colour”
Size is required, colour is optional (i.e. not all products have any colour options).
I have created some Simple Products where the Size is set but Colour just has the empty value.
On the product page for the relevant Configurable Product no option inputs are shown! And in the product view.phtml if I echo $this->hasOptions() it prints an empty string, ie False.
If I set the Colour to a non-empty value then both select boxes are shown on the product page and echo $this->hasOptions() prints 1, ie True.
This doesn’t make sense to me, not sure what is failing?
Im struggling with something similar and have noticed similar behavior to what you have described.
Check first if the product is actually properly configurable. this is taken from another post in SO and was meant to part of a controller. Drop this on the front end ../template/catalog/product/view.phtml just to check.
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
?>
<?php
if ($_product->isConfigurable()) {
$configurable = $_product->getTypeInstance();
$attributes = $configurable->getConfigurableAttributes($_product);
foreach ($attributes as $attribute) {
print $attribute->getLabel();
print "<br />";
}
}
?>
so as an answer i think you are becoming confused between custom options and configurable products.

Magento: how do you change the number of product columns based upon page layout?

I know that in catalog.xml this line effects all layouts:
<action method="setColumnCount"><columns>5</columns></action>
But I want to change the number of columns based upon specific page layouts, i.e.: 2columns with left bar, 3 columns, etc.
This is what someone said to do but I am not sure I was adding the update tag in the right spot because it didn't seem to work. Also if you read the comments they say once you turn caching back on it breaks it:
http://www.lotusseedsdesign.com/blog/change-grid-view-column-4-product-listing
So does anyone know how to use the addColumnCountLayoutDepend method or any other way to change the number of columns on the product grid specific to the page layout?
For sub category page, in app/design/frontend/Your Interface/layout/catalog.xml change columns value the following line :
<action method="setColumnCount"><columns>4</columns></action>
For root category page, in app/design/frontend/Your Interface/template/catalog/product/list.phtml find the following code in "Grid Mode" section and change with appropiate value :
<?php $_columnCount = $this->getColumnCount(); ?>
like
<?php $_columnCount = 4; ?>
A bit old question (almost 3 years :D) but I found an interesting solution to this on Kathir 'Sid' Vel's site
For this you will edit catalog/product/list.phtml template.
Find the row:
<?php $_columnCount = $this->getColumnCount(); ?>
and replace with the code bellow or comment the line and add after it the code.
<?php
/* Get the layout's page template */
$pageLayoutRootTemplate = $this->getLayout()->getBlock('root')->getTemplate();
/* Set the column count based on the layout template used */
switch ($pageLayoutRootTemplate) {
case 'page/1column.phtml':
$_columnCount = 4;
break;
case 'page/2columns-left.phtml':
$_columnCount = 3;
break;
case 'page/2columns-right.phtml':
$_columnCount = 3;
break;
case 'page/3columns.phtml':
$_columnCount = 2;
break;
default:
$_columnCount = 3;
break;
}
?>
There are a few ways to change the column counts. You can try each method to see what suits you.
1) Watch this tutorial: http://www.youtube.com/watch?v=wNbV34v72a0
Here's the code you need to make it work.
<reference name="product_list">
<action method="setColumnCount"><count>4</count></action>
</reference>
2) If the above fails, use this in whatever CMS page you want to change the columns. So for example: if you have a CMS page for displaying a specific category, use this. This is especially great for displaying a specific category products and setting the number of items per row at the same time. Past the following code in your CMS page:
{{block type="catalog/product_list" category_id="42" template="catalog/product/showroom.phtml" columnCount="4"}} 
You can change the columnCount number from 4 to whatever you want. You can get the category id by clicking on a category under your magento admin/category/manage category/
This is very important! Sometimes, when you do everything right in either of the two steps above, the items per row still does not change. The reason can easily be a CSS issue. So that leads to step 3:
3) Open your template's CSS and search for .products-grid
Make sure the width is set to a number high enough to be able to display the number of items per row you want, without having to push down overflowing items. In my case, I tried all I could to have a 4 items per row but failed, until I noticed in the CSS that my .col1-layout .products-grid had a width of 750px. I changed the width to 995px and voila, step 2 worked flawlessly.
This is late but I hope it helps someone out there.
Go to app\design\frontend\base\default\layout\catalog.xml
then go to Go to line 85 and fine the code. and find "catalog/product_list_toolbar" then customize the products according to your need.
Simplest way to change it is from the configuration tab
System->Configuration-> catalog tab-catalog->frontend you can change it easily from your magento backend.

Resources