0

I would like to rewrite SKUs generated by WooCommerce Product SKU Generator and prefix them with the vendor slug from WooCommerce Product Vendors into the following format: [Vendor Slug][Generated SKU]. E.g. 'GT1234', 'JD1235', etc... If the product author is not a vendor or the order is not associated with a vendor, the prefix should default to 'XX'.

Main difficulty for me is retrieving the vendor slug for the user adding the new product. How would I go about doing that? Anything to get me going is appreciated!

function filter_wc_sku_generator_sku( $product_sku, $product ) {

    $vendor_slug = '???'
    if (!$vendor_slug) {
        $product_sku = 'XX' . $product_sku;
    } else {
        $product_sku = $vendor_slug . $product_sku;
    }
    return $product_sku;
};
add_filter( 'wc_sku_generator_sku', 'filter_wc_sku_generator_sku', 10, 2 );
0

There's a class WC_Product_Vendors_Utils that you can use e.g.

$vendor = WC_Product_Vendors_Utils::is_vendor_product( $item["product_vendors_product_id"] ); //replace this with the product ID

    if ( ! empty( $vendor[0] ) ) {
        $vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor[0]->term_id ); 

        if ( ! empty( $vendor_data ) ) {
            $vendor_slug = $vendor_data['slug']; // I am not sure of the correct value here but you can var_dump() just to make sure
        }
    }

Hope that will help you start generating the SKU properly :)

  • Thanks Junnel! That did it. The correct value was indeed 'slug'. After getting the product ID, everything's working great now :) – Gion Tummers Jan 7 at 22:14
  • You're welcome ;) – Junnel Gallemaso Jan 8 at 3:05

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.