retrieve cross sell product in woocommerce

I have been here for the last 5 hours trying to figure out something. I need to be able to fetch the cross sell id of a product in the woo commerce in the order confirmation email. Up to now I have

         $items = $order->get_items(); 
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            }   

And this retrive the id of the product. As there is always just going to be one cross sell product how can I get the id of that product in a variable?

Read More

This will be the last part of an affiliated program I am building for a guy but I now if fetches the purchased product id and I need the cross-sell one.

Related posts

2 comments

  1. First you need cross sell product id for that product
    so it can be like:

    $crosssellProductIds   =   get_post_meta( get_the_ID(), '_crosssell_ids' );
    $crosssellProductIds    =   $crosssellProductIds[0];
    

    Now as we have all related cross cell product’s id, let’s get there information

    foreach($crosssellProductIds as $id):
        $crosssellProduct = wc_get_product( $id );
        print_r($crosssellProduct);
    endforeach;
    

Comments are closed.