admin管理员组

文章数量:1277274

As a result of my research, I can bring similar sku ones no matter which product I enter, but I encountered a problem, the sku code of the product I entered changes.

E.g:

1- ) 36.214.03

2- ) 36.214.19

3- ) 36.214.17

This is how sku values similar to the product I entered are returned. I keep the sku code of each product at the bottom. The Sku code of the product I entered should be "36,214,19" but it always prints the last Sku code as "36,214,17", how can I avoid this?

In short, what can I do so that the product's own sku code does not change?

Following my code: functions.php

function wc_get_product_skus( $sku_excerpt ) {
    global $wpdb;

    // Get all product Ids and skus (standard objects) from a sku excerpt
    return $wpdb->get_results( $wpdb->prepare( "
        SELECT p.ID as id, pm.meta_value as sku
        FROM {$wpdb->prefix}posts p
        INNER JOIN {$wpdb->prefix}postmeta pm
            ON p.ID = pm.post_id
        WHERE p.post_type = 'product'
        AND p.post_status = 'publish'
        AND pm.meta_key = '_sku'
        AND pm.meta_value LIKE '%s'
    ", '%'.$sku_excerpt.'%' ) );
}

single-product.php

$results = wc_get_product_skus('36.214.');

// Loop through results
foreach ( $results as $result ) {
    $product_id  = $result->id;
    $product_sku = $result->sku;

    // Get the WC_Product Object (if needed)
    $product     = wc_get_product( $product_id );

    // Testing output
    echo 'Sku : '.$product_sku.'<br>'; 
}

Thanks in advance

本文标签: phpGet WooCommerce products with similar SKU