For a project we’re currently working on, we’re migrating an e-commerce shop from Zen Cart to WooCommerce. One of the many challenges of such a project is mapping functionality that the client is used to in the previous admin system, with functionality in the new system.

WooCommerce - upsellsOne such functionality was related to up-sells – suggesting related (higher priced) products to a customer considering a purchase – on each product page. In Zen Cart our client could see which product pages a specific product had been added to as an up-sell. For each product they always list another three suggested products, so if a product gets discontinued, they need to add a new up-sell.

In WooCommerce, we couldn’t find such a function, so instead of our client needing to remember which related product pages a specific product had been added to as an up-sell – not really ideal with some 350 products – we built something ourselves instead.

The aim was to integrate the function on each admin product page directly, so that the up-sell relationship could quickly be checked. We wrote a code snippet which added our queried results on the Linked Products tab, keeping it all in context.

[php]
function woocommerce_product_options_related() {
global $post;
$current = $post->ID;
$args = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product’,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo ‘
<ul>’;
while ( $query->have_posts() ) : $query->the_post();
$upsells = get_post_meta( get_the_ID(), ‘_upsell_ids’, true );
if (is_array($upsells) && in_array($current, $upsells)) {
echo ‘
<li><a href=”‘ . admin_url( ‘post.php?post=’ . get_the_ID() ) . ‘&action=edit”>’ . get_the_title() . ‘</a></li>

‘;
}
endwhile;
wp_reset_postdata();
echo ‘</ul>

‘;
}
}
add_filter(‘woocommerce_product_options_related’, ‘woocommerce_product_options_related’);
[/php]

WooCommerce saves the IDs of products that are listed as up-sells for a specific product as meta data. So we query the list of products and get all products that have a meta value ID that corresponds to the currently viewed product. Then it outputs these products, linking each result to its edit page in the admin.