Web Categories

Listing all Web Categories

A GET request to /api/setup/web_categories/ is used to list all web categories.

Using count and offset restricts the number of returned products.

Parameter Description
count Limit the number of results. This can improve performance.
offset The start position of results, used for paging result sets. Can be used with count parameter.
"""
List all web categories using the OnSite API.
"""
import requests
from xml.dom import minidom

# Customize these to your install.
ONSITE_HOST = 'localhost'
ONSITE_PORT = 9630
ONSITE_USERNAME = 'lightspeed'
ONSITE_PASSWORD = 'admin'
APP_ID = 'com.lightspeed.onsite.demo'
APP_VERSION = '1.0'
APP_PRIVATE_ID = '12345678-90ab-cdef-1234-567890abcdef'

web_categories_url = 'https://%s:%d/api/setup/web_categories/?count=10&offset=0' % (ONSITE_HOST, ONSITE_PORT)
logout_url = 'https://%s:%d/api/sessions/current/logout/' % (ONSITE_HOST, ONSITE_PORT)

# Setup a session for the http request.
session = requests.Session()
session.auth = (ONSITE_USERNAME, ONSITE_PASSWORD)
session.headers.update({
    'user-agent': '%s/%s' % (APP_ID, APP_VERSION),
    'x-pappid': APP_PRIVATE_ID})
session.verify = False
session.stream = True

# Send the request to list all web categories.
get_response = session.get(web_categories_url)
assert get_response.status_code == 200

# Log out.
logout_response = session.post(logout_url)
assert logout_response.status_code == 204

# Get the response and print it.
response_xml = minidom.parseString(get_response.text)
print(response_xml.toprettyxml())
<!-- Response -->

<?xml version="1.0" ?>
<categories total_count="1">
    <category id="1" uri="https://localhost:9630/api/setup/web_categories/1/"/>
</categories>

Warning

This request can time-out if there are too many web categories to return. Use limit and offset parameters to mitigate this.

Creating a new Web Category

A POST request to /api/setup/web_categories/ is used to create a web category.

"""
Create a web category using the OnSite API.
"""
import requests
from xml.dom import minidom

# Customize these to your install.
ONSITE_HOST = 'localhost'
ONSITE_PORT = 9630
ONSITE_USERNAME = 'lightspeed'
ONSITE_PASSWORD = 'admin'
APP_ID = 'com.lightspeed.onsite.demo'
APP_VERSION = '1.0'
APP_PRIVATE_ID = '12345678-90ab-cdef-1234-567890abcdef'

web_categories_url = 'https://%s:%d/api/setup/web_categories/' % (ONSITE_HOST, ONSITE_PORT)
logout_url = 'https://%s:%d/api/sessions/current/logout/' % (ONSITE_HOST, ONSITE_PORT)

# This is the web category that will be created.
web_category_xml = """
<category>
    <name>Used Items</name>
    <parent>
        <category id="0" />
    </parent>
    <list_order>0</list_order>
</category>
"""

# Create a session. This will persist cookies across all requests.
session = requests.Session()
session.auth = (ONSITE_USERNAME, ONSITE_PASSWORD)
session.headers.update({
    'user-agent': '%s/%s' % (APP_ID, APP_VERSION),
    'x-pappid': APP_PRIVATE_ID})
session.verify = False

# Send the request to create the web category.
post_response = session.post(web_category_xml, data=web_category_xml)
assert post_response.status_code == 201

# Log out.
logout_response = session.post(logout_url)
assert logout_response.status_code == 204

# Get the response and print it.
response_xml = minidom.parseString(post_response.text)
print(response_xml.toprettyxml())
<!-- Response -->

<?xml version="1.0" ?>
<category id="1" uri="https://localhost:9630/api/setup/web_categories/1/">
    <name>Used Items</name>
    <list_order>0</list_order>
    <type>web</type>
    <depth>0</depth>
    <children/>
    <category_products/>
</category>

Fetching a Web Category

A GET request to /api/setup/web_categories/{id}/ is used to get a web category.

Note

The rendered category will only include up to the first 10 assigned products.

"""
Get a web category using the OnSite API.
"""
import requests
from xml.dom import minidom

