Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Shopify

ShopifyMapper::map_simple_product_dataprivateWC 1.0

Maps simple product data (price, SKU, stock, weight) from Shopify variant.

Method of the class: ShopifyMapper{}

No Hooks.

Returns

Array. Simple product data mappings.

Usage

// private - for code of main (parent) class only
$result = $this->map_simple_product_data( $shopify_product ): array;
$shopify_product(object) (required)
The Shopify product data.

ShopifyMapper::map_simple_product_data() code WC 10.8.1

private function map_simple_product_data( object $shopify_product ): array {
	$simple_data = array();

	if ( ! empty( $shopify_product->variants->edges ) ) {
		$variant_node = $shopify_product->variants->edges[0]->node;

		if ( $this->should_process( 'price' ) ) {
			if ( $variant_node->compareAtPrice && $variant_node->compareAtPrice > $variant_node->price ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
				$simple_data['sale_price']    = $variant_node->price;
				$simple_data['regular_price'] = $variant_node->compareAtPrice; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			} else {
				$simple_data['sale_price']    = null;
				$simple_data['regular_price'] = $variant_node->price;
			}
		}

		if ( $this->should_process( 'sku' ) ) {
			$simple_data['sku'] = wc_clean( $variant_node->sku );
		}

		if ( $this->should_process( 'stock' ) ) {
			$manage_stock                  = property_exists( $variant_node, 'inventoryItem' ) && $variant_node->inventoryItem->tracked; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			$simple_data['manage_stock']   = $manage_stock;
			$stock_quantity                = $variant_node->inventoryQuantity ?? 0; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			$allow_oversell                = $manage_stock && 'CONTINUE' === $variant_node->inventoryPolicy; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			$simple_data['stock_status']   = ( $stock_quantity > 0 || $allow_oversell ) ? 'instock' : 'outofstock';
			$simple_data['stock_quantity'] = $stock_quantity;
		}

		if ( $this->should_process( 'weight' ) ) {
			$weight_data = null;
			if ( property_exists( $variant_node, 'inventoryItem' ) && is_object( $variant_node->inventoryItem ) && // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
				property_exists( $variant_node->inventoryItem, 'measurement' ) && is_object( $variant_node->inventoryItem->measurement ) && // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
				property_exists( $variant_node->inventoryItem->measurement, 'weight' ) && is_object( $variant_node->inventoryItem->measurement->weight ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			) {
				$weight_data = $variant_node->inventoryItem->measurement->weight; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			}
			$weight                = $weight_data ? $weight_data->value : null;
			$weight_unit           = $weight_data ? $weight_data->unit : null;
			$simple_data['weight'] = $this->get_converted_weight( $weight, $weight_unit );
		}

		if ( property_exists( $variant_node, 'inventoryItem' ) && is_object( $variant_node->inventoryItem ) && // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
			property_exists( $variant_node->inventoryItem, 'unitCost' ) && is_object( $variant_node->inventoryItem->unitCost ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
		) {
			$simple_data['cost_of_goods'] = $variant_node->inventoryItem->unitCost->amount; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- GraphQL uses camelCase.
		}

		if ( property_exists( $variant_node, 'taxable' ) ) {
			$simple_data['tax_status'] = $variant_node->taxable ? 'taxable' : 'none';
		}

		$simple_data['original_variant_id'] = ! empty( $variant_node->id ) ? basename( $variant_node->id ) : null;

	} else {
		$simple_data['sku']            = null;
		$simple_data['regular_price']  = null;
		$simple_data['sale_price']     = null;
		$simple_data['stock_quantity'] = null;
		$simple_data['manage_stock']   = false;
		$simple_data['stock_status']   = 'instock';
		$simple_data['weight']         = null;

		if ( property_exists( $shopify_product, 'taxable' ) ) {
			$simple_data['tax_status'] = $shopify_product->taxable ? 'taxable' : 'none';
		}

		$simple_data['original_variant_id'] = null;
	}

	return $simple_data;
}