$this->convertPrice($product->get_regular_price()), 'barcode' => $product->get_global_unique_id() ?: null, 'description' => mb_substr($product->get_description(), 0, 500) ?: null, 'name' => mb_substr($product->get_name(), 0, 45), 'short_description' => mb_substr($product->get_short_description(), 0, 100) ?: null, 'sku' => $product->get_sku() ?: null, 'type' => 'product', 'vat_id' => $this->resolveVatId($product), ]; $data['stock'] = $product->managing_stock() ? (int) $product->get_stock_quantity() // Stock management enabled : null; // No stock management $data['discounted'] = $product->get_sale_price() !== '' ? $this->convertPrice($product->get_sale_price()) // Convert to cents if not null, '' or 0 : null; // No sale price set if (wc_get_container()->get(FeaturesController::class)->feature_is_enabled('cost_of_goods_sold')) { // If cost of goods is enabled on WooCommerce $unitCost = $product->get_meta('_cogs_total_value'); $data['unit_cost'] = $unitCost !== '' && (float) $unitCost > 0 ? $this->convertPrice($unitCost) // Convert to cents if not null, '' or 0 : null; // No unit cost set } return $data; } // Get categories of product that are linked to Aikanen service groups public function getServiceGroupIds(\WC_Product $product): array { $termIds = wp_get_object_terms($product->get_id(), 'product_cat', ['fields' => 'ids']); $groupIds = []; foreach ($termIds as $termId) { $groupId = get_term_meta($termId, '_aikanen_group_id', true); if ($groupId) { $groupIds[] = $groupId; } } return $groupIds; } // Convert price - Woo stores prices as strings, Aikanen expects cents private function convertPrice(string $price): int { return (int) round((float) $price * 100); } private function resolveVatId(\WC_Product $product): int { $taxClass = $product->get_tax_class(); $vatMap = (new ProductImportMapper())->getVatMap(); return (int) (array_search($taxClass, $vatMap, true) ?: 1); } }