# Customize these to your install.
ONSITE_HOST = 'localhost'
ONSITE_PORT = 9630
ONSITE_USERNAME = 'lightspeed'
ONSITE_PASSWORD = 'admin'
APP_ID = 'com.lightspeed.onsite.demo'
APP_VERSION = '1.0'
APP_PRIVATE_ID = '12345678-90ab-cdef-1234-567890abcdef'

web_category_id = 1
web_category_url = 'https://%s:%d/api/setup/web_categories/%d/' % (ONSITE_HOST, ONSITE_PORT, web_category_id)
logout_url = 'https://%s:%d/api/sessions/current/logout/' % (ONSITE_HOST, ONSITE_PORT)

# Setup a session for the http request.
session = requests.Session()
session.auth = (ONSITE_USERNAME, ONSITE_PASSWORD)
session.headers.update({
    'user-agent': '%s/%s' % (APP_ID, APP_VERSION),
    'x-pappid': APP_PRIVATE_ID})
session.verify = False
session.stream = True

# Send the request to get a web category.
get_response = session.get(web_category_url)
assert get_response.status_code == 200

# Log out.
logout_response = session.post(logout_url)
assert logout_response.status_code == 204

# Get the response and print it.
response_xml = minidom.parseString(get_response.text)
print(response_xml.toprettyxml())
<!-- Response -->

<?xml version="1.0" ?>
<category id="1" uri="https://localhost:9630/api/setup/web_categories/1/">
    <name>Used Items</name>
    <list_order>0</list_order>
    <type>web</type>
    <depth>0</depth>
    <children/>
    <category_products>
        <category_product id="33" uri="https://localhost:9630/api/setup/web_categories/1/products/33/">
            <product full_render="false" id="33" uri="https://localhost:9630/api/products/33/">
                <code>333-003-02:46PM</code>
                <flags>
                    <master_model>false</master_model>
                </flags>
                <description>Lucy's Bikini-02:46PM</description>
                <product_photos/>
            </product>
        </category_product>
    </category_products>
</category>

Listing Products assigned to a Web Category

A GET request to /api/setup/web_categories/{id}/products/?count={count}&offset={offset} is used to page through the assigned products.

"""
Get a web category using the OnSite API.
"""
import requests
from xml.dom import minidom

# Customize these to your install.
ONSITE_HOST = 'localhost'
ONSITE_PORT = 9630
ONSITE_USERNAME = 'lightspeed'
ONSITE_PASSWORD = 'admin'
APP_ID = 'com.lightspeed.onsite.demo'
APP_VERSION = '1.0'
APP_PRIVATE_ID = '12345678-90ab-cdef-1234-567890abcdef'

web_category_id = 1
web_category_url = 'https://%s:%d/api/setup/web_categories/%d/products/?count=10&offset=0' % (ONSITE_HOST, ONSITE_PORT, web_category_id)
logout_url = 'https://%s:%d/api/sessions/current/logout/' % (ONSITE_HOST, ONSITE_PORT)

# Setup a session for the http request.
session = requests.Session()
session.auth = (ONSITE_USERNAME, ONSITE_PASSWORD)
session.headers.update({
    'user-agent': '%s/%s' % (APP_ID, APP_VERSION),
    'x-pappid': APP_PRIVATE_ID})
session.verify = False
session.stream = True

# Send the request to get a web category.
get_response = session.get(web_category_url)
assert get_response.status_code == 200

# Log out.
logout_response = session.post(logout_url)
assert logout_response.status_code == 204

# Get the response and print it.
response_xml = minidom.parseString(get_response.text)
print(response_xml.toprettyxml())
<!-- Response -->

<?xml version="1.0" ?>
<category_products>
    <category_product id="33" uri="https://localhost:9630/api/setup/web_categories/1/products/33/">
        <product full_render="false" id="33" uri="https://localhost:9630/api/products/33/">
            <code>333-003-02:46PM</code>
            <flags>
                <master_model>false</master_model>
            </flags>
            <description>Lucy's Bikini-02:46PM</description>
            <product_photos/>
        </product>
    </category_product>
</category_products>

Warning

This request can time-out if there are too many web categories to return. Use limit and offset parameters to mitigate this.

Fetching the root Web Category

To view all unassigned products the api provides an artificial “root” category. This category is in effect the parent of all primary categories and any unassigned products. It can be used like a regular Web Category.

