Skip to content

Commit c3cf84f

Browse files
authored
Merge pull request #18 from landofcoder/feature4
Create product tags display on sidebar
2 parents 893ed78 + fe32e84 commit c3cf84f

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

Block/Tag/Product/ListProduct.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,47 @@
2525

2626
class ListProduct extends \Magento\Framework\View\Element\Template
2727
{
28+
protected $resultPageFactory;
29+
30+
protected $_tagFactory;
31+
32+
protected $_tagcollection;
33+
34+
protected $_tagHelper;
2835

29-
/**
30-
* Constructor
31-
*
32-
* @param \Magento\Framework\View\Element\Template\Context $context
33-
* @param array $data
34-
*/
3536
public function __construct(
3637
\Magento\Framework\View\Element\Template\Context $context,
38+
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
39+
\Lof\ProductTags\Model\TagFactory $tagFactory,
40+
\Lof\ProductTags\Helper\Data $tagdata,
3741
array $data = []
3842
) {
43+
$this->resultPageFactory = $resultPageFactory;
44+
$this->_tagFactory = $tagFactory;
45+
$this->_tagHelper = $tagdata;
3946
parent::__construct($context, $data);
4047
}
41-
42-
/**
43-
* @return string
44-
*/
45-
public function getListProducts()
48+
public function _toHtml(){
49+
if(!$this->_tagHelper->getGeneralConfig('enabled')) return;
50+
if(!$this->_tagHelper->getGeneralConfig('enable_tag_sidebar')) return;
51+
return parent::_toHtml();
52+
}
53+
function getTagHelper(){
54+
return $this->_tagHelper;
55+
}
56+
public function getTagCollection()
4657
{
47-
//Your block code
48-
return __('Hello Developer! This how to get the storename: %1 and this is the way to build a url: %2', $this->_storeManager->getStore()->getName(), $this->getUrl('contacts'));
58+
if(!$this->_tagcollection){
59+
$limit = $this->_tagHelper->getGeneralConfig('number_tags_sidebar');
60+
$limit = $limit?(int)$limit:10;
61+
$tag = $this->_tagFactory->create();
62+
$collection = $tag->getCollection();
63+
$collection->addFieldToFilter("status", 1);
64+
$collection->setOrder("tag_id","DESC");
65+
$collection->setPageSize($limit);
66+
//$collection->setLimit($limit);
67+
$this->_tagcollection = $collection;
68+
}
69+
return $this->_tagcollection;
4970
}
5071
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<page layout="2column-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
<body>
4+
<!-- <move element="catalog.product.related" destination="sidebar.additional" after="tag.view"/> -->
5+
<!-- <referenceBlock name="catalog.compare.sidebar" remove="true" />
6+
<referenceBlock name="wishlist_sidebar" remove="true" /> -->
7+
<referenceContainer name="sidebar.main">
8+
<block class="Lof\ProductTags\Block\Tag\Product\ListProduct" name="tag.view" template="Lof_ProductTags::tag/product/listProduct.phtml"/>
9+
</referenceContainer>
10+
</body>
11+
</page>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* @var $block \Lof\ProductTags\Block\Tag\Product\ListProduct
4+
*/
5+
?>
6+
<?php
7+
$tags = $this->getTagCollection();
8+
$title = $this->getTagHelper()->getGeneralConfig("tag_sidebar_title");
9+
if($tags && $tags->getSize()){
10+
?>
11+
<div class="block block-tags block-product-tags">
12+
<div class="block-title"><h2><?php echo $title; ?></h2></div>
13+
<div class="block-content">
14+
<ul style="padding: 0px;" class="list-tags">
15+
<?php foreach($tags as $tag): ?>
16+
<li style="list-style-type: none; float: left; margin:5px 10px; border: 1px solid #000000; border-radius: 5px; padding: 5px;"><a href="<?php echo $tag->getIdentifier(); ?>">
17+
<style>
18+
li:hover{
19+
background-color: #e4e4e4;
20+
}
21+
a:hover{
22+
color: black;
23+
text-decoration: none;
24+
}
25+
</style>
26+
<?php
27+
echo $tag->getTagTitle();
28+
?>
29+
</a>
30+
</li>
31+
<?php endforeach; ?>
32+
</ul>
33+
</div>
34+
</div>
35+
<?php
36+
}
37+
?>

view/frontend/templates/tag/product/tags.phtml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ if($tags && $tags->getSize()){
1111
<div class="block block-tags block-product-tags">
1212
<div class="block-title"><h2><?php echo $title; ?></h2></div>
1313
<div class="block-content">
14-
<ul class="list-tags">
14+
<ul style="padding: 0px;" class="list-tags">
1515
<?php foreach($tags as $tag): ?>
16-
<li><a href="<?php echo $tag->getIdentifier(); ?>">
17-
<?php
16+
<li style="list-style-type: none; float: left; margin:5px 10px; border: 1px solid #000000; border-radius: 5px; padding: 5px;"><a href="<?php echo $tag->getIdentifier(); ?>">
17+
<style>
18+
li:hover{
19+
background-color: #e4e4e4;
20+
}
21+
a:hover{
22+
color: black;
23+
text-decoration: none;
24+
}
25+
</style>
26+
<?php
1827
echo $tag->getTagTitle();
1928
?>
29+
</a>
2030
</li>
2131
<?php endforeach; ?>
2232
</ul>
2333
</div>
2434
</div>
2535
<?php
2636
}
27-
?>
37+
?>

0 commit comments

Comments
 (0)