Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.54 KB | None | 0 0
  1. <?php
  2. class Namespace_Modulename_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
  3. {
  4. /**
  5. * return the search results with the category filters added
  6. *
  7. * @return Mage_Catalog_Model_Resource_Product_Collection|false
  8. */
  9. public function getCategoryProductSearchResults()
  10. {
  11. $productCollection = $this->getData('product_collection');
  12. return $productCollection;
  13. }
  14. }
  15.  
  16. <?php
  17. /**
  18. * reuse existing magento default category filters
  19. * in order to display the categories and the corresponding products
  20. */
  21. class Namespace_Modulename_Block_CatalogSearch_Layer_Filter_Category extends Mage_Catalog_Block_Layer_Filter_Category
  22. {
  23. public $_filter;
  24.  
  25. /**
  26. * Initialize filter template
  27. *
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. $this->setTemplate('catalogsearch/layer/filter.phtml');
  33. }
  34.  
  35. /**
  36. * set product collection based on the search term
  37. * add to the block and render the block HTML
  38. *
  39. * @param Mage_Catalog_Model_Category $_item
  40. * @param string $count
  41. *
  42. * @return HTML
  43. */
  44. public function getCategoryProductSearchResultHtml($_item, $count)
  45. {
  46. $block = '';
  47.  
  48. //form the search query once again based on the keyword to get the complete product collection
  49. $term = Mage::helper('catalogsearch')->getQueryText();
  50. $query = Mage::getModel('catalogsearch/query')->setQueryText($term)->prepare();
  51.  
  52. Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
  53. Mage::getModel('catalogsearch/fulltext'),
  54. $term,
  55. $query
  56. );
  57.  
  58. //get complete product collection based on the search term
  59. $collection = Mage::getResourceModel('catalog/product_collection');
  60. $collection->getSelect()->joinInner(
  61. array('search_result' => $collection->getTable('catalogsearch/result')),
  62. $collection->getConnection()->quoteInto(
  63. 'search_result.product_id=e.entity_id AND search_result.query_id=?',
  64. $query->getId()
  65. ),
  66. array('relevance' => 'relevance')
  67. );
  68.  
  69. //add category filter for each category
  70. $categoryId = $this->getCategoryId($_item);
  71. $collection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
  72. ->addAttributeToFilter('category_id', array('in' => $categoryId));
  73.  
  74.  
  75. Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
  76. Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
  77.  
  78. //$productIds = array();
  79. //$productIds = $collection->getAllIds();
  80. //Zend_Debug::dump($productIds);
  81. //$this->_productCollection = $collection;
  82. //$collection->printLogQuery(true);
  83.  
  84. if(count($collection))
  85. {
  86. $block = $this->getLayout()->createBlock('namespace_modulename/catalog_product_list')
  87. ->setProductCollection($collection)
  88. ->setTemplate('catalogsearch/product/list.phtml');
  89.  
  90. return $block->toHtml();
  91. }
  92. }
  93.  
  94. /**
  95. * get the category id based on the name/label of the category filter
  96. *
  97. * @param Mage_Catalog_Model_Category $_item
  98. * @return string
  99. */
  100. public function getCategoryId($_item)
  101. {
  102. $categoryCollection = Mage::getModel('catalog/category')->getCollection()
  103. ->addAttributeToSelect('*')
  104. ->addAttributeToFilter('name', trim($_item->getLabel()))
  105. ;
  106.  
  107. foreach ($categoryCollection as $data)
  108. {
  109. return $data->getId();
  110. }
  111. }
  112. }
  113.  
  114. <?php
  115. /**
  116. * reuse the concept of state_renderers from default magento
  117. * change the template for search results page only
  118. */
  119. class Namespace_Modulename_Block_CatalogSearch_Layer_State extends Mage_Catalog_Block_Layer_State
  120. {
  121. /**
  122. * Initialize Layer State template
  123. *
  124. */
  125. public function __construct()
  126. {
  127. parent::__construct();
  128. $this->setTemplate('catalogsearch/layer/state.phtml');
  129. }
  130. }
  131.  
  132. <?php
  133. /**
  134. * reuse the default magento layer concept
  135. * add a new block if its a search page with new template
  136. * load the existing block to display the HTML
  137. */
  138. class Namespace_Modulename_Block_CatalogSearch_Layer extends Mage_CatalogSearch_Block_Layer
  139. {
  140. protected $_searchStateBlockName;
  141.  
  142. protected $_searchFilterHtmlBlockName;
  143.  
  144. /**
  145. * Initialize blocks names
  146. */
  147. protected function _initBlocks()
  148. {
  149. parent::_initBlocks();
  150.  
  151. $route = Mage::app()->getRequest()->getRouteName();
  152. if ( $route == 'catalogsearch' )
  153. {
  154. $this->_searchStateBlockName = 'namespace_modulename/catalogsearch_layer_state';
  155. $this->_searchFilterHtmlBlockName = 'namespace_modulename/catalogsearch_layer_filter_category';
  156. }
  157. }
  158.  
  159. /**
  160. * Get search categories layered navigation state html
  161. *
  162. * @return string
  163. */
  164. public function getSearchStateHtml()
  165. {
  166. if ( $this->_searchStateBlockName != NULL )
  167. {
  168. $searchStateBlock = $this->getLayout()->createBlock($this->_searchStateBlockName)->setLayer($this->getLayer());
  169. $this->setChild('search_layer_state', $searchStateBlock);
  170. }
  171.  
  172. return $this->getChildHtml('search_layer_state');
  173. }
  174.  
  175. /**
  176. * return the manipulated filter html from the PHTML for search
  177. *
  178. * @param Mage_Catalog_Model_Layer_Filter_Abstract|false $_filter
  179. * @return HTML
  180. */
  181. public function getSearchFilterHtml($_filter)
  182. {
  183. if ( $this->_searchStateBlockName != NULL )
  184. {
  185. $block = $this->getLayout()->createBlock($this->_searchFilterHtmlBlockName)
  186. ->setLayer($this->getLayer())
  187. ;
  188. $block->_filter = $_filter;
  189.  
  190. return $block->toHtml();
  191. }
  192. }
  193. }
  194.  
  195. <?xml version="1.0" encoding="UTF-8"?>
  196. <config>
  197. <modules>
  198. <Namespace_Modulename>
  199. <version>1.0.0</version>
  200. </Namespace_Modulename>
  201. </modules>
  202. <global>
  203. <blocks>
  204. <namespace_modulename>
  205. <class>Namespace_Modulename_Block</class>
  206. </namespace_modulename>
  207. <catalog>
  208. <rewrite>
  209. <product_list>Namespace_Modulename_Block_Catalog_Product_List</product_list>
  210. </rewrite>
  211. </catalog>
  212. <catalogsearch>
  213. <rewrite>
  214. <layer>Namespace_Modulename_Block_CatalogSearch_Layer</layer>
  215. </rewrite>
  216. </catalogsearch>
  217. </blocks>
  218. <helpers>
  219. <namespace_modulename>
  220. <class>Namespace_Modulename_Helper</class>
  221. </namespace_modulename>
  222. </helpers>
  223. </global>
  224. </config>
  225.  
  226. ...
  227. <catalogsearch_result_index translate="label">
  228. <label>Quick Search Form</label>
  229. <reference name="root">
  230. <action method="setTemplate"><template>page/3columns.phtml</template></action>
  231. </reference>
  232. <!--reference name="left_first">
  233. <block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml">
  234. <block type="core/text_list" name="catalog.leftnav.state.renderers" as="state_renderers" />
  235. </block>
  236. </reference-->
  237. <reference name="content">
  238. <block type="catalogsearch/result" name="search.result" template="catalogsearch/result.phtml">
  239. <!--block type="catalog/product_list" name="search_result_list" template="catalog/product/list.phtml"-->
  240. <block type="namespace_modulename/catalog_product_list" name="search_result_list" template="catalogsearch/product/list.phtml">
  241. <block type="namespace_modulename/catalogsearch_layer" name="catalogsearch.leftnav" after="currency" template="catalogsearch/layer/view.phtml">
  242. <block type="core/text_list" name="catalog.leftnav.state.renderers" as="state_renderers" />
  243. </block>
  244. </block>
  245. </block>
  246. </reference>
  247. </catalogsearch_result_index>
  248. ...
  249.  
  250. <?php echo $this->getChildHtml('catalogsearch.leftnav'); ?>
  251. <?php
  252. $_productCollection=$this->getCategoryProductSearchResults();
  253.  
  254. if (count($_productCollection))
  255. {
  256. $_helper = $this->helper('catalog/output');
  257. ?>
  258. <div class="category-products">
  259.  
  260. <?php // Grid Mode ?>
  261. <?php $_collectionSize = $_productCollection->count() ?>
  262. <?php $_columnCount = $this->getColumnCount(); ?>
  263. <ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col">
  264. <?php $i=0; foreach ($_productCollection as $_product): ?>
  265. <?php /*if ($i++%$_columnCount==0): ?>
  266. <?php endif*/ ?>
  267. <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
  268. <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
  269. <?php $_imgSize = 210; ?>
  270. <img id="product-collection-image-<?php echo $_product->getId(); ?>"
  271. src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($_imgSize); ?>"
  272. alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
  273. </a>
  274. <div class="product-info">
  275. <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
  276. <?php
  277. // Provides extra blocks on which to hang some features for products in the list
  278. // Features providing UI elements targeting this block will display directly below the product name
  279. if ($this->getChild('name.after')) {
  280. $_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
  281. foreach ($_nameAfterChildren as $_nameAfterChildName) {
  282. $_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
  283. $_nameAfterChild->setProduct($_product);
  284. echo $_nameAfterChild->toHtml();
  285. }
  286. }
  287. ?>
  288. <?php echo $this->getPriceHtml($_product, true) ?>
  289. <?php if($_product->getRatingSummary()): ?>
  290. <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
  291. <?php endif; ?>
  292. <div class="actions">
  293. <?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
  294. <button type="button" title="<?php echo $this->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
  295. <?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
  296. <a title="<?php echo $this->quoteEscape($this->__('View Details')) ?>" class="button" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('View Details') ?></a>
  297. <?php else: ?>
  298. <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
  299. <?php endif; ?>
  300. <ul class="add-to-links">
  301. <?php if ($this->helper('wishlist')->isAllow()) : ?>
  302. <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
  303. <?php endif; ?>
  304. <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
  305. <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
  306. <?php endif; ?>
  307. </ul>
  308. </div>
  309.  
  310. <?php
  311. // Provides extra blocks on which to hang some features for products in the list
  312. // Features providing UI elements targeting this block will display directly below the product name
  313. if ($this->getChild('price.cart.after')) {
  314. $_priceCartAfterChildren = $this->getChild('price.cart.after')->getSortedChildren();
  315. foreach ($_priceCartAfterChildren as $_priceCartAfterChildName) {
  316. $_priceCartAfterChild = $this->getChild('price.cart.after')->getChild($_priceCartAfterChildName);
  317. $_priceCartAfterChild->setProduct($_product);
  318. echo $_priceCartAfterChild->toHtml();
  319. }
  320. }
  321. ?>
  322. </div>
  323. </li>
  324. <?php /*if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
  325. <?php endif*/ ?>
  326. <?php endforeach ?>
  327. </ul>
  328. <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
  329. </div>
  330. <?php
  331. // Provides a block where additional page components may be attached, primarily good for in-page JavaScript
  332. if ($this->getChild('after')) {
  333. $_afterChildren = $this->getChild('after')->getSortedChildren();
  334. foreach ($_afterChildren as $_afterChildName) {
  335. $_afterChild = $this->getChild('after')->getChild($_afterChildName);
  336. //set product collection on after blocks
  337. $_afterChild->setProductCollection($_productCollection);
  338. echo $_afterChild->toHtml();
  339. }
  340. }
  341. }
  342. ?>
  343.  
  344. <?php
  345. /**
  346. * Magento
  347. *
  348. * NOTICE OF LICENSE
  349. *
  350. * This source file is subject to the Academic Free License (AFL 3.0)
  351. * that is bundled with this package in the file LICENSE_AFL.txt.
  352. * It is also available through the world-wide-web at this URL:
  353. * http://opensource.org/licenses/afl-3.0.php
  354. * If you did not receive a copy of the license and are unable to
  355. * obtain it through the world-wide-web, please send an email
  356. * to license@magento.com so we can send you a copy immediately.
  357. *
  358. * DISCLAIMER
  359. *
  360. * Do not edit or add to this file if you wish to upgrade Magento to newer
  361. * versions in the future. If you wish to customize Magento for your
  362. * needs please refer to http://www.magento.com for more information.
  363. *
  364. * @category design
  365. * @package rwd_default
  366. * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
  367. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  368. */
  369. ?>
  370. <?php
  371. /**
  372. * Template for filter items block
  373. *
  374. * @see Mage_Catalog_Block_Layer_Filter
  375. */
  376. ?>
  377.  
  378. <ol>
  379. <?php $count = 1; ?>
  380. <?php foreach ($this->getItems() as $_item): ?>
  381. <li>
  382. <?php if ($_item->getCount() > 0): ?>
  383. <?php /*<a href="<?php echo $this->urlEscape($_item->getUrl()) ?>">*/ ?>
  384. <?php echo $_item->getLabel() ?>
  385. <?php if ($this->shouldDisplayProductCount()): ?>
  386. <span class="count">(<?php echo $_item->getCount() ?>)</span>
  387. <?php endif; ?>
  388. <?php echo $this->getCategoryProductSearchResultHtml($_item, $count); ?>
  389. <?php /*</a>*/ ?>
  390. <?php else: ?>
  391. <span>
  392. <?php echo $_item->getLabel(); ?>
  393. <?php if ($this->shouldDisplayProductCount()): ?>
  394. <span class="count">(<?php echo $_item->getCount() ?>)</span>
  395. <?php endif; ?>
  396. </span>
  397. <?php endif; ?>
  398. </li>
  399. <?php endforeach ?>
  400. </ol>
  401.  
  402. <?php
  403. /**
  404. * Magento
  405. *
  406. * NOTICE OF LICENSE
  407. *
  408. * This source file is subject to the Academic Free License (AFL 3.0)
  409. * that is bundled with this package in the file LICENSE_AFL.txt.
  410. * It is also available through the world-wide-web at this URL:
  411. * http://opensource.org/licenses/afl-3.0.php
  412. * If you did not receive a copy of the license and are unable to
  413. * obtain it through the world-wide-web, please send an email
  414. * to license@magento.com so we can send you a copy immediately.
  415. *
  416. * DISCLAIMER
  417. *
  418. * Do not edit or add to this file if you wish to upgrade Magento to newer
  419. * versions in the future. If you wish to customize Magento for your
  420. * needs please refer to http://www.magento.com for more information.
  421. *
  422. * @category design
  423. * @package rwd_default
  424. * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
  425. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  426. */
  427. ?>
  428. <?php
  429. /**
  430. * Category layered navigation state
  431. *
  432. * @see Mage_Catalog_Block_Layer_State
  433. */
  434. ?>
  435. <?php
  436. $_filters = $this->getActiveFilters();
  437. $_renderers = $this->getParentBlock()->getChild('state_renderers')->getSortedChildren();
  438. ?>
  439. <?php if(!empty($_filters)): ?>
  440. <div class="currently">
  441. <ol>
  442. <?php foreach ($_filters as $_filter): ?>
  443. <?php
  444. $_rendered = false;
  445. foreach ($_renderers as $_rendererName):
  446. $_renderer = $this->getParentBlock()->getChild('state_renderers')->getChild($_rendererName);
  447. if (method_exists($_renderer, 'shouldRender') && $_renderer->shouldRender($_filter)):
  448. $_renderer->setFilter($_filter);
  449. echo $_renderer->toHtml();
  450. $_rendered = true;
  451. break;
  452. endif;
  453. endforeach;
  454.  
  455. if (!$_rendered):
  456. ?>
  457. <li>
  458. <?php
  459. $clearLinkUrl = $_filter->getClearLinkUrl();
  460. if ($clearLinkUrl):
  461. ?>
  462. <a class="btn-previous" href="<?php echo $_filter->getRemoveUrl() ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Previous')) ?>"><?php echo $this->__('Previous') ?></a>
  463. <a class="btn-remove" title="<?php echo $this->escapeHtml($_filter->getFilter()->getClearLinkText()) ?>" href="<?php echo $clearLinkUrl ?>"><?php echo $this->escapeHtml($_filter->getFilter()->getClearLinkText()) ?></a>
  464. <?php else: ?>
  465. <a class="btn-remove" href="<?php echo $_filter->getRemoveUrl() ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Remove This Item')) ?>"><?php echo $this->__('Remove This Item') ?></a>
  466. <?php endif; ?>
  467. <span class="label"><?php echo $this->__($_filter->getName()) ?>:</span> <span class="value"><?php echo $this->stripTags($_filter->getLabel()) ?></span>
  468. </li>
  469. <?php endif; ?>
  470. <?php endforeach; ?>
  471. </ol>
  472. </div>
  473. <?php endif; ?>
  474.  
  475. <?php
  476. /**
  477. * Magento
  478. *
  479. * NOTICE OF LICENSE
  480. *
  481. * This source file is subject to the Academic Free License (AFL 3.0)
  482. * that is bundled with this package in the file LICENSE_AFL.txt.
  483. * It is also available through the world-wide-web at this URL:
  484. * http://opensource.org/licenses/afl-3.0.php
  485. * If you did not receive a copy of the license and are unable to
  486. * obtain it through the world-wide-web, please send an email
  487. * to license@magento.com so we can send you a copy immediately.
  488. *
  489. * DISCLAIMER
  490. *
  491. * Do not edit or add to this file if you wish to upgrade Magento to newer
  492. * versions in the future. If you wish to customize Magento for your
  493. * needs please refer to http://www.magento.com for more information.
  494. *
  495. * @category design
  496. * @package rwd_default
  497. * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
  498. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  499. */
  500. ?>
  501. <?php
  502. /**
  503. * Category layered navigation
  504. *
  505. * @see Mage_Catalog_Block_Layer_View
  506. */
  507. ?>
  508. <?php if($this->canShowBlock()): ?>
  509. <div class="block block-layered-nav<?php if (!$this->getLayer()->getState()->getFilters()): ?> block-layered-nav--no-filters<?php endif; ?>">
  510. <?php /*<div class="block-title">
  511. <strong><span><?php echo $this->__('Shop By') ?></span></strong>
  512. </div>*/ ?>
  513. <!--div class="block-content toggle-content"-->
  514. <?php echo $this->getSearchStateHtml() ?>
  515. <?php /*if ($this->getLayer()->getState()->getFilters()): ?>
  516. <div class="actions"><a href="<?php echo $this->getClearUrl() ?>"><?php echo $this->__('Clear All') ?></a></div>
  517. <?php endif;*/ ?>
  518. <?php if($this->canShowOptions()): ?>
  519. <!--p class="block-subtitle block-subtitle--filter"><?php //echo $this->__('Filter') ?></p>
  520. <dl id="narrow-by-list"-->
  521. <?php $_filters = $this->getFilters() ?>
  522. <?php foreach ($_filters as $_filter): ?>
  523. <?php if($_filter->getItemsCount()): ?>
  524. <dd><?php echo $this->getSearchFilterHtml($_filter); ?></dd>
  525. <?php endif; ?>
  526. <?php endforeach; ?>
  527. <!--/dl-->
  528. <script type="text/javascript">decorateDataList('narrow-by-list')</script>
  529. <?php endif; ?>
  530. <!--/div-->
  531. </div>
  532. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement