Home > Back-end >  Get related products SKUs in WooCommerce
Get related products SKUs in WooCommerce

Time:09-13

We want to show a section called "complete the look" in WooCommerce. We are using PureClarity to do that.

PureClarity asked us to extend the WooCommerce feed by adding a code snippet in functions.php to add related peoducts SKUs under RelatedProducts.

We used the following code:

function product_callbackk($data, $product) {
        $data['RelatedProducts'] =  $product->get_sku() ?? null;
    return $data;
}
add_filter( 'pureclarity_feed_get_product_data', 'product_callbackk', 10, 2 );

But it showing only the current product SKU and not the related products SKUs. Any advice?

CodePudding user response:

I don't use PureClarity so I added an example based on a default hook in WooCommerce.

In this I use wc_get_related_products() which allow to obtain related products based on the current product ID.

The product SKU per product can then be obtained via a foreach loop.

So you get:

function action_woocommerce_single_product_summary() {
    // Get the global product object
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Initialize
        $skus = array();

        // Get product ID
        $product_id = $product->get_id();
        
        // Get related products based on product category and tags || second argument = limit of results, default = 5
        $related_products = wc_get_related_products( $product_id, 10 );

        // Loop through
        foreach ( $related_products as $related_product ) {
            // Get product 
            $product = wc_get_product( $related_product );

            // Get product SKU
            $product_sku = $product->get_sku();

            // NOT empty
            if ( ! empty( $product_sku ) ) {
                // Push to array
                $skus[] = $product_sku;
            }
        }

        // Output
        echo '<pre>', print_r( $skus, 1 ), '</pre>';     
    }
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 10 );

Or in your specific case:

/**
 * @param mixed[] $data - the array of data that will be sent to PureClarity
 * @param WC_Product $product - the WooCommerce product object
 * @return mixed
 */
function filter_pureclarity_feed_get_product_data( $data, $product ) {
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Initialize
        $skus = array();

        // Get product ID
        $product_id = $product->get_id();
        
        // Get related products based on product category and tags || second argument = limit of results, default = 5
        $related_products = wc_get_related_products( $product_id, 10 );

        // Loop through
        foreach ( $related_products as $related_product ) {
            // Get product 
            $product = wc_get_product( $related_product );

            // Get product SKU
            $product_sku = $product->get_sku();

            // NOT empty
            if ( ! empty( $product_sku ) ) {
                // Push to array
                $skus[] = $product_sku;
            }
        }

        $data['RelatedProducts'] = $skus;
    }

    return $data;
}
add_filter( 'pureclarity_feed_get_product_data', 'filter_pureclarity_feed_get_product_data', 10, 2 );
  • Related