Global PHP functions for product data

Reference of all global PHP functions of affiliate-toolkit: read title, prices, links, images and offers of a product in your theme or plugin.

Besides shortcodes and templates, affiliate-toolkit provides global PHP functions that return single values of a product – title, price, affiliate link, images, offers. Use them when you build your own output in a theme file, a page builder element or your own plugin and need the data outside of a template.

The functions are defined by the base plugin. Wrap your code in function_exists() so your theme does not break if the plugin is deactivated.

The $id parameter

Almost every function takes an $id as its first parameter. It decides which product is used:

  • Empty – on a product page the product itself, on any other post its main product (or, if there is none, its main list).
  • Product ID – this product.
  • List ID – the first product of the list.
  • Slug or title – the product or list with this slug or title.

The functions always use the main offer of the product. Shop functions take a shop ID instead; if you pass a product ID, the shop of that product is used.

Examples

<?php if ( function_exists( 'atkp_get_title' ) ) : ?>
    <a <?php echo atkp_get_productlink( 123 ); ?>>
        <?php echo esc_html( atkp_get_title( 123 ) ); ?>
    </a>
    <?php echo atkp_get_saleprice_formatted( 123, 'only %s', 'price on request' ); ?>
<?php endif; ?>

The …link functions return the attributes of an <a> tag (href, rel, target and link tracking), not a complete link. Put them inside the opening tag as shown above.

// main product of the current post
$price = atkp_get_saleprice();

// first product of the list with ID 456
$title = atkp_get_title( 456 );

// product by slug
$image = atkp_get_mediumimageurl( 'nordklang-anc-700' );

// own price comparison
foreach ( (array) atkp_get_offers( 123 ) as $offer ) {
    // $offer is an atkp_product_offer object
}

List of functions

FunctionReturnsDescription
atkp_display_box( $ids, $template, $limit = 0, $field = '', $link = false, $content = '', $randomsort = false, $hidedisclaimer = false, $buttontype = 'notset', $tracking_id = '', $elementcss = '', $containercss = '', $ajax_mode = 'none' )stringComplete HTML output of a box or list – the PHP equivalent of the [atkp] shortcode.
atkp_get_product( $id = '' )atkp_product|nullThe product object (main offer) for further processing.
atkp_get_shop( $id )atkp_shop|nullThe shop object. If $id is not a shop, the shop of the product is returned.
atkp_get_formatter()atkp_formatterA formatter object with default parameters, as used in Blade templates.
atkp_get_title( $id = '' )stringProduct title.
atkp_get_short_title( $id = '' )stringShortened product title.
atkp_get_featuretext( $id = '' )stringFeatures (HTML list).
atkp_get_featuretext_short( $id = '' )stringFeatures, shortened.
atkp_get_descriptiontext( $id = '' )stringProduct description.
atkp_get_descriptiontext_short( $id = '' )stringProduct description, shortened.
atkp_get_infotext( $id = '' )stringInfo text (price info, update date).
atkp_get_pro_list( $id = '', $content_mask = '%s' )stringPros; $content_mask formats each entry.
atkp_get_contra_list( $id = '', $content_mask = '%s' )stringCons; $content_mask formats each entry.
atkp_get_test_badge( $id = '' )stringTest result badge.
atkp_get_saleprice( $id = '' )floatSale price as a number (0 if unknown).
atkp_get_saleprice_formatted( $id = '', $format = '%s', $emptytext = '' )stringFormatted sale price; $emptytext is shown if there is no price.
atkp_get_listprice( $id = '' )floatList price as a number.
atkp_get_listprice_formatted( $id = '', $format = '%s', $emptytext = '' )stringFormatted list price.
atkp_get_amount_saved( $id = '' )floatAmount saved as a number.
atkp_get_amount_saved_formatted( $id = '', $format = '%s' )stringFormatted amount saved.
atkp_get_percentage_saved( $id = '' )floatSaving in percent as a number.
atkp_get_percentage_saved_formatted( $id = '', $format = '-%s%%' )stringFormatted saving in percent, e.g. -15%.
atkp_get_baseprice( $id = '' )floatBase price as a number.
atkp_get_baseprice_formatted( $id = '', $format = '(%s / %s)', $emptytext = '' )stringFormatted base price with unit.
atkp_get_unitprice( $id = '' )floatUnit price as a number.
atkp_get_shipping( $id = '' )floatShipping costs as a number.
atkp_get_shipping_formatted( $id = '', $format = '%s', $emptytext = '' )stringFormatted shipping costs.
atkp_get_producturl( $id = '' )stringAffiliate URL of the product.
atkp_get_productlink( $id = '' )stringLink attributes (href, rel, target, tracking) for an <a> tag.
atkp_get_addtocarturl( $id = '' )stringAdd-to-cart URL (if the shop supports it).
atkp_get_addtocartlink( $id = '' )stringLink attributes for the add-to-cart link; falls back to the product link.
atkp_get_button_link( $id = '' )stringLink attributes for the buy button, according to the button setting.
atkp_get_button_text( $id = '', $shop_id = '' )stringButton text of the shop.
atkp_get_title_link( $id = '' )stringLink attributes for the product title.
atkp_get_customerreviewsurl( $id = '' )stringURL of the customer reviews.
atkp_get_reviews_link( $id = '' )stringLink to the customer reviews.
get_get_reviews_text( $id = '' )stringText with the number of reviews. The unusual name is kept for older templates.
atkp_get_primelogo( $id = '' )stringPrime logo, if the offer is Prime.
atkp_get_smallimageurl( $id = '' )stringURL of the small image.
atkp_get_mediumimageurl( $id = '' )stringURL of the medium image.
atkp_get_largeimageurl( $id = '' )stringURL of the large image.
atkp_get_images( $id = '', $include_main_image = true, $max_images = 0 )array|nullAll images of the product (0 = no limit).
atkp_get_offers( $id = '', $include_main_offer = true, $max_offers = 0 )atkp_product_offer[]|nullAll offers of the product for your own price comparison.
atkp_get_custom_field( $id, $field_name )stringFormatted value of a custom field (Custom Fields module).
atkp_get_updatedate( $id = '' )stringDate of the last update.
atkp_get_updatetime( $id = '' )stringTime of the last update.
atkp_get_disclaimer( $id = '', $disclaimertext = '' )stringDisclaimer text; $disclaimertext overrides the default.
atkp_get_product_page_url( $id = '' )string|nullURL of the product page (Frontend Product Pages module).
atkp_get_woocommerce_page_url( $id = '' )stringURL of the linked WooCommerce product.
atkp_get_shop_title( $shop_id = '' )stringName of the shop.
atkp_get_shop_logo( $shop_id = '' )stringShop logo as an image tag.
atkp_get_shop_logo_url( $shop_id = '' )stringURL of the shop logo.

If a value is missing on the product (for example no list price), you get an empty string, 0 for numbers or null for arrays. Only pass IDs, slugs or titles that exist – an unknown value currently leads to a PHP error instead of an empty result.