Automattic\WooCommerce\StoreApi\Routes\V1

ProductsBySlug::get_product_variation_by_slugprivateWC 1.0

Get a product variation by slug.

Method of the class: ProductsBySlug{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->get_product_variation_by_slug( $slug );
$slug(string) (required)
The slug of the product variation.

ProductsBySlug::get_product_variation_by_slug() code WC 10.8.1

private function get_product_variation_by_slug( $slug ) {
	global $wpdb;

	$result = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT ID, post_name, post_parent, post_type
			FROM $wpdb->posts
			WHERE post_name = %s
			AND post_type = 'product_variation'",
			$slug
		)
	);

	if ( ! $result ) {
		return null;
	}

	return wc_get_product( $result[0]->ID );
}