I tried many approaches online explaining the issue, but didn't find the one could fit with my need.
I want to make a share to whatsapp
link on my website for each product, including product name, line-break and link
. Something like this:
Product Name [/r/n]
https://....
I'm using OpenCart 3. Here is php side code:
'whatsapp_text' => $result['manufacturer'] . ' - ' . $result['model'] . ' - ' . $result['name']
. $this->encodeURIComponent('\r\n' . $this->url->link('product/product', 'product_id=' . $result['product_id']))
Above code returns this:
https://api.whatsapp.com/send?text=Nurinu%20-%201310%20-%20Bra%5Cr%5Cnhttp%3A%2F%2Fwww.myweb.com%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D61
According to this page (https://github.com/kriskbx/whatsapp-sharing/issues/16#issuecomment-294854157) it's possible to use window.encodeURIComponent(whatsappMessage)
to have a line-break
, but I don't know how to combine it with my php code or use it in html side:
<a href="https://api.whatsapp.com/send?text={{ product.whatsapp_text }}" data-action="share/whatsapp/share">Whatsapp</a>
UPDATE
I forgot to include the function (encodeURIComponent):
function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
I fixed the issue according to this article (http://webdevelopmentscripts.com/35-share-a-link-on-whatsapp-from-website) and CBroe
's suggestion on using double quote
for line break "\n"
:
'whatsapp_text' => $result['manufacturer'] . '-' . $result['model'] . '-' . $result['name']
. rawurlencode("\n" . $this->url->link('product/product&product_id=' . $result['product_id']))
<a href="whatsapp://send?text={{ product.whatsapp_text }}">whatsapp</a>
The result is exactly what I want:
Moonslictese-251-Bra
http://www.example.com/index.php?route=product/product&product_id=46
Also I could use encodeURIComponent
:
javascript:void(location.href='whatsapp://send?text='+encodeURIComponent({{ product.whatsapp_text }}))