Getting customer adress in custom shipping method

Viewed 31

I'm currently working on a custom shipping method for WooCommerce. The goal is to calculate the shipping based on the adress the customer enters in the order process. We currently have an API that receives a storeId and the adress of a customer and returns back the exact amount of shipping we need to apply to the order.

For this, I've started to write a new custom shipping order plugin. I am already receiving the $packages array with the order, however I can't seem to find a way to receive the customers adress in the calculate_shipping method. There also will be a useraction required to select the prefered warehouse on the reviewOrderBeforePayment page and I am not quite sure how to access that data yet.

<?php
/*
* Plugin Name: Warehouses Integration
* Description: WooCommerce integration of the Warehouses API
* Version: 1.1.0
* Author: -
* Author URI: -
* Developer: Laura Heimann
* Developer URI: -
* Text Domain: woocommerce-extension
* WC requires at least: 4.0.1
* WC tested up to: 4.0.1
*/

// Security Measure
if (!defined('ABSPATH')) {
    exit;
}

// Check if WooCommerce is installed
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option( 'active_plugins')))) {
    alert("WooCommerce is not installed!");
}

// Main Render Function
// This will be hooked into the WooCommerce Order Page
function wcfw_render() {
    // TODO: Show List of possible stores, let user select and save selected store somehow
    echo "WCFWRENDER";
}

function wcfw_renderThankYou() {
    echo "WCFWTHANKYOU";
}

function wcfw_renderReviewOrder() {
    // TODO: Show warning if items from different stores
    echo "WCFWREVIEWORDER";
}

function wcfw_shippingMethodInit() {
    if ( ! class_exists( 'WC_WCFW_Shipping_Method' ) ) {
        class WC_WCFW_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct() {
                $this->id                 = 'wcfw_shipping_method';
                $this->title       = __( 'Warehouses Shipping' );
                $this->method_description = __( 'Shipping Method through the Warehouse' );
                $this->enabled            = "yes";
                $this->init();
            }
    
            /**
             * Init your settings
             *
             * @access public
             * @return void
             */
            function init() {
                // Load the settings API
                $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
                $this->init_settings(); // This is part of the settings API. Loads settings you previously init.
    
                // Save settings in admin if you have any defined
                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
            }

            /**
             * Settings
             */
            function init_form_fields() {
                $this->form_fields = array(
                    'apiBase' => array(
                        'title' => __('Api Base'),
                        'type' => 'text',
                        'description' => __('The base URL for API-Calls.'),
                        'default' => __('---')
                        ),
                );
            }
    
            /**
             * calculate_shipping function.
             *
             * @access public
             * @param mixed $package
             * @return void
             */
            public function calculate_shipping( $package = array() ) {
                var_dump( $package );

                // TODO: Get Customers Adress and selected store

                // TODO: Send data to API and apply provided shipping

                $rate = array(
                    'label'    => __('Shipping by Truck'),
                    'cost'     => '10.99',
                    'calc_tax' => 'per_item'
                );
                
                // Register the rate
                $this->add_rate( $rate );
            }
        }
    }
}

function add_wcfw_shipping_method( $methods ) {
    $methods[] = 'WC_WCFW_Shipping_Method'; 
    return $methods;
}

// Hooks
add_action('woocommerce_review_order_before_payment', 'wcfw_render', 10);
add_action('woocommerce_thankyou', 'wcfw_renderThankYou', 10);
add_action('woocommerce_after_cart_contents', 'wcfw_renderReviewOrder', 10);
add_action('woocommerce_shipping_init', 'wcfw_shippingMethodInit');
add_filter('woocommerce_shipping_methods', 'add_wcfw_shipping_method');
0 Answers
Related