Automattic\WooCommerce\Internal\ProductFeed\Integrations\POSCatalog

ProductMapper::map_productpublicWC 10.5.0

Map WooCommerce product to catalog row

Method of the class: ProductMapper{}

Hooks from the method

Returns

Array. Mapped product data array.

Usage

$ProductMapper = new ProductMapper();
$ProductMapper->map_product( $product ): array;
$product(WC_Product) (required)
Product to map.

Changelog

Since 10.5.0 Introduced.

ProductMapper::map_product() code WC 10.7.0

public function map_product( WC_Product $product ): array {
	$is_variation = $product->is_type( 'variation' );
	$controller   = $is_variation
		? $this->variations_controller
		: $this->products_controller;

	// This should never be the case, as the class should be loaded through DI.
	if ( null === $controller ) {
		throw new \RuntimeException( 'ProductMapper::init() must be called before map_product().' );
	}

	$request  = $is_variation ? $this->get_variations_request() : $this->get_products_request();
	$response = $controller->prepare_object_for_response( $product, $request );

	// Apply _fields filtering (normally done by REST server dispatch).
	$fields = $is_variation ? $this->variation_fields : $this->fields;
	if ( null !== $fields ) {
		$response = rest_filter_response_fields( $response, rest_get_server(), $request );
	}

	$row = array(
		'type' => $product->get_type(),
		'data' => $response->get_data(),
	);

	/**
	 * Filter mapped catalog product data.
	 *
	 * @since 10.5.0
	 * @param array      $row     Mapped product data.
	 * @param WC_Product $product Product object.
	 */
	return apply_filters( 'woocommerce_pos_catalog_map_product', $row, $product );
}