【问题标题】:woocommerce third party shipping methodswoocommerce 第三方运输方式
【发布时间】:2020-07-01 07:08:15
【问题描述】:

我创建了 woocommerce 插件,它从第三方 URL(我的网站)获取运费并将其显示在结帐页面上。当我单击一种运输方式时,它还会计算正确的运费,但是当我再次刷新结帐页面时,我选择的运输选项会丢失,我必须再次选择。此外,我的自定义送货选项在成功下单后无法下单。

在下面的屏幕截图中,我传递了郊区和邮政编码,我得到了我想要的所有费率,当我点击一种运输方式时,它也会计算正确的运费

点击下单后,采用第一种发货方式,统一费率:5美元,请看下面的截图

这是我的插件,请查看并指导我哪里出错了。

<?php
/**
 * Plugin Name: Iconsignit Shipping
 * Plugin URI: http://Iconsignit.com.au
 * Description: Iconsignit Shipping Method for WooCommerce
 * Version: 1.0.0
 * Author: Jaimin prajapati
 * Author URI: http://www.webbrainstechnologies.com
 * License: GPL-3.0+
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 * Domain Path: /
 * Text Domain: Iconsignit
 */

if (!defined('WPINC')) {
    die;
}

/*
 * Check if WooCommerce is active
 */
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

    function iconsignit_shipping_method()
    {
        if (!class_exists('Iconsignit_Shipping_Method')) {
            class Iconsignit_Shipping_Method extends WC_Shipping_Method
            {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct($instance_id = 0)
                {
                    $this->id = 'iconsignit';
                    $this->instance_id = absint($instance_id);
                    $this->method_title = __('Iconsignit Shipping', 'iconsignit');
                    $this->method_description = __('Custom Shipping Method for iconsignit', 'iconsignit');
                    $this->init();
                    $this->enabled = isset($this->settings['enabled']) ? $this->settings['enabled'] : 'yes';
                    $this->title = isset($this->settings['title']) ? $this->settings['title'] : __('Iconsignit Shipping', 'iconsignit');
                }

                /**
                 * Init your settings
                 *
                 * @access public
                 * @return void
                 */
                function init()
                {
                    // Load the settings API
                    $this->init_form_fields();
                    $this->init_settings();
                    // Save settings in admin if you have any defined
                    add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                }

                /**
                 * Define settings field for this shipping
                 * @return void
                 */
                function init_form_fields()
                {

                    // We will add our settings here
                    $this->form_fields = array(
                        'ApiToken' => array(
                            'title' => __('Api Token', 'iconsignit-integration-demo'),
                            'type' => 'text',
                            'description' => __('Enter with your API Key. You can find this in "User Profile" drop-down (top right corner) > API Keys.', 'iconsignit-integration-demo'),
                            'desc_tip' => true,
                            'default' => '',
                        ),
                        'ApiUrl' => array(
                            'title' => __('Api Url', 'iconsignit-integration-demo'),
                            'type' => 'text',
                            'default' => '',
                            'desc_tip' => true,
                            'description' => __('Website URL', 'iconsignit-integration-demo'),
                        ),
                        'ConnectIconsignUrl' => array(
                            'title' => __('Iconsignit Url', 'iconsignit-integration-demo'),
                            'type' => 'text',
                            'default' => '',
                            'desc_tip' => true,
                            'description' => __('Url from where all shipping rates will come', 'iconsignit-integration-demo'),
                        ),
                        
                    );

                }

                /**
                 * This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
                 *
                 * @access public
                 * @param mixed $package
                 * @return void
                 */
                public function calculate_shipping($package = array())
                {

                    // We will add the cost, rate and logics in here
                    $item = array();
                    $count = 0;
                    foreach ($package['contents'] as $item_id => $values) {
                        $item[$count]['item_qty'] = $values['quantity'];
                        $item[$count]['item_length'] = $values['data']->get_length();
                        $item[$count]['item_width'] = $values['data']->get_width();
                        $item[$count]['item_height'] = $values['data']->get_height();
                        $item[$count]['item_weight'] = $values['data']->get_weight();
                        $item[$count]['item_palletised'] = 0;
                        $count++;
                    }
                    $country = $_POST['s_country'];
                    $state = $_POST['s_state'];
                    $postcode = $_POST['s_postcode'];
                    $city = $_POST['s_city'];
                    $address = $_POST['s_address'];
                    $address_2 = $_POST['s_address_2'];
                    $isCredentials = get_option('woocommerce_iconsignit_settings');
                    $data = array('ApiUrl' => $isCredentials['ApiUrl'], 'ApiToken' => $isCredentials['ApiToken'], 'DeliveryTown' => $city, 'DeliveryPostcode' => $postcode, 'Items' => $item);
                    $isResponse = Requests::post($isCredentials['ConnectIconsignUrl'].'/api/getconsignrate', array(), $data);

                    $resp = json_decode($isResponse->body, true);
                    $counter = 1;
                    foreach ($resp['result'] as $key => $res) {
                        $rate = array(
                            'id' => $res['QuoteRateID'],//$this->id,
                            'label' => $res['carrier_nm'] . "-(" . $res['service_nm'] . ")",
                            'cost' => $res['total_charge'],
                            'calc_tax' => 'per_item',
                        );
                        $this->add_rate($rate);
                        $counter++;
                    }
                }
            }
        }
    }

    add_action('woocommerce_shipping_init', 'iconsignit_shipping_method');

    function add_iconsignit_shipping_method($methods)
    {
        $methods[] = 'Iconsignit_Shipping_Method';
        return $methods;
    }

    add_filter('woocommerce_shipping_methods', 'add_iconsignit_shipping_method');
    

    
}

【问题讨论】:

  • 不,这工作正常,它根本不会打扰我,问题是在订购发货方式之前。无论我选择什么重置为默认统一费率 5

标签: woocommerce


【解决方案1】:

问题已解决,问题在 $this->id 中,它必须是唯一的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2014-07-05
    • 2020-08-12
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多