tierDiscounts = $discounts; $this->tierDiscountSettings = $settings; } /** * Whether price strikes are enabled. * * @return bool */ protected function tierStrikeEnabled(): bool { return null === $this->tierDiscountSettings || $this->tierDiscountSettings->strikethroughEnabled(); } /** * Discounted unit prices keyed by tier id. * * @param Product $product The product whose tiers to price. * @return array The discounted unit price keyed by price id (only covered tiers present). */ protected function tierDiscountMap( Product $product ): array { if ( null === $this->tierDiscounts ) { return array(); } $nodes = array(); $priceIds = array(); foreach ( $product->prices() as $price ) { if ( ! $price->isPurchasable() ) { continue; } $nodes[] = array( 'product_id' => (int) $product->id(), 'subtotal' => $price->amount(), ); $priceIds[] = (int) $price->id(); } if ( array() === $nodes ) { return array(); } try { $best = $this->tierDiscounts->bestForNodes( $nodes, null ); } catch ( DiscountPersistenceException $e ) { return array(); } $result = array(); foreach ( array_values( $nodes ) as $index => $node ) { $application = $best[ $index ] ?? null; if ( null !== $application ) { $result[ $priceIds[ $index ] ] = (int) $node['subtotal'] - $application->total(); } } return $result; } }