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
| Function | Returns | Description |
|---|---|---|
atkp_display_box( $ids, $template, $limit = 0, $field = '', $link = false, $content = '', $randomsort = false, $hidedisclaimer = false, $buttontype = 'notset', $tracking_id = '', $elementcss = '', $containercss = '', $ajax_mode = 'none' ) | string | Complete HTML output of a box or list – the PHP equivalent of the [atkp] shortcode. |
atkp_get_product( $id = '' ) | atkp_product|null | The product object (main offer) for further processing. |
atkp_get_shop( $id ) | atkp_shop|null | The shop object. If $id is not a shop, the shop of the product is returned. |
atkp_get_formatter() | atkp_formatter | A formatter object with default parameters, as used in Blade templates. |
atkp_get_title( $id = '' ) | string | Product title. |
atkp_get_short_title( $id = '' ) | string | Shortened product title. |
atkp_get_featuretext( $id = '' ) | string | Features (HTML list). |
atkp_get_featuretext_short( $id = '' ) | string | Features, shortened. |
atkp_get_descriptiontext( $id = '' ) | string | Product description. |
atkp_get_descriptiontext_short( $id = '' ) | string | Product description, shortened. |
atkp_get_infotext( $id = '' ) | string | Info text (price info, update date). |
atkp_get_pro_list( $id = '', $content_mask = '%s' ) | string | Pros; $content_mask formats each entry. |
atkp_get_contra_list( $id = '', $content_mask = '%s' ) | string | Cons; $content_mask formats each entry. |
atkp_get_test_badge( $id = '' ) | string | Test result badge. |
atkp_get_saleprice( $id = '' ) | float | Sale price as a number (0 if unknown). |
atkp_get_saleprice_formatted( $id = '', $format = '%s', $emptytext = '' ) | string | Formatted sale price; $emptytext is shown if there is no price. |
atkp_get_listprice( $id = '' ) | float | List price as a number. |
atkp_get_listprice_formatted( $id = '', $format = '%s', $emptytext = '' ) | string | Formatted list price. |
atkp_get_amount_saved( $id = '' ) | float | Amount saved as a number. |
atkp_get_amount_saved_formatted( $id = '', $format = '%s' ) | string | Formatted amount saved. |
atkp_get_percentage_saved( $id = '' ) | float | Saving in percent as a number. |
atkp_get_percentage_saved_formatted( $id = '', $format = '-%s%%' ) | string | Formatted saving in percent, e.g. -15%. |
atkp_get_baseprice( $id = '' ) | float | Base price as a number. |
atkp_get_baseprice_formatted( $id = '', $format = '(%s / %s)', $emptytext = '' ) | string | Formatted base price with unit. |
atkp_get_unitprice( $id = '' ) | float | Unit price as a number. |
atkp_get_shipping( $id = '' ) | float | Shipping costs as a number. |
atkp_get_shipping_formatted( $id = '', $format = '%s', $emptytext = '' ) | string | Formatted shipping costs. |
atkp_get_producturl( $id = '' ) | string | Affiliate URL of the product. |
atkp_get_productlink( $id = '' ) | string | Link attributes (href, rel, target, tracking) for an <a> tag. |
atkp_get_addtocarturl( $id = '' ) | string | Add-to-cart URL (if the shop supports it). |
atkp_get_addtocartlink( $id = '' ) | string | Link attributes for the add-to-cart link; falls back to the product link. |
atkp_get_button_link( $id = '' ) | string | Link attributes for the buy button, according to the button setting. |
atkp_get_button_text( $id = '', $shop_id = '' ) | string | Button text of the shop. |
atkp_get_title_link( $id = '' ) | string | Link attributes for the product title. |
atkp_get_customerreviewsurl( $id = '' ) | string | URL of the customer reviews. |
atkp_get_reviews_link( $id = '' ) | string | Link to the customer reviews. |
get_get_reviews_text( $id = '' ) | string | Text with the number of reviews. The unusual name is kept for older templates. |
atkp_get_primelogo( $id = '' ) | string | Prime logo, if the offer is Prime. |
atkp_get_smallimageurl( $id = '' ) | string | URL of the small image. |
atkp_get_mediumimageurl( $id = '' ) | string | URL of the medium image. |
atkp_get_largeimageurl( $id = '' ) | string | URL of the large image. |
atkp_get_images( $id = '', $include_main_image = true, $max_images = 0 ) | array|null | All images of the product (0 = no limit). |
atkp_get_offers( $id = '', $include_main_offer = true, $max_offers = 0 ) | atkp_product_offer[]|null | All offers of the product for your own price comparison. |
atkp_get_custom_field( $id, $field_name ) | string | Formatted value of a custom field (Custom Fields module). |
atkp_get_updatedate( $id = '' ) | string | Date of the last update. |
atkp_get_updatetime( $id = '' ) | string | Time of the last update. |
atkp_get_disclaimer( $id = '', $disclaimertext = '' ) | string | Disclaimer text; $disclaimertext overrides the default. |
atkp_get_product_page_url( $id = '' ) | string|null | URL of the product page (Frontend Product Pages module). |
atkp_get_woocommerce_page_url( $id = '' ) | string | URL of the linked WooCommerce product. |
atkp_get_shop_title( $shop_id = '' ) | string | Name of the shop. |
atkp_get_shop_logo( $shop_id = '' ) | string | Shop logo as an image tag. |
atkp_get_shop_logo_url( $shop_id = '' ) | string | URL 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.