How to get product price?

  • Posts: 82
  • Thank you received: 2
  • Hikashop Multisite
6 years 1 week ago #291427

-- HikaShop version -- : 3.4.0
-- Joomla version -- : 3.8.6
-- PHP version -- : 7.1.15
-- Browser(s) name and version -- : Chrome

Hi
I would like how to get the product price based on product ID.
Currently I have an API working to get on the product name based on product ID and need to include the product price too.

public function get_product_name() {
		//$this->check_request_token();
		$data = new stdClass();
		$config =& hikashop_config();
		$id = JFactory::getApplication()->input->post->getArray();
		$productClass = hikashop_get('class.product');
		$dt = [];
		$datas = [];
		
		foreach($id[dataArrayPost] as $key => $value){
			if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){
				return false; 
			}
			$dt['prodID'][] = $value[productID];
			$datas['quota'][] = $value[remainingQuota];
		}
		foreach($dt['prodID'] as $key => $value){
			$datas['productName'][] = $productClass->get($value)->product_name;
		} 
		echo json_encode( ($datas));
		JFactory::getApplication()->close();
	}

This code will return the product name and required quantity. Any ways that i can include product price by using the same code?

Last edit: 6 years 1 week ago by alainz.

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

  • Posts: 81515
  • Thank you received: 13069
  • MODERATOR
6 years 1 week ago #291428

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

  • Posts: 82
  • Thank you received: 2
  • Hikashop Multisite
6 years 6 days ago #291448

When I'm doing like that I'm getting array of null. Can you check if I'm doing the wrong way.

public function get_product_name() {
		//$this->check_request_token();
		$data = new stdClass();
		$config =& hikashop_config();
		$id = JFactory::getApplication()->input->post->getArray();
		$productClass = hikashop_get('class.product');
		$currencyClass = hikashop_get('class.currency');
		$dt = [];
		$datas = [];
		//$product=$productClass->get(134);
		
		foreach($id[dataArrayPost] as $key => $value){
			if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){
				return false; 
			}
			$dt['prodID'][] = $value[productID];
			$datas['quota'][] = $value[remainingQuota];
			//$ids = array($product->product_id);
			$currencyClass->getPrices($products, $dt['prodID'], hikashop_getCurrency(), $config->get('main_currency'), hikashop_getZone(), $config->get('discount_before_tax'));
			$datas['price'][] = $product->prices;
		}
		foreach($dt['prodID'] as $key => $value){
			$datas['productName'][] = $productClass->get($value)->product_name;
		} 
		echo json_encode( ($datas));
		JFactory::getApplication()->close();
	}



Thanks in advanced.

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

  • Posts: 81515
  • Thank you received: 13069
  • MODERATOR
6 years 6 days ago #291469

Hi,

Your code is not correct.
It should be:

public function get_product_name() {
		//$this->check_request_token();
		$data = new stdClass();
		if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){
			return false; 
		}
		$config =& hikashop_config();
		$id = JFactory::getApplication()->input->post->getArray();
		$productClass = hikashop_get('class.product');
		$currencyClass = hikashop_get('class.currency');
		$dt = [];
		$datas = [];
		
		
		foreach($id[dataArrayPost] as $key => $value){
			
			$product = $productClass->get($value[productID]);
			$dt['prodID'][] = $value[productID];
			$datas['quota'][] = $value[remainingQuota];
			$ids = array($product->product_id);
			$currencyClass->getPrices($product, $ids, hikashop_getCurrency(), $config->get('main_currency'), hikashop_getZone(), $config->get('discount_before_tax'));
			$datas['price'][] = $product->prices;
		}
		foreach($dt['prodID'] as $key => $value){
			$datas['productName'][] = $productClass->get($value)->product_name;
		} 
		echo json_encode( ($datas));
		JFactory::getApplication()->close();
	}

The following user(s) said Thank You: alainz

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

Time to create page: 0.040 seconds
Powered by Kunena Forum