Jump to content

Webservice example (SHELLSCRIPT)


jsolla

Recommended Posts

Hi there,

 

This is my first topic here.

 

I've been working with the PS webservice in order to create a SYNC system with an external ERP, and I would like to share my experience (if somebody is interested).

 

I've also other scripts to add products (including images), etc. If somebody interested...let me know.

 

Before using the script:

 

- This is a LINUX bash shellscript (will not work in windows)

- You have to install the "xmlstarlet" tools (apt-get install xmlstarlet in Ubuntu)

- You have to install "CURL" (apt-get install curl in Ubuntu)

- You have to create a config.inc file providing your PS webservice KEY and base URL for your SITE

 

config.inc contents:

 

WS_KEY=XYZABCDDJ5ERJTW81231231231231
BASEURL=http://YOURSITE.COM/prestashop/api

 

After creating the config.inc file on the SAME directory as ps_edtProduct.sh you can use it on a

regular console like this:

 

Change QUANTITY for product ID=1234:

./ps_edtProduct.sh 1234 QUANTITY=120

 

Change PRICE for product ID=1234:

./ps_edtProduct.sh 1234 PRICE=120

 

Change WHOLESALE_PRICE for product ID=1234:

./ps_edtProduct.sh 1234 WPRICE=120

 

 

I'm experiencing problems while uploading images, they upload fine, but PS refuses to use the image as "main" image until I manually edit the product. Somebody having this issue?

 

NOTE: I've uploaded the script renamed as .txt to avoid upload restrictions, please re-rename it after downloading

  • Like 1
Link to comment
Share on other sites

It seems that the attachment on my previous post didn't worked... I'm pasting here the code of the script:

 

#!/bin/sh
# Prestashop product edition using webservice.
# V: 1.0
# D: 7 Oct, 2011
# Author: Jorge Solla (jorgesolla (A T) gmail (D O T) com)
# WARNING!!!!!!: Before using this script, you *MUST* create a config.inc file with your shop parameters containing the following vars:
# WS_KEY=<YOUR PSHOP WEBSERVICE KEY>
# BASEURL=http://MYSHOP.COM/prestashop/api

# Get configuration params
source ./config.inc
if [ $# -lt 2 ]
then
echo "Usage: $0 <PSHOP_ID> [ACTIVE=1/0] [QUANTITY=value] [PRICE=value] [WPRICE=value] [REFERENCE=value] [DESC=\"value\" NOTE: double quotes!!] ";
echo "Example: $0 1234 ACTIVE=0"
echo "Example: $0 1234 STOCK=500"
exit 1;
fi
# Evaluate all parameters
PCOUNT=0
for param in $*
do
# Param 0 doesn't need evaluation -> Product ID in PS
if [ $PCOUNT -gt 0 ]
then
 eval $param
fi
# Inc param count
((PCOUNT++))
done;
# Get product ID from first param
ID=$1
# Retrieve current product on PRESTASHOP
CURL_REPLY=$( curl -s -u "$WS_KEY:"   $BASEURL/products/$ID )
if [ $? -gt 0 ]
then
echo "Error: Curl was unable to connect to PS webservice"
exit 1
fi
# Get reply length: When we request a non-existent product, prestashop returns OK but NO data
LENGTH=${#CURL_REPLY}
if [ $LENGTH -eq 0 ]
then
echo "Error: Product ID [$ID] does not exist"
exit 1
fi
# Search for errors on the reply
RESULT=$(echo "$CURL_REPLY" | grep "error")
LENGTH=${#RESULT}
if [ $LENGTH -gt 0 ]
then
echo "PS WEBSERVICE: Error detected fetching product ID [$ID]. Unable to continue.";
exit 1
fi

# Prestashop requires to delete some keys from the product XML to use it as an update XML
XML=$CURL_REPLY
XML=$(echo "$XML" |  xmlstarlet ed -d '/prestashop/product/position_in_category')
XML=$(echo "$XML" |  xmlstarlet ed -d '/prestashop/product/id_default_combination')
XML=$(echo "$XML" |  xmlstarlet ed -d '/prestashop/product/id_default_image')
XML=$(echo "$XML" |  xmlstarlet ed -d '/prestashop/product/manufacturer_name')
echo -n "Setting new values: "
# Insert new values into fields
if [ -n "${ACTIVE+x}" ]
then
echo -n "ACTIVE=$ACTIVE | "
XML=$(echo "$XML" |  xmlstarlet ed -u "/prestashop/product/active" -v "$ACTIVE")
fi
if [ -n "${QUANTITY+x}" ]
then
echo -n "QUANTITY=$QUANTITY |"
XML=$(echo "$XML" |  xmlstarlet ed -u "/prestashop/product/quantity" -v "$QUANTITY")
fi
if [ -n "${PRICE+x}" ]
then
echo -n "PRICE=$PRICE |" 
XML=$(echo "$XML" |  xmlstarlet ed -u "/prestashop/product/price" -v "$PRICE")
fi
if [ -n "${WPRICE+x}" ]
then
echo -n "WHOLESALE_PRICE=$WPRICE |"
XML=$(echo "$XML" |  xmlstarlet ed -u "/prestashop/product/wholesale_price" -v "$WPRICE")
fi
# Create a tmp file to update the article (Create a PUT file for CURL)
ARTICLE_DATA_FILE=$(mktemp)
echo -e "$XML" > $ARTICLE_DATA_FILE
# Update product
CURL_REPLY=$( curl -s -u "$WS_KEY:" -i -H "Content-Type:application/x-www-form-urlencoded" -X PUT -T "$ARTICLE_DATA_FILE"  $BASEURL/products/$ID )
# Check CURL return status
if [ $? -gt 0 ]
then
echo "Error: Curl was unable to PUT the modified product"
# Delete TMP file before quitting
rm $ARTICLE_DATA_FILE
exit 1
fi
# Delete TMP file
rm $ARTICLE_DATA_FILE

# Search for errors on the reply
RESULT=$(echo "$CURL_REPLY" | grep "error")
LENGTH=${#RESULT}
if [ $LENGTH -eq 0 ]
then
echo " -> OK";
exit 0
else
echo ""
echo "Error: PS Returns error while updating ID [$ID]";
echo "$CURL_REPLY"
exit 1
fi

Link to comment
Share on other sites

  • 2 weeks later...

I an such a newbie that i don'thave any idea wht your talking about. But is it possible with your script to import CSV files with URL images from the server from my supplier?

 

 

Yes, that's what I actually do.

 

You can write a little script (or program) that reads your CSV file and then call the script for updating the fields you need.

 

I still have problems with the Image uploading on my script -> My image uploader is able to upload the image, but when you go to the admin panel the image is "disabled" and you have to manually enable it.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Hi, can you explain to me what exactly what the code does? does it inmport an xml file and update products in the db?

 

Andy

 

Andy,

 

My script gets an article from the prestashop webservice, then modifies the fields it needs (quantity, price,etc) and sends it back to the prestashop server via webservice.

 

Please read about the prestashop webservice to know more.

Link to comment
Share on other sites

  • 1 month later...
  • 12 years later...

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...