Page 1 of 1

How to hide certain category or product based on language

Posted: Wed May 23, 2012 5:31 pm
by jarvine
Hello,

Before starting OpenCart project in more detail, I need help in one thing and I could not find answer to this using search:

Store will have 3 languages: english, german, spanish.
Let's say there is 3 categories - cat1, cat2, cat3.

Cat1 should be visible to all languages.
Cat2 should be visible only if end user has selected german as language.
Cat3 should be visible only to end users who have selected spanish as language.

Is this possible out-of-the-box, and if, how? What files should I start looking to code it, if not built-in?

P.S. It is completely OK to show restricted categories and/or products to whatever geo-location the user comes from. I mean, english language user can change the language to german and see cat2 for german users.

Re: How to hide certain category or product based on languag

Posted: Thu May 24, 2012 6:35 pm
by Avvici
Seems this would be pretty easy to do just by always having the language code + Category ID to use in a conditional.
for 1.5.2.1 open catalog/controller/common/header.php and find this line:

Code: Select all

protected function index() {
Directly below it add this:

Code: Select all

$this->data['language_check'] = $this->language->get('code');
Now find this line:

Code: Select all

'children' => $children_data,
Directly below it add this:

Code: Select all

'id' => $category['category_id'],
Now open: catalog/view/theme/your_theme/template/common/header.tpl and find this code:

Code: Select all

<?php if ($categories) { ?>
<div id="menu">
  <ul>
    <?php foreach ($categories as $category) { ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
      <div>
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul>
          <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i])) { ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      </div>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
</div>
<?php } ?>
Replace entire code above with this:

Code: Select all

<?php if ($categories) { ?>
<div id="menu">
  <ul>
    <?php foreach ($categories as $category) { ?>
    <!--Start Category Conditional -->
     <?php if($language_check == "en" &&  $category['id'] == 45){ ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
      <div>
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul>
          <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i])) { ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      </div>
      <?php } ?>
    </li>
     <!--End Category Conditional -->
      <?php } ?>
    <?php } ?>
  </ul>
</div>
<?php } ?>
The following will only display category's that have a language code of 'en' and a category id of 45 (YOU NEED TO OBTAIN YOUR CATEGORY ID's by turning SEO URL off and looking at your browser bar, or opening up database in PHP My Admin to look at the table.) Tested and works fine. It's up to you to format your conditional. This is for parent categories only. If you want to conventionalize children categories as well then you simply just set up another inside the children array.

Re: How to hide certain category or product based on languag

Posted: Thu May 24, 2012 7:43 pm
by straightlight
While this subject may bring its interest to others, a module should rather be developed in case multiple category IDs would be involved in the template loop. Besides, from a module, if the condition is false from the validation, the customer can still be redirected to the previous path IDs and if no path IDs are to be found then to redirect to the home page. ;)

Re: How to hide certain category or product based on languag

Posted: Thu May 24, 2012 8:26 pm
by Avvici
straightlight wrote:While this subject may bring its interest to others, a module should rather be developed in case multiple category IDs would be involved in the template loop. Besides, from a module, if the condition is false from the validation, the customer can still be redirected to the previous path IDs and if no path IDs are to be found then to redirect to the home page. ;)
Get on it then. O0 Notice I told him that it was up to him to enhance his conditional? Therefore I did not place the }else{ in there for a return FALSE lol.

Re: How to hide certain category or product based on languag

Posted: Wed Jul 10, 2013 11:41 pm
by wtbi
Hi,

I would like to hide/restrict products by language, is this possible? Alternatively I would like to have different model no's, sku's and images for each language.

Is this the best approach?

Any help would be greatly appreciated.