Category page custom category filter magento2
I have loaded custom template before the product list in category list page.
this is my block file Vendor/Module/Block/Category.php
<?php
namespace VendorModuleBlock;
class Category extends MagentoFrameworkViewElementTemplate{
protected $categoryFactory;
protected $_storeManager;
protected $_categoryCollectionFactory;
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollectionFactory
)
{
$this->categoryFactory = $categoryFactory;
$this->_storeManager = $storeManager;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
parent::__construct($context);
}
public function getParentCategoryList(){
$categoryId = 3;
$category = $this->categoryFactory->create()->load($categoryId);
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
public function getChildCategoryList($categoryId){
$category = $this->categoryFactory->create()->load($categoryId);
// Children Categories
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
}
This is my template file view/frontend/template/category.phtml
<?php
$parentCategories = $block->getParentCategoryList();
?>
<div class="select-box">
<div class="container">
<div class="custom-dropdown">
<select id="parent-cat" name="parent-cat"
onchange="getChildrenList(this.value)">
<option value ="">- Please Select -</option>
<?php foreach($parentCategories as $parent){
$parentCatName = $parent->getName();
$parentCatUrl = $parent->getUrl();
$parentCatId = $parent->getId();
?>
<option value="<?php echo $parentCatId; ?>"><?php echo
$parentCatName; ?></option>
<?php } ?>
</select>
</div>
<div class="custom-dropdown center-cus-drop">
<select id="child-cat" name="child-cat"
onchange="getChildUrl(this.value)">
<option value ="">- Product category -</option>
</select>
</div>
</div>
</div>
<script>
function getChildrenList(parentId) {
if(parentId !=''){
var customurl = "<?php echo $this-
>getUrl().'frontname/category/childcategory' ?>";
jQuery.ajax({
url: customurl,
type: "POST",
data : 'category_id='+parentId,
dataType: 'json',
success : function(result) {
var optionHtml;
optionHtml +='<option value="">-Product Category-</option>';
for(var option of result){
optionHtml +='<option value="'+option.url+'">'+option.name+'</option>';
}
jQuery('#child-cat').html(optionHtml);
}
});
}
}
function getChildUrl(childUrl) {
var url = childUrl;
if (url) {
window.location = url; // redirect
}
///return false;
}
</script>
This is my controller file.
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelCategory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJsonFactory;
class Childcategory extends Action
{
protected $categoryRepository;
protected $resultJsonFactory;
public function __construct(Context $context, CategoryRepositoryInterface $categoryRepository, JsonFactory $jsonFactory)
{
$this->categoryRepository = $categoryRepository;
$this->resultJsonFactory = $jsonFactory;
parent::__construct($context);
}
public function execute()
{
$parentId = $this->getRequest()->getParam('category_id');
$category = $this->categoryRepository->get($parentId);
$childrenData = ;
foreach ($category->getChildrenCategories() as $childrenCategory) {
$parent_category = $childrenCategory->getParentCategory();
$childrenData = [
'name' => $childrenCategory->getName(),
'id' => $childrenCategory->getId(),
'url'=> $childrenCategory->getUrl()
];
}
return $this->resultJsonFactory->create()->setData($childrenData);
}
}
I have done first part, once the parent category is selected, i am populating the next drop down with its child categories,
On selecting the second drop down, i am redirecting to the child category url.
In this case as url redirects, the selected options again disappears.
How can i make both the drop down are selected on redirecting. Please anyone help me on this issue. Thanks
magento2 ajax category-listing
add a comment |
I have loaded custom template before the product list in category list page.
this is my block file Vendor/Module/Block/Category.php
<?php
namespace VendorModuleBlock;
class Category extends MagentoFrameworkViewElementTemplate{
protected $categoryFactory;
protected $_storeManager;
protected $_categoryCollectionFactory;
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollectionFactory
)
{
$this->categoryFactory = $categoryFactory;
$this->_storeManager = $storeManager;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
parent::__construct($context);
}
public function getParentCategoryList(){
$categoryId = 3;
$category = $this->categoryFactory->create()->load($categoryId);
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
public function getChildCategoryList($categoryId){
$category = $this->categoryFactory->create()->load($categoryId);
// Children Categories
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
}
This is my template file view/frontend/template/category.phtml
<?php
$parentCategories = $block->getParentCategoryList();
?>
<div class="select-box">
<div class="container">
<div class="custom-dropdown">
<select id="parent-cat" name="parent-cat"
onchange="getChildrenList(this.value)">
<option value ="">- Please Select -</option>
<?php foreach($parentCategories as $parent){
$parentCatName = $parent->getName();
$parentCatUrl = $parent->getUrl();
$parentCatId = $parent->getId();
?>
<option value="<?php echo $parentCatId; ?>"><?php echo
$parentCatName; ?></option>
<?php } ?>
</select>
</div>
<div class="custom-dropdown center-cus-drop">
<select id="child-cat" name="child-cat"
onchange="getChildUrl(this.value)">
<option value ="">- Product category -</option>
</select>
</div>
</div>
</div>
<script>
function getChildrenList(parentId) {
if(parentId !=''){
var customurl = "<?php echo $this-
>getUrl().'frontname/category/childcategory' ?>";
jQuery.ajax({
url: customurl,
type: "POST",
data : 'category_id='+parentId,
dataType: 'json',
success : function(result) {
var optionHtml;
optionHtml +='<option value="">-Product Category-</option>';
for(var option of result){
optionHtml +='<option value="'+option.url+'">'+option.name+'</option>';
}
jQuery('#child-cat').html(optionHtml);
}
});
}
}
function getChildUrl(childUrl) {
var url = childUrl;
if (url) {
window.location = url; // redirect
}
///return false;
}
</script>
This is my controller file.
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelCategory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJsonFactory;
class Childcategory extends Action
{
protected $categoryRepository;
protected $resultJsonFactory;
public function __construct(Context $context, CategoryRepositoryInterface $categoryRepository, JsonFactory $jsonFactory)
{
$this->categoryRepository = $categoryRepository;
$this->resultJsonFactory = $jsonFactory;
parent::__construct($context);
}
public function execute()
{
$parentId = $this->getRequest()->getParam('category_id');
$category = $this->categoryRepository->get($parentId);
$childrenData = ;
foreach ($category->getChildrenCategories() as $childrenCategory) {
$parent_category = $childrenCategory->getParentCategory();
$childrenData = [
'name' => $childrenCategory->getName(),
'id' => $childrenCategory->getId(),
'url'=> $childrenCategory->getUrl()
];
}
return $this->resultJsonFactory->create()->setData($childrenData);
}
}
I have done first part, once the parent category is selected, i am populating the next drop down with its child categories,
On selecting the second drop down, i am redirecting to the child category url.
In this case as url redirects, the selected options again disappears.
How can i make both the drop down are selected on redirecting. Please anyone help me on this issue. Thanks
magento2 ajax category-listing
add a comment |
I have loaded custom template before the product list in category list page.
this is my block file Vendor/Module/Block/Category.php
<?php
namespace VendorModuleBlock;
class Category extends MagentoFrameworkViewElementTemplate{
protected $categoryFactory;
protected $_storeManager;
protected $_categoryCollectionFactory;
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollectionFactory
)
{
$this->categoryFactory = $categoryFactory;
$this->_storeManager = $storeManager;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
parent::__construct($context);
}
public function getParentCategoryList(){
$categoryId = 3;
$category = $this->categoryFactory->create()->load($categoryId);
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
public function getChildCategoryList($categoryId){
$category = $this->categoryFactory->create()->load($categoryId);
// Children Categories
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
}
This is my template file view/frontend/template/category.phtml
<?php
$parentCategories = $block->getParentCategoryList();
?>
<div class="select-box">
<div class="container">
<div class="custom-dropdown">
<select id="parent-cat" name="parent-cat"
onchange="getChildrenList(this.value)">
<option value ="">- Please Select -</option>
<?php foreach($parentCategories as $parent){
$parentCatName = $parent->getName();
$parentCatUrl = $parent->getUrl();
$parentCatId = $parent->getId();
?>
<option value="<?php echo $parentCatId; ?>"><?php echo
$parentCatName; ?></option>
<?php } ?>
</select>
</div>
<div class="custom-dropdown center-cus-drop">
<select id="child-cat" name="child-cat"
onchange="getChildUrl(this.value)">
<option value ="">- Product category -</option>
</select>
</div>
</div>
</div>
<script>
function getChildrenList(parentId) {
if(parentId !=''){
var customurl = "<?php echo $this-
>getUrl().'frontname/category/childcategory' ?>";
jQuery.ajax({
url: customurl,
type: "POST",
data : 'category_id='+parentId,
dataType: 'json',
success : function(result) {
var optionHtml;
optionHtml +='<option value="">-Product Category-</option>';
for(var option of result){
optionHtml +='<option value="'+option.url+'">'+option.name+'</option>';
}
jQuery('#child-cat').html(optionHtml);
}
});
}
}
function getChildUrl(childUrl) {
var url = childUrl;
if (url) {
window.location = url; // redirect
}
///return false;
}
</script>
This is my controller file.
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelCategory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJsonFactory;
class Childcategory extends Action
{
protected $categoryRepository;
protected $resultJsonFactory;
public function __construct(Context $context, CategoryRepositoryInterface $categoryRepository, JsonFactory $jsonFactory)
{
$this->categoryRepository = $categoryRepository;
$this->resultJsonFactory = $jsonFactory;
parent::__construct($context);
}
public function execute()
{
$parentId = $this->getRequest()->getParam('category_id');
$category = $this->categoryRepository->get($parentId);
$childrenData = ;
foreach ($category->getChildrenCategories() as $childrenCategory) {
$parent_category = $childrenCategory->getParentCategory();
$childrenData = [
'name' => $childrenCategory->getName(),
'id' => $childrenCategory->getId(),
'url'=> $childrenCategory->getUrl()
];
}
return $this->resultJsonFactory->create()->setData($childrenData);
}
}
I have done first part, once the parent category is selected, i am populating the next drop down with its child categories,
On selecting the second drop down, i am redirecting to the child category url.
In this case as url redirects, the selected options again disappears.
How can i make both the drop down are selected on redirecting. Please anyone help me on this issue. Thanks
magento2 ajax category-listing
I have loaded custom template before the product list in category list page.
this is my block file Vendor/Module/Block/Category.php
<?php
namespace VendorModuleBlock;
class Category extends MagentoFrameworkViewElementTemplate{
protected $categoryFactory;
protected $_storeManager;
protected $_categoryCollectionFactory;
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollectionFactory
)
{
$this->categoryFactory = $categoryFactory;
$this->_storeManager = $storeManager;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
parent::__construct($context);
}
public function getParentCategoryList(){
$categoryId = 3;
$category = $this->categoryFactory->create()->load($categoryId);
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
public function getChildCategoryList($categoryId){
$category = $this->categoryFactory->create()->load($categoryId);
// Children Categories
$childrenCategories = $category->getChildrenCategories();
return $childrenCategories;
}
}
This is my template file view/frontend/template/category.phtml
<?php
$parentCategories = $block->getParentCategoryList();
?>
<div class="select-box">
<div class="container">
<div class="custom-dropdown">
<select id="parent-cat" name="parent-cat"
onchange="getChildrenList(this.value)">
<option value ="">- Please Select -</option>
<?php foreach($parentCategories as $parent){
$parentCatName = $parent->getName();
$parentCatUrl = $parent->getUrl();
$parentCatId = $parent->getId();
?>
<option value="<?php echo $parentCatId; ?>"><?php echo
$parentCatName; ?></option>
<?php } ?>
</select>
</div>
<div class="custom-dropdown center-cus-drop">
<select id="child-cat" name="child-cat"
onchange="getChildUrl(this.value)">
<option value ="">- Product category -</option>
</select>
</div>
</div>
</div>
<script>
function getChildrenList(parentId) {
if(parentId !=''){
var customurl = "<?php echo $this-
>getUrl().'frontname/category/childcategory' ?>";
jQuery.ajax({
url: customurl,
type: "POST",
data : 'category_id='+parentId,
dataType: 'json',
success : function(result) {
var optionHtml;
optionHtml +='<option value="">-Product Category-</option>';
for(var option of result){
optionHtml +='<option value="'+option.url+'">'+option.name+'</option>';
}
jQuery('#child-cat').html(optionHtml);
}
});
}
}
function getChildUrl(childUrl) {
var url = childUrl;
if (url) {
window.location = url; // redirect
}
///return false;
}
</script>
This is my controller file.
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelCategory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJsonFactory;
class Childcategory extends Action
{
protected $categoryRepository;
protected $resultJsonFactory;
public function __construct(Context $context, CategoryRepositoryInterface $categoryRepository, JsonFactory $jsonFactory)
{
$this->categoryRepository = $categoryRepository;
$this->resultJsonFactory = $jsonFactory;
parent::__construct($context);
}
public function execute()
{
$parentId = $this->getRequest()->getParam('category_id');
$category = $this->categoryRepository->get($parentId);
$childrenData = ;
foreach ($category->getChildrenCategories() as $childrenCategory) {
$parent_category = $childrenCategory->getParentCategory();
$childrenData = [
'name' => $childrenCategory->getName(),
'id' => $childrenCategory->getId(),
'url'=> $childrenCategory->getUrl()
];
}
return $this->resultJsonFactory->create()->setData($childrenData);
}
}
I have done first part, once the parent category is selected, i am populating the next drop down with its child categories,
On selecting the second drop down, i am redirecting to the child category url.
In this case as url redirects, the selected options again disappears.
How can i make both the drop down are selected on redirecting. Please anyone help me on this issue. Thanks
magento2 ajax category-listing
magento2 ajax category-listing
asked yesterday
jafar pinjar
618412
618412
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256773%2fcategory-page-custom-category-filter-magento2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256773%2fcategory-page-custom-category-filter-magento2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown