is_pricing_table_applicable( $cart_item['quantity'], $pricing_table ) ) { return; } $cart_item['data']->set_price( $this->_calculate_price( $cart_item['quantity'], $pricing_table, $this->get_cart_item_id( $cart_item ) ) ); } /** * Calculate Pricing Model Function. * * @param int $quantity * @param array $pricing_table * @param int $product_id * @return mixed */ public function _calculate_price( $quantity, $pricing_table, $product_id, $return_total_price = false ) { $total_price = 0; $_product = wc_get_product( $product_id ); $base_price = (float) $_product->get_price(); // Loop over the Cart item Quantity => Get the target Price. for ( $i = 1; $i <= $quantity; $i++ ) { $target_pricing_division = $this->get_pricing_division( $pricing_table, $i ); if ( is_null( $target_pricing_division ) ) { $total_price += $base_price; } else { $total_price += $target_pricing_division['price']; } } return ( $return_total_price ? $total_price : ( $total_price / $quantity ) ); } /** * Get the current Quantity counter position in the pricing table quantities division. * * @param array $pricing_table * @param int $quantity_counter * @return array|null Pricing Row. */ private function get_pricing_division( $pricing_table, $quantity_counter ) { $target_pricing_row = null; $closest_qty = 0; foreach ( $pricing_table as $pricing_row ) { if ( $pricing_row['quantity'] === $quantity_counter ) { $target_pricing_row = $pricing_row; break; } if ( ( $quantity_counter > $pricing_row['quantity'] ) && ( $pricing_row['quantity'] > $closest_qty ) ) { $closest_qty = $pricing_row['quantity']; $target_pricing_row = $pricing_row; } } return $target_pricing_row; } /** * Pricing Table HTML. * * @param int|\WC_Product $product_id * @return mixed */ public function pricing_table_html( $product_id, $echo = false ) { if ( is_int( $product_id ) ) { $_product = wc_get_product( $product_id ); } else { $_product = $product_id; $product_id = $_product->get_id(); } $pricing_table = $this->get_product_pricing_table( $product_id, true ); if ( empty( $pricing_table ) ) { return; } if ( ! $echo ) { ob_start(); } ?>