PHP to get product name for a form field on product page

  • Posts: 39
  • Thank you received: 0
5 months 4 weeks ago #304906

-- HikaShop version -- : 4.0.3
-- Joomla version -- : 3.9.3
-- PHP version -- : 7

I need to put a contact form on product pages so a person can submit a contact request regarding the product. For user convenience I want to automatically insert the product name into a field in the contact form.

I am trying to use this to get the product name:

$productName = hikashop_get('product_name');
echo $productName;

It does't return anything and no error occurs on the page. What am I doing wrong?

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

  • Posts: 66841
  • Thank you received: 9874
  • MODERATOR
5 months 4 weeks ago #304913

Hi,

hikashop_get is a function to initiliaze class instances, not to load elements information. So that code cannot possibly work. You would first have to get the current product id, then load the product class, then load the product data using the product class and then you can echo the product name.
Supposing that you're on the product page, you can use such code to do that:

$product_id = (int)hikashop_getCID('product_id');
$class = hikashop_get('class.product');
$product = $class->get($product_id);
echo $product->product_name;

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

  • Posts: 39
  • Thank you received: 0
5 months 4 weeks ago #304957

Thanks Nicolas that code works on a menu item that goes direct to a product page.

However, it isn't working correctly on a product page that is accessed via a Categories listing menu item (which is how I need it to work).

I have PM you with further details.

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

  • Posts: 66841
  • Thank you received: 9874
  • MODERATOR
5 months 3 weeks ago #304958

Hi,

The link you gave on your pm is not a product page. It's a category page. It displays a listing of the products of a category. And the id in the URL is the id of a category, not of a product.
So the first line of my code, which gets the id in the URL actually uses that category id as a product id.
I'm not sure what you're trying to do here.
Since it's a listing of products of a category, the list of the products is not available outside of the view files displaying the products listing.
If you add your code in the listing_img_title file of the product view via the menu Display>Views and change that first line to:
$product_id = $this->row->product_id;
then you'll get the product name of the current product for each product displayed on the listing. But since the product data is already loaded there, you could simply do:
echo $this->row->product_name;

PS: Please use instead our contact form and include a link to this thread in your message:
www.hikashop.com/support/contact-us.html

Last edit: 5 months 4 weeks ago by nicolas.

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

Time to create page: 0.056 seconds
Powered by Kunena Forum