/** * Prints rate json for a bring service. * * Only available from admin. * @todo: refactor!! */ static function wp_ajax_get_rate() { header('Content-type: application/json'); $result = ['success' => false, 'rate' => null, 'packages' => null]; if (isset($_GET['post_id']) && isset($_GET['service'])) { $order = new WC_Order($_GET['post_id']); $items = $order->get_items(); $fake_cart = []; foreach ($items as $item) { $fake_cart[uniqid()] = ['quantity' => $item['qty'], 'data' => new WC_Product_Simple($item['product_id'])]; } //include( '../../common/class-fraktguiden-packer.php' ); $packer = new Fraktguiden_Packer(); $product_boxes = $packer->create_boxes($fake_cart); $packer->pack($product_boxes, true); $package_params = $packer->create_packages_params(); //@todo: share / filter $standard_params = array('clientUrl' => $_SERVER['HTTP_HOST'], 'from' => Fraktguiden_Helper::get_option('from_zip'), 'fromCountry' => Fraktguiden_Helper::get_option('from_country'), 'to' => $order->shipping_postcode, 'toCountry' => $order->shipping_country, 'postingAtPostOffice' => Fraktguiden_Helper::get_option('post_office') == 'no' ? 'false' : 'true', 'additional' => Fraktguiden_Helper::get_option('evarsling') == 'yes' ? 'evarsling' : ''); $params = array_merge($standard_params, $package_params); $url = add_query_arg($params, WC_Shipping_Method_Bring::SERVICE_URL); $url .= '&product=' . $_GET['service']; // Make the request. $request = new WP_Bring_Request(); $response = $request->get($url); if ($response->status_code == 200) { $json = json_decode($response->get_body(), true); $service = $json['Product']['Price']['PackagePriceWithoutAdditionalServices']; $rate = Fraktguiden_Helper::get_option('vat') == 'exclude' ? $service['AmountWithoutVAT'] : $service['AmountWithVAT']; $result['success'] = true; $result['rate'] = $rate; $result['packages'] = json_encode($package_params); } } echo json_encode($result); die; }
public function pack_order($cart) { $packer = new Fraktguiden_Packer(); $product_boxes = $packer->create_boxes($cart); // // Create an array of 'product boxes' (l,w,h,weight). // $product_boxes = array(); // // /** @var WC_Cart $cart */ // $cart = $woocommerce->cart; // foreach ( $cart->get_cart() as $values ) { // // /** @var WC_Product $product */ // $product = $values['data']; // // if ( ! $product->needs_shipping() ) { // continue; // } // $quantity = $values['quantity']; // for ( $i = 0; $i < $quantity; $i++ ) { // if ( ! $product->has_dimensions() ) { // // If the product has no dimensions, assume the lowest unit 1x1x1 cm // $dims = array( 0, 0, 0 ); // } // else { // $dims = array( // $product->length, // $product->width, // $product->height // ); // } // // // Workaround weird LAFFPack issue where the dimensions are expected in reverse order. // rsort( $dims ); // // $box = array( // 'length' => $dims[0], // 'width' => $dims[1], // 'height' => $dims[2], // 'weight' => $product->weight, // 'weight_in_grams' => $packer->get_weight( $product->weight ) // For $packer->exceeds_max_package_values only. // ); // // // Return if product is larger than available Bring packages. // if ( $packer->exceeds_max_package_values( $box ) ) { // return; // } // // $product_boxes[] = $box; // } // } if (!$product_boxes) { return false; } // Pack product boxes. $packer->pack($product_boxes, true); // Create the url. return $packer->create_packages_params(); }