Jump to content

Static token for add to cart in Block Viewed product module


Recommended Posts

Hello everyone

 

I need help in a module block viewed products . I need Add to cart button in Block viewed product module , I have added same add to cart code which is in product-list.tpl file in blockview.tpl. But issue is when I add a product, product is added but price get added twice.  I am generating token in my blockview.php file and using the below code...

 

Website link : http://brands200.com/no/fc-barcelona-t-skjorte-bla-Burlington-35.html

 

blockview.php file code :

public function hookRightColumn($params)
	{
		$pVNumber = (int)Configuration::get('PRODUCTS_VIEWED_NBR') + 1;   // Peter
		$productsViewed = (isset($params['cookie']->viewed) && !empty($params['cookie']->viewed)) ? array_slice(array_reverse(explode(',', $params['cookie']->viewed)), 0, $pVNumber) : array();   // Peter
		
		//print_r($productsViewed);	
		// Peter 
		$id_product = (int)Tools::getValue('id_product');
		if ($id_product) 
		{
			if(($key = array_search($id_product, $productsViewed)) !== false) {
				unset($productsViewed[$key]);
			}
		}
		//  \\Peter 

		if (count($productsViewed))
		{
			$defaultCover = Language::getIsoById($params['cookie']->id_lang).'-default';

			$productIds = implode(',', array_map('intval', $productsViewed));
			$productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
			SELECT MAX(image_shop.id_image) id_image, p.id_product, p.price, il.legend, product_shop.active, pl.name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite
			FROM '._DB_PREFIX_.'product p
			'.Shop::addSqlAssociation('product', 'p').'
			LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').')
			LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'.
			Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
			LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = image_shop.id_image)
			LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').')
			WHERE p.id_product IN ('.$productIds.')
			AND pl.id_lang = '.(int)($params['cookie']->id_lang).'
			AND cl.id_lang = '.(int)($params['cookie']->id_lang).'
			GROUP BY product_shop.id_product'
			);

			$productsImagesArray = array();
			foreach ($productsImages as $pi)
				$productsImagesArray[$pi['id_product']] = $pi;

			$productsViewedObj = array();
			foreach ($productsViewed as $productViewed)
			{
				$obj = (object)'Product';
				if (!isset($productsImagesArray[$productViewed]) || (!$obj->active = $productsImagesArray[$productViewed]['active']))
					continue;
				else
				{
					//echo '<pre>';
					$obj->id = (int)($productsImagesArray[$productViewed]['id_product']);
					//echo $obj->id.'<br>';
					$obj->otherprice = (int)$productsImagesArray[$productViewed]['price'];

					$productpricenew = Product::getPriceStatic($obj->id, true, NULL, 6);
					//echo $productpricenew;
					//echo $productpricenew.'<br>';
					 $pricefinal = Tools::ps_round($productpricenew, 2);

					 //$currency_array =   Currency::getCurrencies($object = false, $active = 1);
					// print_r($currency_array);
					//$currencyid= $this->context->currency->id;
					//print_r($productsImagesArray[$productViewed]);
					

					  $current_currency = $this->context->currency->iso_code;
					  $default_currency = Currency::getDefaultCurrency()->iso_code;
					  $currency_array =   Currency::getCurrencies($object = false, $active = 1);

	                                  $second_currency = 'EUR';
	                                     if($current_currency == $second_currency)
	                                     {
		                               foreach ($currency_array as $arr){
			                       if ((string)$arr['iso_code'] == $second_currency ){
			    	
			                       	//echo $productpricenew.(float)$arr['conversion_rate'].'<br>';
			                  	$multiply = $obj->otherprice * $arr['conversion_rate'];			    	
			    	                $pricefinal = Tools::ps_round($multiply,$arr['decimal']);
			                      }
		                     }
		              }			
					
		
					$obj->id = (int)($productsImagesArray[$productViewed]['id_product']);
					$obj->price = $pricefinal;
					$obj->id_image = (int)$productsImagesArray[$productViewed]['id_image'];
					$obj->cover = (int)($productsImagesArray[$productViewed]['id_product']).'-'.(int)($productsImagesArray[$productViewed]['id_image']);
					$obj->legend = $productsImagesArray[$productViewed]['legend'];
					$obj->name = $productsImagesArray[$productViewed]['name'];
					$obj->description_short = $productsImagesArray[$productViewed]['description_short'];
					$obj->link_rewrite = $productsImagesArray[$productViewed]['link_rewrite'];
					$obj->category_rewrite = $productsImagesArray[$productViewed]['category_rewrite'];
					// $obj is not a real product so it cannot be used as argument for getProductLink()
					$obj->product_link = $this->context->link->getProductLink($obj->id, $obj->link_rewrite, $obj->category_rewrite);
					$obj->currentcurrency = $current_currency;
					$obj->token = Tools::getToken(false);

					if (!isset($obj->cover) || !$productsImagesArray[$productViewed]['id_image'])
					{
						$obj->cover = $defaultCover;
						$obj->legend = '';
					}
					$productsViewedObj[] = $obj;
				}
			}

			if (!count($productsViewedObj))
				return;

			$this->smarty->assign(array(
				'productsViewedObj' => $productsViewedObj,
				'mediumSize' => Image::getSize('medium')));

			return $this->display(__FILE__, 'blockviewed.tpl');
		}
		return;
	}

Blockview.tpl

<a class="button ajax_add_to_cart_button btn btn-default" href="{$link->getPageLink('cart',false, NULL, "add=1&id_product={$viewedProduct->id|intval}&token={$viewedProduct->token}", false)|escape:'html':'UTF-8'}" rel="nofollow" title="{l s='Add to cart'}" data-id-product="{$viewedProduct->id|intval}">
                                            <span>Add to Cart</span>
                                        </a>


Please help. 

 

 

Link to comment
Share on other sites

  • 1 year later...

just a thought does this only happen when its euro because if so when you throw the below part could result in such a think not for sure as I just quickly glanced at the possible problems.

   if($current_currency == $second_currency)
	                                     {
		                               foreach ($currency_array as $arr){
			                       if ((string)$arr['iso_code'] == $second_currency ){
			    	
			                       	//echo $productpricenew.(float)$arr['conversion_rate'].'<br>';
			                  	$multiply = $obj->otherprice * $arr['conversion_rate'];			    	
			    	                $pricefinal = Tools::ps_round($multiply,$arr['decimal']);
			                      }

But this is the only area I see that it can add two prices and as an example of the way I got my add to cart link to work was using this below as the href in my tpl file.

href='{$link->getPageLink('cart', true, null, 'qty=1&id_product={$product.id_product|intval}&token={$static_token}&add')|escape:'html':'UTF-8'}' data-id-product='{$product.id_product|intval}'' title='{l s='Add to cart' mod='specialsslider'}' target='_self'

hope this helps...

Edited by ShopSaveNEarn (see edit history)
  • Thanks 1
Link to comment
Share on other sites

×
×
  • Create New...