Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Handle Dimension Calculation
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- class Dimension_Calculation {
- private $widths = [];
- private $heights = [];
- private $lengths = [];
- private $weight = [];
- public function get_dimension_from_package( $package_contents ) {
- if ( ! empty( $package_contents['contents'] ) ) {
- foreach ( $package_contents['contents'] as $package ) {
- /**
- * @var $product WC_Product
- */
- $product = $package['data'];
- $quantity = (int) $package['quantity'];
- // dimension
- $this->heights[] = (int) $product->get_height() * $quantity;
- $this->widths[] = $product->get_width();
- $this->lengths[] = $product->get_length();
- $this->weight[] = (int) $product->get_weight() * $quantity;
- }
- }
- }
- public function get_dimension_from_order_items( $items ) {
- foreach ( $items as $item ) {
- $detail_item = $item->get_data();
- $product = wc_get_product( $detail_item['product_id'] );
- $quantity = (int) $item->get_quantity();
- // dimension
- $this->heights[] = (int) $product->get_height() * $quantity;
- $this->widths[] = $product->get_width();
- $this->lengths[] = $product->get_length();
- $this->weight[] = (int) $product->get_weight() * $quantity;
- }
- }
- /**
- * @return mixed
- */
- public function get_height() {
- if ( ! empty( $this->heights ) ) {
- return array_sum( $this->heights );
- }
- return 0;
- }
- /**
- * @return mixed
- */
- public function get_width() {
- if ( ! empty( $this->widths ) ) {
- return max( $this->widths );
- }
- return 0;
- }
- /**
- * @return mixed
- */
- public function get_length() {
- if ( ! empty( $this->lengths ) ) {
- return max( $this->lengths );
- }
- return 0;
- }
- public function get_weight() {
- if ( ! empty($this->weight) ) {
- return array_sum( $this->weight );
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement