WP-Lister for Amazon API – Hooks and filters

Contents

If you are a developer writing a plugin or theme for WP-Lister for Amazon, you might find the following filters and actions hooks useful.

Note: This documentation is work in progress and doesn’t cover all available hooks yet.

If you want us to add a specific hook to help in your particular use case, don’t hesitate to contact us. We appreciate all suggestions and will usually implement your request in the next update.

Where to add custom code snippets

The standard location to add custom code snippets like some of the examples below would usually be the functions.php file contained in your current WordPress theme. This can become a problem when you need to update your theme, which would overwrite that functions.php file, meaning you would loose all your custom code snippets.

That’s why it’s generally recommended to use a child theme when you want to add or modify any code of your theme, but some commercial themes are already child themes and still need to be updated eventually, which is why we highly recommend a custom plugin as the location for all your custom code snippets.

Unless you’re a developer who prefers to write something similar yourself, please take a look at our WP Lab Custom Code plugin. This one is meant to be “A handy little plugin to contain your custom code snippets” and is used by our support staff whenever we need to install some custom code on a client’s site. If you are looking for a more comprehensive solution that supports not only custom PHP snippets but also CSS, JavaScript and template overrides, please check out the Theme Customisations plugin created by the people from WooCommerce.

wpla_product_has_changed

Mark a product as changed in WP-Lister. Use this if you need to emulate how WP-Lister behaves when a product is updated manually in WooCommerce: The profile will be reapplied and the listing status is set to changed, which means the changes will be submitted to Amazon on the next cron job run. So if your cron job runs every 5 minutes, a listing feed will be submitted within 5 minutes – but keep in mind that Amazon might take longer than that to actually process your feed (usually less than 30 minutes though).

Usage

do_action('wpla_product_has_changed', $post_id );

wpla_prepare_listing

Prepare a new listing from a WooCommerce product and apply a profile. The parameter $post_id can be a single product ID or an array of IDs.

Usage

do_action('wpla_prepare_listing', $post_id, $profile_id );

wpla_filter_listing_feed_column

This is a general purpose filter which allows you to modify any column in a listing feed. The example below demonstrates how to force the price column to be left empty – in case you prefer to manage Amazon prices on Seller Central only. Available since version 0.9.1.5.

Usage

add_filter( 'wpla_filter_listing_feed_column', 'my_custom_amazon_feed_filter' , 10, 6 );
function my_custom_amazon_feed_filter( $value, $column, $item, $product, $profile, $template_type ) {

  // force empty price column
  if ( 'price' == $column ) {
    $value = '';
  }

  // force empty sale price column (Category feed)
  if ( 'sale_price' == $column ) {
    $value = '';
  }

  // force empty sale price column (ListingLoader feed)
  if ( 'sale-price' == $column ) {
    $value = '';
  }

  return $value;
}

wpla_filter_imported_product_data

This filter allows you to customize the product details when importing listings from Amazon and creating products in WooCommerce. Available since version 0.9.6.7.

Usage

add_filter( 'wpla_filter_imported_product_data', 'my_custom_amazon_import_filter' , 10, 2 );
function my_custom_amazon_import_filter( $data, $listing ) {

  // translate and truncate publication date
  if ( isset( $data['attributes']['Publication date'] ) ) {
    $data['attributes']['Publication date']->name  = 'Jahr';
    $data['attributes']['Publication date']->value = date( 'Y', strtotime( $data['attributes']['Publication date']->value ) );
  }

  return $data;
}

wpla_filter_imported_condition_html

This filter allows you to customize the item condition HTML that is appended to the main product description when importing products from Amazon. Available since version 0.9.6.7.

Usage

add_filter( 'wpla_filter_imported_condition_html', 'my_custom_amazon_condition_import_filter' , 10, 2 );
function my_custom_amazon_condition_import_filter( $html, $data ) {

  // translate item conditions to german
  $html = str_replace( 'New', 'Neu', $html );
  $html = str_replace( 'Used', 'Gebraucht', $html );

  return $html;
}

wpla_filter_product_price / wpla_filter_sale_price

Allows you to filter the product price / sale price to be used for Amazon. The example code below will use the cost of good amount provided by the Cost of Good extension for WooCommerce, add $4.95 for shipping and increase it by 35%. Available since version 0.8.9.2.

Usage

add_filter( 'wpla_filter_product_price', 'my_custom_amazon_product_price' , 10, 5 );
add_filter( 'wpla_filter_sale_price',    'my_custom_amazon_product_price' , 10, 5 );
function my_custom_amazon_product_price( $price, $post_id, $product, $item, $profile ) {

  // check if cost of good is set
  $cost_of_good = get_post_meta( $post_id, '_wc_cog_cost', true );
  if ( ! $cost_of_good ) return $price;

  // apply custom math
  $price = ( $cost_of_good + 4.95 ) * 1.35;

  return $price;
}

wpla_reason_for_not_creating_wc_order

Allows you programmatically determine if WP-Lister should create an order in WooCommerce or not. The example below will skip FBA orders by returning a non-empty value, which will appear in the order history log. Return false to create the order (default). Available since version 0.9.6.8.

Usage

add_filter( 'wpla_reason_for_not_creating_wc_order', 'my_custom_amazon_order_creation_filter' , 10, 2 );
function my_custom_amazon_order_creation_filter( $reason, $wpla_order ) {
  // check if this is an FBA order
  if ( 'AFN' == $wpla_order['details']->FulfillmentChannel ) {
    // set reason to skip order
    $reason = 'FBA orders are not created in WooCommerce';
  }
  return $reason;
}

wpla_mcf_enabled_order_statuses

Allows you enable custom order statuses to be fulfilled via FBA / Multi Channel Fulfillment. Available since version 0.9.6.13.

Usage

add_filter( 'wpla_mcf_enabled_order_statuses', 'my_custom_mcf_order_status_filter' , 10, 1 );
function my_custom_mcf_order_status_filter( $allowed_order_statuses ) {
  // add custom order status (by slug)
  $allowed_order_statuses[] = 'wc-my-custom-status-slug';
  return $allowed_order_statuses;
}

wpla_available_shipping_providers

This filter allows you to customize the list of available shipping providers along with their respective shipping methods on the order details page.

The example below shows how to add new shipping methods to the existing provider “GLS”.

Usage

add_filter( "wpla_available_shipping_providers", "my_custom_amazon_shipping_providers", 10, 1 );

function my_custom_amazon_shipping_providers( $shipping_providers ) {
    // define custom selection of shipping providers and methods
    $my_shipping_providers = array(
        'GLS'   => array(
            'ExpeditedGlobalParcel',
            'ExpeditedLocalParcel'
         )
    );

    $shipping_providers = array_merge_recursive( $shipping_providers, $my_shipping_providers );
    return $shipping_providers;
}

wpla_after_create_order_with_nonexisting_items

Use this action hook if you need to post process WooCommerce orders that were created by WP-Lister. The example below will remove the email address from the WooCommerce order, which might be useful if you want to make sure that no other plugins (like the Follow Up Emails extension) could accidentally send emails to the alias address provided by Amazon.

Warning: If you use this hook to process order line items, please keep in mind that WP-Lister might create orders for products which do not exist in WooCommerce! (hence the name)

Usage

add_action( 'wpla_after_create_order_with_nonexisting_items', 'my_wpla_after_create_order_action' , 10, 1 );
function my_wpla_after_create_order_action( $post_id ) {
  // remove email address from order
  update_post_meta( $post_id, '_billing_email', '' );
}

wpla_processed_third_party_tracking

Use this action hook to send order tracking information to Amazon during the submission of the Shipping Confirmation feed.

<?php

/**
 * Insert custom tracking information before submitting the Shipment Confirmation feed to Amazon
 */
add_action( 'wpla_processed_third_party_tracking', function( $order_id ) {

	if ( $custom_tracking_number = get_post_meta( $order_id, 'my_tracking_number', true ) ) {
		update_post_meta( $order_id, '_wpla_tracking_number', $custom_tracking_number );
	}

    if ( $custom_carrier = get_post_meta( $order_id, 'my_shipping_carrier', true ) ) {
        update_post_meta( $order_id, '_wpla_tracking_provider', $custom_carrier );
    }

    if ( $custom_method = get_post_meta( $order_id, 'my_shipping_method', true ) ) {
        update_post_meta( $order_id, '_wpla_tracking_service_name', $custom_method );
    }
} );

Allows you to filter the product gallery images if you don’t want to use all available images for Amazon.The example code below will use only images with “_amazon” in the filename and ignore all others. Available since version 0.8.8.9.

Usage

add_filter( 'wpla_product_gallery_attachment_ids', 'my_custom_amazon_images_filter' , 10, 2 );
function my_custom_amazon_images_filter( $attachment_ids, $post_id ) {
  $attachments_for_amazon = array();
  foreach ($attachment_ids as $id) {
    // get image url
    $image_url = wp_get_attachment_image_src( $id, 'full' );
    $image_url = isset($image_url[0]) ? $image_url[0] : '';
    // check if filename contains '_amazon'
    if ( strpos( $image_url, '_amazon') ) {
      $attachments_for_amazon[] = $id;
    }
  }
  return $attachments_for_amazon;
}

wpla_product_main_image_url

Allows you to filter the main product image if you don’t want to use the default main image from WooCommerce. The example code below will use the first image with “_amazon” in its filename from the Product Gallery as the main image on Amazon. Available since version 0.8.8.11.

Usage

add_filter( 'wpla_product_main_image_url', 'my_custom_amazon_main_image_url' , 10, 2 );
function my_custom_amazon_main_image_url( $original_image_url, $post_id ) {
    $product = get_product( $post_id );
    $attachment_ids = $product->get_gallery_attachment_ids();
    foreach ($attachment_ids as $id) {
        // get image url
        $image_url = wp_get_attachment_image_src( $id, 'full' );
        $image_url = isset($image_url[0]) ? $image_url[0] : '';
        // check if filename contains '_amazon'
        if ( strpos( $image_url, '_amazon') ) {
            return $image_url;
        }
    }
    return $original_image_url;
}

wpla_custom_values

This filter allows you to add custom values which can be mapped to Amazon feed columns like any other product property or attribute. Available since version 0.9.7.

The first example assumes you have a custom taxonomy “product_brand” and adds the option to select “Product Brands” as an custom value when editing a profile.

Example #1

add_filter( 'wpla_custom_values', 'my_custom_wpla_product_brand_value' );

function my_custom_wpla_product_brand_value( $shortcodes ) {
    $shortcode = 'product_brands_list';
    $shortcodes[ $shortcode ] = array(
        'slug'       => $shortcode,
        'title'      => 'Product Brands',
        'callback'   => 'my_custom_get_product_brands_list_function',
        'content'    => false,
    );

    return $shortcodes;
}

function my_custom_get_product_brands_list_function( $product_id ) {
    if ( $parent_id = WPLA_ProductWrapper::getVariationParent( $product_id ) ) {
        $product_id = $parent_id;
    }

    $brands_tax = wp_get_object_terms( $product_id, 'product_brand', array( 'fields' => 'names' ) );

    if ( ! is_wp_error( $brands_tax ) ) {
        return implode( ', ', $brands_tax );
    }

    return '';
}

The next example allows you to use the name of your tax class as an custom value.

Example #2

add_filter( 'wpla_custom_values', 'my_custom_wpla_product_tax_class_value' );

function my_custom_wpla_product_tax_class_value( $shortcodes ) {
    $shortcode = 'product_tax_class';
    $shortcodes[ $shortcode ] = array(
        'slug'       => $shortcode,
        'title'      => 'Product Tax Class',
        'callback'   => 'my_custom_get_product_tax_class_function',
        'content'    => false,
    );

    return $shortcodes;
}

function my_custom_get_product_tax_class_function( $product_id ) {
    if ( $parent_id = WPLA_ProductWrapper::getVariationParent( $product_id ) ) {
        $product_id = $parent_id;
    }

    $product = wc_get_product( $product_id );

    return $product->get_tax_class();
}