Magento 1.7 add Pagination in product viewed not working - magento

Product viewed located here:
App/code/local/Mage/Reports/Block/Product/Viewed.php
I have added the following code:
class Mage_Reports_Block_Product_Viewed extends Mage_Reports_Block_Product_Abstract{
...
protected function _prepareLayout()
{
parent::_prepareLayout();
$toolbar = $this->getLayout()->createBlock('catalog/product_list_toolbar', microtime())
->setCollection($this->getProductCollection());
$pager = $this->getLayout()->createBlock('page/html_pager', microtime());
$toolbar->setChild('product_list_toolbar_pager', $pager);
$this->setChild('toolbar', $toolbar);
$this->getProductCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('toolbar');
}
}
My template should be like this:
<?php if ($_products = $this->getRecentlyViewedProducts()): ?>
<!-- top pagination -->
<?php echo $this->getPagerHtml(); ?>
<?php if($_collection->getSize()): ?>
...
<?php foreach ($_collection as $_item): ?>
   ...
  <?php endforeach; ?>
  
<?php endif ?>
<!-- bottom pagination -->
<?php echo $this->getPagerHtml(); ?>
<?php endif ?>
I'm added code not working in Viewed.php. Can someone please help me solve this problem?
Any help would be greatly appreciated.

You can add your Block code under : app/code/local/Mage/Catalog/Block/Product/Viewed.php
And make the class name changes respectively .
If your code is correct , it should work .

Related

How Do I Output Multiselect Attribute Values In Magento 2.3.x?

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);
}

Magento - Unable to display custom options

I'm using a module for displaying grouped configurable products and all options are showing except custom options. They are displaying on the configurable product page but that's it. I'm trying to use the code in app\design\frontend\blank\blank\template\catalog\product\view\options.phtml in my custom configurable.phtml but $_options is showing up null. Here is the code used to retrieve $_options
<?php $_options = Mage::helper('core')->decorateArray($this->getOptions()) ?>
<?php if (count($_options)):?>
and after the javascript
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>
<?php else: echo dlkghflghf;?>
<?php endif; ?>
The dlkghflghf is displaying so i know $_options is not showing up. Any suggestions?
Your custom configurable.phtml is a template of what block? You can use a block that extends Mage_Catalog_Block_Product_View_Options or you can set your own method for options like the method from Mage_Catalog_Block_Product_View_Options, you also need to have a method getProduct():
public function getOptions()
{
return $this->getProduct()->getOptions();
}

Get data from helper in CodeIgniter

I have a trouble with CodeIgniter.
I would like to get (recoup) data from a helper that I call in a view.
I will explain with the code
Here's my view :
// CALL THE FUNCTION FROM THE HELPER
<?php recup_email(); ?>
// DATA RECOUP FROM THE HELPER
<?php foreach($em as $e): ?>
<?php echo $e->email_membre; ?>
<?php endforeach; ?>
As you can see, I call the function and just after I would like to use data that I had recoup.
Here's my helper :
function recup_email()
{
$CI =& get_instance();
$CI->load->model('unite_model');
$data['em'] = $CI->unite_model->email_membre_model();
}
But I don't know how to recoup data without using
$layout_network['contenu'] = $this->load->view('list_vote_friend', $data, TRUE);
$this->load->view('layout_network',$layout_network);
because the view is already loaded.
I hope you understand, sorry for my bad english.
Many thanks.
In the example you have given, why not just return the data from the helper function?
function recup_email()
{
$CI =& get_instance();
$CI->load->model('unite_model');
$membres = $CI->unite_model->email_membre_model();
return $membres;
}
So that is you view;
// CALL THE FUNCTION FROM THE HELPER
<?php $em = recup_email(); ?>
// DATA RECOUP FROM THE HELPER
<?php foreach($em as $e): ?>
<?php echo $e->email_membre; ?>
<?php endforeach; ?>

Best way for creating dynamic sidebar section in Symfony 1.4

I have few frontend modules which has own sidebar menu link. I want to create those links in actions class of module:
public function preExecute()
{
$items['plan/new'] = 'Create Plan';
$items['plan/index'] = 'Plans Listing';
$this->getResponse()->setSlot('sidebar', $items);
}
Slot file sidebar.php
#apps/frontend/templates/sidebar.php
<?php slot('sidebar') ?>
<ul>
<?php foreach($items as $url => $title) : ?>
<li><?php echo link_to($url, $title) ?></li>
<?php endforeach ?>
</ul>
<?php end_slot() ?>
layout.php:
<?php if (has_slot('sidebar')): ?>
<div id="sidebar"><?php include_slot('sidebar') ?></div>
<?php endif ?>
but my output is Array, how can I render my slot?
You seem to be mixing slots and partials. In your action, you set your slot to an array, later you call include_slot, and the string representation is Array, that is correct.
You should pass items via $this->items = $items, then in your action see if isset($items) is true, and call include_partial("sidebar", array("items" => $items)) if neccesary. This will look for a file called _sidebar.php.
For more detailed information of how this stuff works, read the Inside the View Layer: Code fragments part of the sf1.4 book.

Sorting categories in Magento according to the position in admin

I would like to know how to sort this list of categories (I followed this tutorial here http://www.devinrolsen.com/magento-custom-category-listing-block/) in magento by position in the admin panel? Currently it is sorted by id
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo '<a href="' . $category->getUrl() . '">';
echo $category->getName() . '</a>';
?>
</li>
<?php endforeach; ?>
</ul>
You're making way too much work for yourself trying to deal with IDs and stuff. The following is already sorted by position as standard.
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<?php echo $category->getName() ?>
</li>
<?php endforeach; ?>
</ul>
If you want to sort the categories by the position created in adminhtml you can then, since catalog/category is an instance of Mage_Catalog_Model_Resource_Category_Collection, make a query where you specify what you want to select, filter and/or sort.
The case here is getting categories from catalog_category_entity select only the name, filtering after the id and sort the query on the position.
<?php
$subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('position', ASC);
?>
This is what I did:
$allCategories = Mage::getModel('catalog/category');
$CategoriesTree = $allCategories->getTreeModel()->load();
$categoriesIds =
$CategoriesTree->getCollection()->addAttributeToSort('position', 'asc')->getAllIds();
after to retrieve the categories:
$categoryChildren = array();
if ($categoriesIds) {
foreach ($categoriesIds as $categoryId){
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryChildren[] = $category;
}
}
and then:
// Sort by position
function comparePosition($a, $b) {
if ($a->position == $b->position)
return 0;
return ($a->position > $b->position) ? 1 : -1;
}
usort($categoryChildren, 'comparePosition');
Inverting the return (1 and -1) would obviously change the order.
It worked just fine for me.
Hope it helps someone.
I strongly suggest to lok here first http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections and also other articles in knowledge base are a must read for any magento dev.
<?php
$cats = Mage::getModel('catalog/category')->addAttributeToSort('yourfield', 'desc')->getCollection()->getChildren();
$catIds = explode(',',$cats);
?>
<?php
$model_category = Mage::getModel('catalog/category')->load($_category->getEntityId());
$sub_categories = $model_category->getCollection();
$sub_categories -> addAttributeToSelect('url_key')
-> addAttributeToSelect('name')
-> addAttributeToFilter('is_active',1)
-> addIdFilter($model_category->getChildren())
-> setOrder('position', 'ASC')
-> load();
?>
<?php foreach($sub_categories->getData() as $each_subcat): ?>
<?php $model_subcat = Mage::getModel('catalog/category')->load($each_subcat['entity_id']); ?>
<?php endforeach; ?>

Resources