get_item($orderItemId); return $item instanceof \WC_Order_Item_Product ? $item : null; } public function isEligible(\WC_Order $order): bool { return in_array($order->get_status(), self::ELIGIBLE_STATUSES, true); } public function lineFacts(\WC_Order $order, \WC_Order_Item_Product $item): ?LineFacts { $quantity = (int) $item->get_quantity(); if ($quantity < 1) { return null; } $refunded = min($quantity, (int) abs((float) $order->get_qty_refunded_for_item($item->get_id()))); [$deliveredOn, $source] = $this->delivery($order); return new LineFacts( (int) $order->get_id(), (int) $item->get_id(), (int) ($item->get_variation_id() ?: $item->get_product_id()), $quantity, $refunded, $this->contractDate($order), $deliveredOn, $source, $this->guaranteeMonths($item), $this->isBusinessOrder($order) ? ConsumerStatus::Unconfirmed : ConsumerStatus::Consumer, ); } /** Spec section 2: the contract date is the day the Woo order was created, in the shop's time zone. */ public function contractDate(\WC_Order $order): LegalDate { $created = $order->get_date_created(); return LegalDate::fromInstant($created ?? new \DateTimeImmutable('now'), wp_timezone()); } /** * Estimated delivery: order completed + configured transit days. A * courier integration can confirm the real date through the filter. * * @return array{0:?LegalDate, 1:DeliverySource} */ public function delivery(\WC_Order $order): array { /** * Lets a courier or tracking plugin give the delivery date it knows. * * @param array{date:string, source:string}|null $delivery null to use the estimate; source 'confirmed' or 'estimated'. * @param \WC_Order $order */ $custom = apply_filters('abcreate_wd_delivery', null, $order); if (is_array($custom) && isset($custom['date'], $custom['source'])) { $source = DeliverySource::tryFrom((string) $custom['source']); if (null !== $source && DeliverySource::Unknown !== $source) { try { return [LegalDate::of((string) $custom['date']), $source]; } catch (\Throwable $e) { // Fall back to the estimate below. unset($e); } } } $completed = $order->get_date_completed(); if (null === $completed || !in_array($order->get_status(), ['completed', 'refunded'], true)) { return [null, DeliverySource::Unknown]; } $day = \DateTimeImmutable::createFromInterface($completed)->setTimezone(wp_timezone()); $day = $day->modify('+' . Options::transitDays() . ' days'); return [LegalDate::fromInstant($day, wp_timezone()), DeliverySource::Estimated]; } /** * Rule 6: an order with a company name or a VAT number may be a business * purchase. The dates stay hidden until the operator confirms the buyer. */ public function isBusinessOrder(\WC_Order $order): bool { $business = '' !== trim((string) $order->get_billing_company()); if (!$business) { /** * Order meta keys holding a VAT number. * * @param list $keys */ $keys = (array) apply_filters('abcreate_wd_vat_meta_keys', self::VAT_META_KEYS); foreach ($keys as $key) { if ('' !== trim((string) $order->get_meta((string) $key, true))) { $business = true; break; } } } /** * Whether the order may be a business purchase (the buyer then needs * confirming as a consumer before dates are shown). * * @param bool $business * @param \WC_Order $order */ return (bool) apply_filters('abcreate_wd_is_business_order', $business, $order); } /** * Art. 133 c. 4: the length agreed at the sale, stored on the order line * when the order was created (see WooHooks::rememberGuarantee). Never the * product as it is today: changing the catalogue does not change a * contract. A line without it (orders older than the plugin) gets the * legal 24 months; the operator corrects it on the unit, with a reason. */ public function guaranteeMonths(\WC_Order_Item_Product $item): int { $stored = $item->get_meta(Options::ITEM_MONTHS_META, true); $months = is_numeric($stored) ? (int) $stored : 0; if ($months < LegalRules::MIN_USED_GOODS_MONTHS || $months > LegalRules::GUARANTEE_MONTHS) { return LegalRules::GUARANTEE_MONTHS; } return $months; } /** The length set on the product now, for a line being sold now. */ public function productGuaranteeMonths(\WC_Order_Item_Product $item): int { $months = 0; foreach (array_filter([(int) $item->get_variation_id(), (int) $item->get_product_id()]) as $productId) { $product = wc_get_product($productId); $value = $product instanceof \WC_Product ? $product->get_meta(Options::PRODUCT_MONTHS_META, true) : ''; if ('' !== $value && null !== $value && false !== $value) { $months = (int) $value; break; } } if ($months < LegalRules::MIN_USED_GOODS_MONTHS || $months > LegalRules::GUARANTEE_MONTHS) { return LegalRules::GUARANTEE_MONTHS; } return $months; } public function customerName(\WC_Order $order): string { $name = trim($order->get_formatted_billing_full_name()); return '' !== $name ? $name : (string) $order->get_billing_company(); } }