Instantly share code, notes, and snippets.

Embed
What would you like to do?
add_action( 'woocommerce_order_details_after_order_table', 'post_sale_cross_sell' );
function post_sale_cross_sell( $order ) {
$cross_sells = array();
$in_order = array();
foreach ( $order->get_items() as $item ) {
$product_id = $order->get_item( $item )->get_product_id();
$product = wc_get_product( $product_id );
$cross_sells = array_merge( $product->get_cross_sell_ids(), $cross_sells );
$in_order[] = $product_id;
}
$cross_sells = array_diff( $cross_sells, $in_order );
$cross_sells = apply_filters( 'woocommerce_cart_crosssell_ids', wp_parse_id_list( $cross_sells ) );
?>
<section class="related products post_sale">
<h2><?php esc_html_e( 'You might be interested in...', 'domain' ); ?></h2>
<p><?php esc_html_e( 'People who purchased the same products you bought usually also buy the following ones. Care to take a look?', 'woocommerce' ); ?></p>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $cross_sells as $product_id ) : ?>
<?php
$post_object = get_post( $product_id );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment