Automattic\WooCommerce\Internal\CLI\Migrator\Core

WooCommerceProductImporter::get_attachment_by_urlprivateWC 1.0

Get existing attachment by URL.

Method of the class: WooCommerceProductImporter{}

No Hooks.

Returns

Int|null. Attachment ID or null if not found.

Usage

// private - for code of main (parent) class only
$result = $this->get_attachment_by_url( $image_url ): ?int;
$image_url(string) (required)
Image URL.

WooCommerceProductImporter::get_attachment_by_url() code WC 10.8.1

private function get_attachment_by_url( string $image_url ): ?int {
	global $wpdb;

	$basename      = wp_basename( $image_url );
	$attachment_id = $wpdb->get_var(
		$wpdb->prepare(
			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE %s",
			'%' . $wpdb->esc_like( $basename )
		)
	);
	return $attachment_id ? (int) $attachment_id : null;
}