Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic
CheckoutSessionSchema::format_totals
Format totals array.
Метод класса: CheckoutSessionSchema{}
Хуков нет.
Возвращает
Массив. Totals array.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->format_totals( $cart );
- $cart(WC_Cart) (обязательный)
- Cart object.
Код CheckoutSessionSchema::format_totals() CheckoutSessionSchema::format totals WC 11.0.1
protected function format_totals( $cart ) {
$totals = [];
// Items base amount.
$items_base = 0;
foreach ( $cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
$items_base += $product->get_price() * $cart_item['quantity'];
}
$totals[] = [
'type' => TotalType::ITEMS_BASE_AMOUNT,
'display_text' => __( 'Items Base Amount', 'woocommerce' ),
'amount' => $this->amount_to_cents( $items_base ),
];
// Items discount.
$discount = $cart->get_cart_discount_total();
$totals[] = [
'type' => TotalType::ITEMS_DISCOUNT,
'display_text' => __( 'Items Discount', 'woocommerce' ),
'amount' => $this->amount_to_cents( $discount ),
];
// Subtotal.
$totals[] = [
'type' => TotalType::SUBTOTAL,
'display_text' => __( 'Subtotal', 'woocommerce' ),
'amount' => $this->amount_to_cents( $cart->get_subtotal() - $discount ),
];
// Fulfillment (shipping).
$totals[] = [
'type' => TotalType::FULFILLMENT,
'display_text' => __( 'Shipping', 'woocommerce' ),
'amount' => $this->amount_to_cents( $cart->get_shipping_total() ),
];
// Tax.
$totals[] = [
'type' => TotalType::TAX,
'display_text' => __( 'Tax', 'woocommerce' ),
'amount' => $this->amount_to_cents( $cart->get_total_tax() ),
];
// Total.
$totals[] = [
'type' => TotalType::TOTAL,
'display_text' => __( 'Total', 'woocommerce' ),
'amount' => $this->amount_to_cents( $cart->get_total( 'edit' ) ),
];
return $totals;
}