Shortik
Shortik

Reputation: 175

WooCommerce, WP all import - Find product ID by attribute value

I want to update WooCommerce products by the WP All import. I have 2 independent xml files (from the first one I'm getting prices, from the second one descriptions). For some reasons product's _sku is {code} from the first one xml not {ean}. So I put {ean} into product attribute pa_ean to match the products from the first one and the second one xml. Now I don't know how to write the php function to return product_id so WP All import could update right products with right descriptions.

Upvotes: 0

Views: 1050

Answers (2)

Shortik
Shortik

Reputation: 175

I put {ean} to Custom field, not to Attribute and after that I can easily use it in WP All import.

Upvotes: 0

kajdzo
kajdzo

Reputation: 62

Do you have any unique identifier that are same for the product in both files? Like product_key or something. If you have it you can store it as custom meta for that product and get product by that meta key, something like below ↓

function check_if_product_exist($rawDataFromFile) {
        global $products;

        // Get WC_Product Objects
        $products = wc_get_products([
            'meta_key' => 'your_meta_key',
            'meta_value' => $rawDataFromFile->your_key
        ]);

        if (!empty($products)) {
            return $products[0];
        }
        return null;
    }

Upvotes: 1

Related Questions