The endpoint /api/setup/web_categories/root/ is the “root” Web Category that contains all primary categories and any Products that have no Web Category set.

A GET request to /api/setup/web_categories/root/ is used.

"""
Get the root web category using the OnSite API.
"""
import requests
from xml.dom import minidom

# Customize these to your install.
ONSITE_HOST = 'localhost'
ONSITE_PORT = 9630
ONSITE_USERNAME = 'lightspeed'
ONSITE_PASSWORD = 'admin'
APP_ID = 'com.lightspeed.onsite.demo'
APP_VERSION = '1.0'
APP_PRIVATE_ID = '12345678-90ab-cdef-1234-567890abcdef'

web_categories_url = 'https://%s:%d/api/setup/web_categories/root/' % (ONSITE_HOST, ONSITE_PORT)
logout_url = 'https://%s:%d/api/sessions/current/logout/' % (ONSITE_HOST, ONSITE_PORT)

# Setup a session for the http request.
session = requests.Session()
session.auth = (ONSITE_USERNAME, ONSITE_PASSWORD)
session.headers.update({
    'user-agent': '%s/%s' % (APP_ID, APP_VERSION),
    'x-pappid': APP_PRIVATE_ID})
session.verify = False
session.stream = True

# Send the request to get the root web category.
get_response = session.get(web_categories_url)
assert get_response.status_code == 200

# Log out.
logout_response = session.post(logout_url)
assert logout_response.status_code == 204

# Get the response and print it.
response_xml = minidom.parseString(get_response.text)
print(response_xml.toprettyxml())
<!-- Response -->

<?xml version="1.0" ?>
<category uri="https://localhost:9630/api/setup/web_categories/root/">
    <type>web</type>
    <depth>0</depth>
    <children>
        <category id="1" uri="https://localhost:9630/api/setup/web_categories/1/"/>
    </children>
    <category_products total_count="62">
        <category_product id="7" uri="https://localhost:9630/api/setup/web_categories/root/products/7/">
            <product full_render="false" id="7" uri="https://localhost:9630/api/products/7/">
                <code>333-003-02:35PM</code>
                <flags>
                    <master_model>false</master_model>
                </flags>
                <description>Lucy's Bikini-02:35PM</description>
                <product_photos/>
            </product>
        </category_product>
        <category_product id="11" uri="https://localhost:9630/api/setup/web_categories/root/products/11/">
            <product full_render="false" id="11" uri="https://localhost:9630/api/products/11/">
                <code>FT-1-02:35PM</code>
                <flags>
                    <master_model>false</master_model>
                </flags>
                <description>Temporary Tattoo-02:35PM</description>
                <product_photos/>
            </product>
        </category_product>
    </category_products>
</category>

Adding Products to a Web Category

A POST request to /api/setup/web_categories/{id}/products/ is used to add a product to a web category.

"""
Add a product to a web category using the OnSite API.
"""
import requests
from xml.dom import minidom

# Customize these to your install.
ONSITE_HOST = 'localhost'
ONSITE_PORT = 9630
ONSITE_USERNAME = 'lightspeed'
ONSITE_PASSWORD = 'admin'
APP_ID = 'com.lightspeed.onsite.demo'
APP_VERSION = '1.0'
APP_PRIVATE_ID = '12345678-90ab-cdef-1234-567890abcdef'

web_category_id = 1
web_category_url = 'https://%s:%d/api/setup/web_categories/%d/' % (ONSITE_HOST, ONSITE_PORT, web_category_id)
logout_url = 'https://%s:%d/api/sessions/current/logout/' % (ONSITE_HOST, ONSITE_PORT)

# This is the payment being added to the order.
web_category_xml = """
<category_product>
    <product id="33"/>
</category_product>
"""

# Create a session. This will persist cookies across all requests.
session = requests.Session()
session.auth = (ONSITE_USERNAME, ONSITE_PASSWORD)
session.headers.update({
    'user-agent': '%s/%s' % (APP_ID, APP_VERSION),
    'x-pappid': APP_PRIVATE_ID})
session.verify = False

# Lock the order before making the change.
lock_response = session.request('LOCK', web_category_url)
assert lock_response.status_code == 200

# Send the request to update the order.
post_response = session.post(web_category_url + 'products/', data=web_category_xml)
assert post_response.status_code == 201

