Last active
October 30, 2023 10:04
-
-
Save SirDarcanos/22d21e46342d6467d5176668853f2e1b to your computer and use it in GitHub Desktop.
Post-sale cross-sell with WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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