使用3.0 API的WooCommerce运输方式



您好,我正在尝试为 wooCommerce 构建一个非常简单的插件,以显示有关无税交付的一些信息。其他方法的不同之处在于额外的 HTML 字段。

我阅读了很多文档,但我认为配置上有一些遗漏。管理员似乎在工作。但是该方法没有出现在结帐屏幕上。代码如下:

<?php
if ( ! defined( 'WPINC' ) ) {
   die('security by preventing any direct access to your plugin file');
}
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
function shipping_delivery_info() {
    if (!class_exists('shipping_delivery_info')) {
        class shipping_delivery_info extends WC_Shipping_Method {
            public function __construct( $instance_id = 0) {
                $this->id = 'shipping_delivery_info';
                $this->instance_id = absint( $instance_id );
                $this->method_title = __('Shipping Delivery Info', 'shipping_delivery_info');
                $this->method_description = __('A Woocommerce custom shipping method plugin, that shows ' . 
                    'some shipping information to costumer, like free shipping but with HTML field.', 
                    'shipping_delivery_info');
                $this->supports = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',                      
                );
                $this->init();
            }
             /**
              * Load the settings API
              */
             function init() {
                // Load the settings
                $this->init_form_fields();
                $this->init_settings();
                $this->enabled = $this->get_option( 'enabled' );
                $this->title   = $this->get_option( 'title' );
                $this->info   = $this->get_option( 'info' );
                add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
             }
             function init_form_fields() {
                $this->instance_form_fields = array(
                    'enabled' => array(
                        'title'         => __( 'Enable/Disable', 'shipping_delivery_info'),
                        'type'          => 'checkbox',
                        'label'         => __( 'Enable this shipping method', 'shipping_delivery_info'),
                        'default'       => 'yes',
                    ),
                    'title' => array(
                        'title' => __('Title', 'shipping_delivery_info'),
                        'type' => 'text',
                        'description' => __( 'The title to be displayed during checkout.', 'shipping_delivery_info' ),
                        'default' => __('Shipping Information', 'shipping_delivery_info'),
                    ),
                    'info' => array(
                        'title' => __('Information', 'shipping_delivery_info'),
                        'type' => 'text',
                        'description' => __( 'Information about delivery and its taxes.', 'shipping_delivery_info' ),
                        'default' => __('Insert here some HTML.'),
                    ),
                );
             }
        }
    }
}
add_action('woocommerce_shipping_init', 'shipping_delivery_info');
function add_shipping_delivery_info($methods)
{
    $methods['shipping_delivery_info'] = 'shipping_delivery_info';
    return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_shipping_delivery_info');
function shipping_delivery_info_message($posted)
{
    $packages = WC()->shipping->get_packages();
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    if (is_array($chosen_methods) && in_array('shipping_delivery_info', $chosen_methods)) {
        foreach ($packages as $i => $package) {
            if ($chosen_methods[$i] != "shipping_delivery_info") {
                continue;
            }
            $shipping_delivery_info = new shipping_delivery_info();
            $message = $shipping_delivery_info->settings['info'];
            return $message;
            /*$messageType = "info";                
            wc_add_notice($message, $messageType);*/
        }
    }
}
add_action('woocommerce_review_order_before_cart_contents', 'shipping_delivery_info_message', 10);
add_action('woocommerce_after_checkout_validation', 'shipping_delivery_info_message', 10);
}

您需要将 calculate_shipping 函数添加到您的类中

       /**
         * function calculate_shipping.
         *
         * @access public
         * @param mixed $package
         * @return void
         */
        public function calculate_shipping( $package = array() ) {
                $rate = array(
                   'id' => 'My Method id',
                   'label' => 'New method',
                   'cost' => 0,
                   'calc_tax' => 'per_item'
                 );
                $this->add_rate( $rate );   
            }
        }

无论如何,我认为您不需要添加新的运输方式只是为了在购物车中显示一些信息,而是可以使用以下操作:

add_action( 'woocommerce_review_order_before_submit', 'add_tracking_notification', 12 );
add_action('woocommerce_proceed_to_checkout', 'add_tracking_notification');
function add_tracking_notification() {
        echo '<h5 style="margin-bottom:10px">This is a custom message</h5>';
}

最新更新