# Unlock the order after the change is complete.
unlock_response = session.request('UNLOCK', web_category_url)
assert unlock_response.status_code == 200

# Log out.
logout_response = session.post(logout_url)
assert logout_response.status_code == 204

# Print the revised product.
response_xml = minidom.parseString(post_response.text)
print(response_xml.toprettyxml())
<!-- Response -->

<?xml version="1.0" ?>
<category id="1" uri="https://localhost:9630/api/setup/web_categories/1/">
    <name>Used Items</name>
    <list_order>0</list_order>
    <type>web</type>
    <depth>0</depth>
    <children/>
    <category_products/>
</category>

<?xml version="1.0" ?>
<category_product id="33" uri="https://localhost:9630/api/setup/web_categories/1/products/33/">
    <product full_render="true" id="33" uri="https://localhost:9630/api/products/33/">
        <class/>
        <currency id="1" uri="https://localhost:9630/api/setup/currencies/1/"/>
        <code>333-003-02:46PM</code>
        <costs>
            <cost/>
            <average>0.000</average>
            <raw/>
        </costs>
        <supplier_costs/>
        <flags>
            <current>true</current>
            <editable>false</editable>
            <gift_card>false</gift_card>
            <inventoried>false</inventoried>
            <new_cost>false</new_cost>
            <new_import>false</new_import>
            <new_update>false</new_update>
            <no_live_rules>false</no_live_rules>
            <no_profit>false</no_profit>
            <serialized>false</serialized>
            <web>false</web>
            <editable_sell>false</editable_sell>
            <master_model>false</master_model>
        </flags>
        <sell_price>10.000</sell_price>
        <pricing_levels/>
        <created>2017-07-11T14:46:13.885410</created>
        <modified>2017-07-21T08:37:59.097340</modified>
        <description>Lucy's Bikini-02:46PM</description>
        <long_web_description/>
        <family/>
        <gl_product>
            <asset/>
            <cogs_expense/>
            <income/>
            <payable_expense/>
        </gl_product>
        <product_id>P-1032</product_id>
        <import_id/>
        <inventory>
            <available>-3.000</available>
            <reserved>0.000</reserved>
            <coming_for_stock>0</coming_for_stock>
            <coming_for_customer>0</coming_for_customer>
            <warehouses>0.000</warehouses>
            <in_transit>0.000</in_transit>
            <total>-3.000</total>
        </inventory>
        <margin/>
        <minimum_margin>0.000</minimum_margin>
        <notes/>
        <product_info>
            <color/>
            <height>0.000</height>
            <length>0.000</length>
            <size/>
            <weight>0.000</weight>
            <width>0.000</width>
        </product_info>
        <reorder>
            <amount>0.000</amount>
            <calc>0.000</calc>
            <point>0.000</point>
            <type>0</type>
        </reorder>
        <sells>
            <sell>10.000</sell>
            <sell_tax_inclusive/>
            <sell_web/>
        </sells>
        <supplier/>
        <supplier_code/>
        <upc/>
        <web>
            <inventory/>
        </web>
        <keywords>
            <keyword/>
            <keyword/>
            <keyword/>
        </keywords>
        <multi_store_label>33</multi_store_label>
        <multi_store_master_label/>
        <categories>
            <pos/>
            <web>
                <category id="1" uri="https://localhost:9630/api/setup/web_categories/1/">
                    <name>Used Items</name>
                    <list_order>0</list_order>
                    <type>web</type>
                    <depth>0</depth>
                </category>
            </web>
        </categories>
        <related_products/>
        <serial_numbers/>
        <product_photos/>
        <tax_exemption id="0" uri="https://localhost:9630/api/setup/tax_exemptions/0/">
            <name>Default</name>
            <taxes>
                <tax id="1">
                    <exempt>false</exempt>
                </tax>
                <tax id="2">
                    <exempt>false</exempt>
                </tax>
                <tax id="3">
                    <exempt>false</exempt>
                </tax>
                <tax id="4">
                    <exempt>false</exempt>
                </tax>
                <tax id="5">
                    <exempt>false</exempt>
                </tax>
            </taxes>
            <active>true</active>
        </tax_exemption>
        <master_product/>
    </product>
</category_product>