Jump to content

Showing products only for specific languages?


AndriusS

Recommended Posts

I am not aware of such module at the moment (but anyone correct me if I am wrong), so the way I'd go would be to setup a multistore faking the language url, and allow only one language per store. Then, you would setup a fake language block which points to the other site instead of changing language (with links, basically, which point to the other store)

  • Thanks 1
Link to comment
Share on other sites

I ended up hardcoding the function to my site.

Might as well share it. Not the best code but it works fine (at least, for now!).

AdminProductsController.php :

	private function updateProductShowByLang($product_id){
		$sql = 'DELETE FROM `'._DB_PREFIX_.'product_show_by_lang` WHERE `product_id` = '.$product_id;
		@Db::getInstance()->Execute($sql);
		foreach($_POST['show_by_lang'] as $lang_id){
			$sql = 'INSERT INTO `'._DB_PREFIX_.'product_show_by_lang` SET `product_id` = '.$product_id.', lang_id = '.$lang_id;
			@Db::getInstance()->Execute($sql);
		}
	}

Product.php :

	public static function getProductShows($product_id){
		$productShows = Db::getInstance()->ExecuteS('SELECT `lang_id` FROM `'._DB_PREFIX_.'product_show_by_lang` WHERE `product_id` ='.$product_id); 
		$list = array();
		foreach($productShows as $show){
			$list[] = $show['lang_id'];
		}
		return $list;

	}
	public static function getProductShowsDefLang($product_id,$lang_id){
		$productShows = Db::getInstance()->ExecuteS('SELECT `lang_id` FROM `'._DB_PREFIX_.'product_show_by_lang` WHERE `product_id` ='.$product_id.' AND `lang_id` ='.$lang_id); 
		return sizeof($productShows)?false:true;
	}

Now, for the Back Office (adding/updating product).

Using this:

$this->updateProductShowByLang($object->id);

After the checking is complete. So I mostly winged it and added it to submitAddproduct() (messed up camel case function name :wacko: ) after Hook::updateProduct($object); and Hook::addProduct($object);

 

Once we are done with the back-end stuff, we add it to out product view, the initFormInformations() in AdminProductsController.php (I just added it to the start of the function):

            // Assigning for "show only in languages..."
            $productShows = array();
            if($product->id)
                $productShows = Product::getProductShows($product->id);
                $table = "";
                $table .= '		<tr><td colspan="2"><hr style="width:100%;" />'.$this->l('Shows only in these languages:').'</td></tr>';
		foreach ($this->_languages as $language){
			$table .= '<tr><td>'.$language['name'].'</td><td><input '.(in_array($language['id_lang'],$productShows) || !sizeof($productShows)?'checked="checked"':'').' type="checkbox" name="show_by_lang[]" value="'.$language['id_lang'].'" /></td></tr>';
		}
		$table .='		<tr><td colspan="2"><hr style="width:100%;" /></td></tr>';
                $table .= "";
                $this->context->smarty->assign(array(
                        "productShowsTable" => $table,
                    
                ));

Now, for the Front Office...

Edited ProductController.php, init():

if (!Validate::isLoadedObject($this->product))

to

if (!Validate::isLoadedObject($this->product) || Product::getProductShowsDefLang($this->product->id,intval($this->context->cookie->id_lang)))

And then, the category view only needs a fix for the query. File Category.php, the function is getProducts(), before:

WHERE product_shop.`id_shop` =...

added:

INNER JOIN `'._DB_PREFIX_.'product_show_by_lang` sbl ON (p.`id_product` = sbl.`product_id` AND sbl.`lang_id` = '.(int)$id_lang.')

For the MySQL part, you obviously need to create the table 'product_show_by_lang' with product_id and lang_id as integers.

 

After it's all done, the only thing left to do is to add it to a template. So, I used informations.tpl like this:

                <table style="float:right;margin-right: 100px; background-color: white;padding: 10px;">
                    {$productShowsTable}
                </table>

Wherever it works. I just added it after the Tags.

 

 

I hope, I'm not forgetting anything as I'm doing it after I'm done with it.

Edited by AndriusS (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...