Automattic\WooCommerce\Internal\ProductFeed\Mapping
UcpProductMapper::map_detail_product
Map a product for POST /catalog/product.
A selection narrows the variants to those matching every selected option. One matching no variation is relaxed from its last option until one does, and selected reports the options kept, as UCP requires. A request naming a variation returns it first, with its own options as the selection.
Method of the class: UcpProductMapper{}
No Hooks.
Returns
array.
Usage
$UcpProductMapper = new UcpProductMapper(); $UcpProductMapper->map_detail_product( $product, $selected, $variation_id ): array;
- $product(WC_Product) (required)
- WooCommerce product.
- $selected(array)
- Selected option values, each
{ name, label, id? }.
Default:array() - $variation_id(int)
- Variation the request named, or 0 when it named the product.
UcpProductMapper::map_detail_product() UcpProductMapper::map detail product code WC 11.3.0
public function map_detail_product( \WC_Product $product, array $selected = array(), int $variation_id = 0 ): array {
if ( ! $product instanceof \WC_Product_Variable || ( empty( $selected ) && $variation_id <= 0 ) ) {
return $this->map_product( $product );
}
$item = $this->item( $product );
// UCP: a variant id fully determines the selection state, so the
// request's `selected` is ignored rather than merged.
if ( $variation_id > 0 ) {
$selected = $this->variation_options_by_id( $item )[ $variation_id ] ?? array();
}
$matching = array();
while ( ! empty( $selected ) ) {
$matching = $this->match_selection( $item, $selected );
if ( ! empty( $matching ) ) {
break;
}
array_pop( $selected );
}
$required = $variation_id > 0 ? array( $variation_id ) : array();
$mapped = $this->map_item( $product, $item, $required, false, empty( $matching ) ? null : $matching );
$mapped['selected'] = $selected;
return $mapped;
}