Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Handle Dimension Calculation
  4.  */
  5.  
  6. if ( ! defined( 'ABSPATH' ) ) {
  7.     exit;
  8. }
  9.  
  10. class Dimension_Calculation {
  11.  
  12.     private $widths = [];
  13.     private $heights = [];
  14.     private $lengths = [];
  15.     private $weight = [];
  16.  
  17.  
  18.     public function get_dimension_from_package( $package_contents ) {
  19.  
  20.         if ( ! empty( $package_contents['contents'] ) ) {
  21.             foreach ( $package_contents['contents'] as $package ) {
  22.  
  23.                 /**
  24.                  * @var $product WC_Product
  25.                  */
  26.                 $product = $package['data'];
  27.  
  28.                 $quantity = (int) $package['quantity'];
  29.  
  30.                 // dimension
  31.                 $this->heights[] = (int) $product->get_height() * $quantity;
  32.                 $this->widths[]  = $product->get_width();
  33.                 $this->lengths[] = $product->get_length();
  34.  
  35.                 $this->weight[] = (int) $product->get_weight() * $quantity;
  36.             }
  37.         }
  38.     }
  39.  
  40.     public function get_dimension_from_order_items( $items ) {
  41.         foreach ( $items as $item ) {
  42.             $detail_item = $item->get_data();
  43.             $product     = wc_get_product( $detail_item['product_id'] );
  44.  
  45.             $quantity = (int) $item->get_quantity();
  46.             // dimension
  47.             $this->heights[] = (int)  $product->get_height() * $quantity;
  48.             $this->widths[]  = $product->get_width();
  49.             $this->lengths[] = $product->get_length();
  50.             $this->weight[]  = (int) $product->get_weight() * $quantity;
  51.         }
  52.     }
  53.  
  54.     /**
  55.      * @return mixed
  56.      */
  57.     public function get_height() {
  58.         if ( ! empty( $this->heights ) ) {
  59.             return array_sum( $this->heights );
  60.         }
  61.  
  62.         return 0;
  63.     }
  64.  
  65.     /**
  66.      * @return mixed
  67.      */
  68.     public function get_width() {
  69.  
  70.         if ( ! empty( $this->widths ) ) {
  71.             return max( $this->widths );
  72.         }
  73.  
  74.         return 0;
  75.     }
  76.  
  77.     /**
  78.      * @return mixed
  79.      */
  80.     public function get_length() {
  81.         if ( ! empty( $this->lengths ) ) {
  82.             return max( $this->lengths );
  83.  
  84.         }
  85.         return 0;
  86.     }
  87.  
  88.     public function get_weight() {
  89.         if ( ! empty($this->weight) ) {
  90.             return array_sum( $this->weight );
  91.         }
  92.  
  93.         return 0;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement