PHP functions

This page contains information about ASA 2 PHP functions you can use in your WordPress theme templates.

Asa2_Renderer_Asin::render

With this method you can render single products based on an ASIN.

Description

<?php
echo Asa2_Renderer_Asin::render( $asin [, array $options] );

Parameters

asin
The ASIN of the product you want to render
options

An array of Options (same as for the [asa2] shortcode).

  • ajax Set to 1 activate the AJAX mode
  • country_code The template ID
  • no_cache Set to 1 to bypass the cache
  • tpl The template name
  • tplid The template ID
  • tracking_id Custom tracking ID

Examples

This code snippet can be used in a page template to render a product based on an ASIN from a custom field. Additionally the code checks for other optional custom fields where you could define a custom template, country code or tracking ID.

<?php
// get ASIN from custom field "asin"
$asin = get_post_meta($post->ID, 'asin', true);

if (!empty($asin)) {

    $tplid = get_post_meta($post->ID, 'tplid', true);
    $country_code = get_post_meta($post->ID, 'country_code', true);
    $tracking_id = get_post_meta($post->ID, 'tracking_id', true);

    // populate the options array
    $options = array();
    if (!empty($tplid)) {
        $options['tplid'] = $tplid;
    }
    if (!empty($country_code)) {
        $options['country_code'] = $country_code;
    }
    if (!empty($tracking_id)) {
        $options['tracking_id'] = $tracking_id;
    }

    // display the product
    echo Asa2_Renderer_Asin::render($asin, $options);
}
Custom fields

Asa2_Renderer_Collection::render

With this method you can render a collection.

Description

<?php
echo Asa2_Renderer_Collection::render( $collection [, array $options] );

Parameters

collection
The collection name or ID
options

An array of Options (same as for the [asa2_collection] shortcode).

  • limit Limits the amount of collection items
  • no_cache Set to 1 to bypass the cache
  • orderby To order the items (see orderby)
  • order The order direction
  • s A search string
  • tpl The template name
  • tplid The template ID

Examples

This code renders a collection based on a custom field named “asa2_collection”. If a collection name or ID was entered, the options array will be build based on other custom fields and the collection will be rendered.

<?php
// get collection name from custom field
$asa2_collection = get_post_meta($post->ID, 'asa2_collection', true);

if (!empty($asa2_collection)) {

    $tplid = get_post_meta($post->ID, 'tplid', true);
    $limit = get_post_meta($post->ID, 'limit', true);
    $orderby = get_post_meta($post->ID, 'orderby', true);
    $order = get_post_meta($post->ID, 'order', true);

    // populate the options array
    $options = array();
    if (!empty($tplid)) {
        $options['tplid'] = $tplid;
    }
    if (!empty($limit)) {
        $options['limit'] = $limit;
    }
    if (!empty($orderby)) {
        $options['orderby'] = $orderby;
    }
    if (!empty($order)) {
        $options['order'] = $order;
    }

    // display the collection
    echo Asa2_Renderer_Collection::render($asa2_collection, $options);
}
Custom fields

Asa2_Module_Repo_Renderer_SmartCollection::render

Use this method to dynamically create a ASA 2 smart collection in your PHP code.

Description

<?php
echo Asa2_Module_Repo_Renderer_SmartCollection::render( $options );

Parameters

options

An array of Options (same as for the [asa2_smart_collection] shortcode).

  • limit Limits the amount of collection items
  • no_cache Set to 1 to bypass the cache
  • orderby To order the items
  • order The order direction
  • s A search string
  • tpl The template name
  • tplid The template ID
  • cat To filter by category
  • tag To filter by tag
  • rating_gt Average rating greater than
  • rating_lt Average rating less than
  • rating_between Average rating between
  • is_available_main Only products with main price
  • is_prime Prime filter
  • ... see Options for all smart_collection options

Examples

In this example an array of smart collection options is manually set and passed to the method. You can use custom fields to dynamically set the options.

<?php
$options = array(
    'cat_slug' => 'games',
    'limit' => 10,
    'is_available_main' => true,
    'is_prime' => true,
    'rating_gt' => 3,
    'orderby' => 'rand',
    'tplid' => 46
);

// display the smart collection
echo Asa2_Module_Repo_Renderer_SmartCollection::render($options);

Asa2_Service::itemLookup

This method lets you work directly with an ASA 2 item object. You can access all placeholder keys as public properties.

Description

<?php
$item = Asa2_Service::itemLookup( $asin [, array $options] );

Parameters

asin
The ASIN of the product you want to render
options

An array of Options (same as for the [asa2] shortcode).

  • ajax Set to 1 activate the AJAX mode
  • country_code The product’s country code
  • no_cache Set to 1 to bypass the cache
  • tpl The template name
  • tplid The template ID
  • tracking_id Custom tracking ID

Examples

Based on the custom field “asin” this code tries to create an ASA 2 item object by using Asa2_Service::itemLookup(). You can check if the result is a valid object by using Asa2_Service::isItem().

On a valid item object you can access all Placeholders as public properties. For example, if you want to access the item’s title, known as placeholder {{ Title }}, use $item->Title.

<?php
// get ASIN from custom field
$asin = get_post_meta($post->ID, 'asin', true);

if (!empty($asin)) {

    $tplid = get_post_meta($post->ID, 'tplid', true);

    $options = array();
    if (!empty($tplid)) {
        $options['tplid'] = $tplid;
    }

    $item = Asa2_Service::itemLookup($asin, $options);

    if (Asa2_Service::isItem($item)) {
        // it is an ASA 2 item object

        echo '<h1>' . $item->Title . '</h1>';
        printf('<img src="%s" width="%d" height="%d">', $item->SmallImageURL, $item->SmallImageWidth, $item->SmallImageHeight);
        echo 'List price: ' . $item->ListPriceFormattedPrice . '<br>';
        echo 'Current price: ' . $item->OffersMainPriceFormattedPrice . '<br>';
        echo $item->CustomerReviewsImgTag;
    }
}