首页 > 解决方案 > 以自定义运输方式获取客户地址

问题描述

我目前正在为 WooCommerce 开发一种自定义运输方式。目标是根据客户在订单流程中输入的地址计算运费。我们目前有一个 API,它接收 storeId 和客户的地址,并返回我们需要应用于订单的确切运费金额。

为此,我开始编写一个新的自定义运输订单插件。我已经收到带有订单的 $packages 数组,但是我似乎找不到在 calculate_shipping 方法中接收客户地址的方法。在 reviewOrderBeforePayment 页面上选择首选仓库还需要一个用户操作,我还不太确定如何访问该数据。

<?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');

标签: wordpresswoocommerceshipping-method

解决方案


推荐阅读