Jump to content

Sending Null To Web Service -- Deliveries


techjp
 Share

Recommended Posts

I am working on adding carrier, price range, and weight range support to the PrestaSharp C# .NET client library for the PrestaShop web service.

 

The short form of this question is:

 

I need to send NULL values to the Web Service but sending NULL (tried many ways!) results in the value 0 (zero) being stored in the ps_deliveries table. How can I send NULL values to the Web Service within an XML file?

 

For development and testing I am using PrestaShop 1.6.1.1 in a Bitnami VM. Edit: This issue still exists & is unchanged in PrestaShop 1.6.1.4

 

 

Longer with lots of details:

 

Here is an image of the ps_deliveries table data. This data was entered through the web administration interface. The administration interface has not been modified in any way:

 

Mddh8Oi.png

 

 

As you can see, the id_shop, id_shop_group, and id_range_price values are NULL.

 

This test store does not have Multistore enabled. I checked the ps_shop and ps_shop_group tables and the only value available for id_shop is 1, and likewise the only value for id_shop_group is 1.

 

However if I set id_shop or id_shop_group to any value besides NULL the shipping prices no longer appear in the web administration interface.

 

Here is what the web admin interface looks like with NULL in the id_shop and id_shop_group columns:

 

iGst9Z7.png

 

 

 

However if I change id_shop and/or id_shop_group to any value besides NULL (0, 1, etc) I get the following results in the web admin interface:

 

0HpXGiG.png

 

 

To eliminate any possible issues with the PrestaSharp XML serializer and my own coding I have used the "Advanced Rest Client" Chrome Extension to send various XML requests to the PrestaShop Web Service. The requests complete successfully but the stored values in the ps_deliveries table do not reflect the requested NULL values. I have tried doing the following:

 

1. Send NULL values as NULL within XML tags for id_range_price, id_shop, and id_shop_group as follows: 

<?xml version="1.0" encoding="utf-8"?>
<prestashop>
<delivery>
  <id xmlns="">26</id>
  <id_carrier xmlns="">4</id_carrier>
  <id_range_price>NULL</id_range_price>
  <id_range_weight xmlns="">3</id_range_weight>
  <id_zone xmlns="">227</id_zone>
  <id_shop xmlns="">NULL</id_shop>
  <id_shop_group xmlns="">NULL</id_shop_group>
  <price xmlns="">22.000000</price>
</delivery>
</prestashop>
 
This results in a "400 Bad Request" return. The reason for this is that even though the web admin interface stores NULL in the id_range_price field (we only use weight ranges) the Web Service requires a value be set for id_range_price and will fail if it is set to NULL.
 
 

2. Send NULL values as NULL within XML tags as follows, except set 0 (zero) for id_range_price:

<?xml version="1.0" encoding="utf-8"?>
<prestashop>
<delivery>
  <id xmlns="">26</id>
  <id_carrier xmlns="">4</id_carrier>
  <id_range_price>0</id_range_price>
  <id_range_weight xmlns="">3</id_range_weight>
  <id_zone xmlns="">227</id_zone>
  <id_shop xmlns="">NULL</id_shop>
  <id_shop_group xmlns="">NULL</id_shop_group>
  <price xmlns="">22.000000</price>
</delivery>
</prestashop>

This results in a "200 OK" response with the following data returned:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<delivery>
<id><![CDATA[26]]></id>
<id_carrier xlink:href="https://192.168.10.105/prestashop/api/carriers/4"><![CDATA[4]]></id_carrier>
<id_range_price><![CDATA[0]]></id_range_price>
<id_range_weight xlink:href="https://192.168.10.105/prestashop/api/weight_ranges/3"><![CDATA[3]]></id_range_weight>
<id_zone xlink:href="https://192.168.10.105/prestashop/api/zones/227"><![CDATA[227]]></id_zone>
<id_shop><![CDATA[NULL]]></id_shop>
<id_shop_group><![CDATA[NULL]]></id_shop_group>
<price><![CDATA[22.000000]]></price>
</delivery>
</prestashop>

Looks great! Except when I check the table I can see that id_shop and id_shop_group have both been set to 0 (zero) and not to NULL! Also, id_range_price is (of course) set to 0 (zero) as well when it really should be NULL based on how the web admin interface saves data.

 

 

3. We have tried sending NULL formatted in different ways as well. Empty tags, for example:

<?xml version="1.0" encoding="utf-8"?>
<prestashop>
<delivery>
  <id xmlns="">26</id>
  <id_carrier xmlns="">4</id_carrier>
  <id_range_price>0</id_range_price>
  <id_range_weight xmlns="">3</id_range_weight>
  <id_zone xmlns="">227</id_zone>
  <id_shop xmlns=""></id_shop>
  <id_shop_group xmlns=""></id_shop_group>
  <price xmlns="">22.000000</price>
</delivery>
</prestashop>
 
Returns "200 OK" with the following:
<?xml version="1.0" encoding="UTF-8" ?>
<prestashop>
 <delivery>
 <id>
 <![CDATA[
26
]]>
 </id>
 <id_carrier xlink:href="https://192.168.10.105/prestashop/api/carriers/4">
 <![CDATA[
4
]]>
 </id_carrier>
 <id_range_price>
 <![CDATA[
0
]]>
 </id_range_price>
 <id_range_weight xlink:href="https://192.168.10.105/prestashop/api/weight_ranges/3">
 <![CDATA[
3
]]>
 </id_range_weight>
 <id_zone xlink:href="https://192.168.10.105/prestashop/api/zones/227">
 <![CDATA[
227
]]>
 </id_zone>
<id_shop />
<id_shop_group />
 <price>
 <![CDATA[
22.000000
]]>
 </price>
 </delivery>
</prestashop>
Once again however the values are stored as 0 (zero) and not NULL.
 
4. Other ways we have tried include the "<id_shop />" empty tag format (same 0 value stored) as well as specifying NULL as "<id_shop xsi:nil="true"/>" (results in a "500 Internal Server Error" return). We have also tried just excluding the NULL tags completely but this also results in a 0 (zero) value being stored.
 

 

So, as far as I can tell, I have tried everything. Of course there's always a chance I have missed something! From my side however it looks like the PrestaShop Web Service doesn't support sending NULL values but we definitely need to send NULL values to support adding & updating data in the ps_deliveries table.

 

Help?

 

Edit: This issue still exists & is unchanged in PrestaShop 1.6.1.4

Edited by techjp (see edit history)

Share this post


Link to post
Share on other sites

  • 2 months later...
  • 5 years later...

I have the same problem in 1.6.1.20 version. Have you sorted this out ?

UPDATE: After some research, it turned out, that it's actually very simple. All you need to do to solve it, is to paste this function into /classes/Delivery.php:

public function add($autodate = true, $null_values = true)
{
    if (!parent::add($autodate, $null_values)) {
        return false;
    }

    return true;
}

or even better is to do something like this (to limit this behaviour changes just to webservice):

protected $webserviceParameters = array(
    'objectMethods' => array(
        'add' => 'addWs',
    ),
    'objectsNodeName' => 'deliveries',
    'fields' => array(
        'id_carrier' => array('xlink_resource' => 'carriers'),
        'id_range_price' => array('xlink_resource' => 'price_ranges'),
        'id_range_weight' => array('xlink_resource' => 'weight_ranges'),
        'id_zone' => array('xlink_resource' => 'zones'),
    )
);


public function addWs($autodate = true, $null_values = true)
{
    if (!parent::add($autodate, $null_values)) {
        return false;
    }

    return true;
}

 

Edited by karcharoth2 (see edit history)

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More