Custom link on product page (with variants)

  • Posts: 17
  • Thank you received: 0
  • Hikashop Business
3 months 3 weeks ago #336056

-- HikaShop version -- : 4.4.3
-- Joomla version -- : 3.9.5
-- PHP version -- : 7.4.14

Hi, I need to make custom link (to demo site) for product and it's variants. What it the better solution: use some standard field or maybe custom field?

I've try do use "product_code" but it shows code in a strange way - while open product page it takes code form php but while switching on variants it takes code from java script (at least that's how I understood the action).

I want to past code to view like it:
<div class="button_cont" align="center">
<a class="butdemo" href=" mywebiste.com/ <?php echo $this->element->product_code; ?>" target="_blank" rel="nofollow"><span>See demo on-line</span></a>
</div>

Thank you in advance
Greg

Please Log in or Create an account to join the conversation.

  • Posts: 74595
  • Thank you received: 11508
  • MODERATOR
3 months 2 weeks ago #336057

Hi,

If it's the same URL with only the product_code being different for each product, you'll want to add your HTML/PHP to the show_default.php view file as a view file override, via the menu Display>Views.
And regarding the product code, you'll want to do something like that :

<?php
if(!empty($this->element->main))
echo $this->element->main->product_code,
else
echo $this->element->product_code;
?>
Otherwise, for products with variants, $this->element->product_code contains the product code of the default variant and not the one of the main product.

Please Log in or Create an account to join the conversation.

  • Posts: 17
  • Thank you received: 0
  • Hikashop Business
3 months 2 weeks ago #336075

Thank you for your replay, problem is that your code is not dynamic - it doesn't change while choosing variants (at least on my website).

When I use this code:
<span id="hikashop_product_code_main">
<?php echo $this->element->product_code;?>
</span>
it works perfectly but don't know how make a link from it. - after switching to variant link is breaking. Also notice that without id="hikashop_product_code_main" it's also not dynamic so it works like your code.

Please look on my site: opop.pl/product/43-cosmetica and try to choose color variant, your code doesn't switch.
I'v put there all 3 codes that we are talking about so can touch the problem.

Ps.: I'm new to HikaShop and made many changes in configuration and few in views... is it possible that I'm the reason :) ?

Regards
Greg

Please Log in or Create an account to join the conversation.

  • Posts: 74595
  • Thank you received: 11508
  • MODERATOR
3 months 2 weeks ago #336077

Hi,

My code is fine for what you want as long as you put it somewhere outside of a div with an id finishing by _main in show_default.php
If you add it inside a div with an id finishing by _main, then you have also to make a similar modification in product / show.php where you have a big foreach for the variants of the product to display their own HTML in _variant_xxx divs. And when you change the selected variant, the HTML in the _main divs will be replaced by the HTML in the corresponding _variant_xxx divs. So you want to make the modifications in both places so that the HTML can be replaced with HTML that you modified too.

Please Log in or Create an account to join the conversation.

  • Posts: 17
  • Thank you received: 0
  • Hikashop Business
3 months 2 weeks ago #336094

Hi Nicolas, did you look at my webiste? Please notice that your code is now odded outside of a div with an id finishing by _main and it's not changeing while choosing variants. To explain how it's done on example page here is the code:

<p> Hika Support 1st:
<?php
if(!empty($this->element->main))
echo $this->element->main->product_code;
else
echo $this->element->product_code;
?>
</p>

<p> Hika Support 2nd:
<?php
echo $this->element->product_code;
?>
</p>

<p>Good one, with id="hikashop_product_code_main"
<span id="hikashop_product_code_main">
<?php echo $this->element->product_code;?>
</span>
</p>

Whole problem is that I don't want to use div with an id finishing by _main but only then product code is changing as I want. Hope it's clear now ;)

Regards
Greg

Please Log in or Create an account to join the conversation.

  • Posts: 74595
  • Thank you received: 11508
  • MODERATOR
3 months 2 weeks ago #336095

Hi,


Well, I thought that you didn't want the product code to change.

If you want the product code of the link to change to the product code of the currently selected variant, then you need to have your code inside of a div with a _main id, and similar code in the product / show.php view file in the corresponding div with the _variant_xxx id. There is no alternative to that.

Please Log in or Create an account to join the conversation.

  • Posts: 17
  • Thank you received: 0
  • Hikashop Business
3 months 2 weeks ago #336156

Could you write some example of code in show.php file, please? I don't understand where to put it and how.

As I wrote on my first post maybe it's better to use custom field in my task - what you think about it?

Please Log in or Create an account to join the conversation.

  • Posts: 74595
  • Thank you received: 11508
  • MODERATOR
3 months 2 weeks ago #336160

Hi,

In show_default, you have this code by default:

<span id="hikashop_product_code_main" class="hikashop_product_code_main"><?php
			echo $this->element->product_code;
		?></span>
So I suppose you want to replace it with:
<span id="hikashop_product_code_main" class="hikashop_product_code_main">
<div class="button_cont" align="center">
<a class="butdemo" href=" mywebiste.com/ <?php echo $this->element->product_code; ?>" target="_blank" rel="nofollow"><span>See demo on-line</span></a>
</div>
</span>
In the product / show.php view file you can see this code:
	<div id="hikashop_product_code_<?php echo $variant_name; ?>" style="display:none;"><?php
		echo $variant->product_code;
	?></div>
So I suppose you want to replace it with:
	<div id="hikashop_product_code_<?php echo $variant_name; ?>" style="display:none;">
<div class="button_cont" align="center">
<a class="butdemo" href=" mywebiste.com/ <?php echo $variant->product_code; ?>" target="_blank" rel="nofollow"><span>See demo on-line</span></a>
</div></div>

Please Log in or Create an account to join the conversation.

  • Posts: 17
  • Thank you received: 0
  • Hikashop Business
3 months 4 days ago #336359

Hi, Nicolas, thak you very much. Code solved the problem.

Please Log in or Create an account to join the conversation.

Time to create page: 0.072 seconds
Powered by Kunena Forum