Jump to content

[solved] problem with price import - Including new PHP in tpl.


Arkataev

Recommended Posts

Hi!

I have this problem.

I need the price for each product to be imported with xml. request from the other server and displayed in pruduct.tpl.

 

I have the following xml:

 

$xml="
<digiseller.request>
<id_goods></id_goods>
</digiseller.request>
";

function _GetAnswer($address, $xml){
$ch = curl_init($address);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result=curl_exec($ch);
return $result;
}

$answer=_GetAnswer("http://shop.digiseller.ru/xml/personal_goods_info.asp", $xml);

$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->wmr as $price_goods) {
echo $price_goods;
};

$smarty->assign('digsale',$xml);

 

 

I need prestashop to take each product reference value and put it to <id_goods>HERE</id_goods> in xml. request. Than I need to assign the xml response to a variable and display it in the price field on product page.

 

I input the xml request to productcontroller.php and assigned the variable 'digsale'. But I i wonder how to insert the product reference variable into the xml request. Could anyone help me, please?

 

[sOLVED]

Edited by vekia (see edit history)
Link to comment
Share on other sites

You could do something like this. First add a static text REPLACEME so you have a needle to find

$xml="
<digiseller.request>
<id_goods>REPLACEME</id_goods>
</digiseller.request>
";

 

Then use string replace function to replace REPLACEME with the product reference.

$xml=str_replace('REPLACEME', $product->reference, $xml)

 

Then you can send that request and use the response information as you like. However if I understand you properly, that will only change the price display on the product page. If the customer tries to purchase this product, it will not use this price during checkout.

  • Like 1
Link to comment
Share on other sites

Unfortunately it refuse working:( I did as you adviced:

 

$xml="
<digiseller.request>
<id_goods>REPLACEME</id_goods>
</digiseller.request>
";
$xml=str_replace('REPLACEME', $product->reference, $xml);
function _GetAnswer($address, $xml){
  $ch = curl_init($address);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  $result=curl_exec($ch);
  return $result;
}

$answer=_GetAnswer("http://shop.digiseller.ru/xml/personal_goods_info.asp", $xml);

$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->wmr as $price_goods) {
echo $price_goods;
};
$smarty->assign('digsale',$xml);
  }

 

I tried different placement throughout the productcontroller.php but without any use.

Maybe I did smth wrong in tpl.? I just put an {$digsale} variable into product tpl. waiting it will return the proper value.But there's no any sign of it. I suppose this got to be lil' bit trickier...

Link to comment
Share on other sites

However if I understand you properly, that will only change the price display on the product page. If the customer tries to purchase this product, it will not use this price during checkout.

 

You're right, but that's exactly what I need. That's because I use an external checkout: after hitting the 'Buy" button, client is redirected to the outside payment system, where he also gets his product (digital).

Link to comment
Share on other sites

Fortunately i got it work, in some words...

$xml="
<digiseller.request>
<id_goods>REPLACEME</id_goods>
</digiseller.request>
";
$xml=str_replace("REPLACEME", 122332, $xml);
function _GetAnswer($address, $xml){
  $ch = curl_init($address);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  $result=curl_exec($ch);
  return $result;
}

$answer=_GetAnswer("http://shop.digiseller.ru/xml/personal_goods_info.asp", $xml);

$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->wmr as $price_goods) ;


	  $this->context->smarty->assign(array(
'digsale'=>$price_goods  

));

 

This returns the price of a product from the outside server in right way. But in the mean time, when I use $product->reference instead of that fixed number here

$xml=str_replace('REPLACEME', 31313134, $xml);

This returns nothing. Apparantly this is about $product->reference

p.s.

I put the code to /controllers/front/productcontroller.php

Edited by vega17 (see edit history)
Link to comment
Share on other sites

Got it!

[/size]
$xml="
<digiseller.request>
<id_goods>REPLACEME</id_goods>
</digiseller.request>
";
$xml=str_replace("REPLACEME", (int)$this->product->reference, $xml);
function _GetAnswer($address, $xml){
  $ch = curl_init($address);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  $result=curl_exec($ch);
  return $result;
}
$answer=_GetAnswer("http://shop.digiseller.ru/xml/personal_goods_info.asp", $xml);
$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->wmr as $price_goods) ;

			  $this->context->smarty->assign(array(
'digsale'=>$price_goods 

));

 

This returns the right response formed with a product reference.

Link to comment
Share on other sites

  • 2 months later...

Hi! The problem was to change price display on product page. I needed to use data from external source with a help of xml response. And one more thing. An xml requst had to be formed using data in "product reference" field.

So I put the following code into productcontroller itself (controllers->front). It gows on line 251

protected function assignPriceAndTax()
{
.....some code here
$xml="
<digiseller.request>
<id_goods>REPLACEME</id_goods>
</digiseller.request>
";
$xml=str_replace("REPLACEME", (int)$this->product->reference ,$xml);
function _GetAnswer($address, $xml){
  $ch = curl_init($address);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  $result=curl_exec($ch);
  return $result;
}

$answer=_GetAnswer("http://shop.digiseller.ru/xml/personal_goods_info.asp", $xml);

$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->wmr as $price_goods) ;
$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->wmu as $wmu
);
$xmlres = simplexml_load_string($answer);
foreach ($xmlres->price_goods->pcr as $pcr
);
$xmlres = simplexml_load_string($answer);
foreach ($xmlres->reward as $reward
);
$xmlres = simplexml_load_string($answer);
foreach ($xmlres->reward_summ as $arew
);
$xmlres = simplexml_load_string($answer);
foreach ($xmlres->reward_curr as $arewc
);

$this->context->smarty->assign(array(
'digsale'=>$price_goods,
'digsalewmu'=>$wmu,
'digsalepcr'=>$pcr,
'digsalereward'=>$reward,
'digsalearew'=>$arew,
'digsalearewc'=>$arewc,
));
}

 

This thing returns data needed and then it assignes the proper variables for it. As a result I'm availabe to use that variables in product.tpl. To do this you need to just input them in your tpl. in {$yourvariable} form.

 

Now I have a product price formed with a help of data from an external source.

 

p.s. I use ps 1.5.2.0

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...