admin管理员组

文章数量:1302929

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 );

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 );
Share Improve this question asked Jan 7, 2019 at 0:59 Gion TummersGion Tummers 31 bronze badge 1
  • What code did you finally use to include the seller's initials in the plugin auto-generated SKU? Thanks – Alvaro Commented Feb 17, 2021 at 20:27
Add a comment  | 

1 Answer 1

Reset to default 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 :)

本文标签: pluginsAdd WooCommerce vendor slug to autogenerated SKU