woocommerce 自定义运输方式未出现在运输区域中

2023-11-22

已编辑 - 原始帖子取得的进展

我创建了一个简单的自定义运输方法插件存根(请参阅下面的代码)。

该插件已注册并且now当我创建运送区域时,会出现在运送方式下拉列表中。但是,选择此选项后,运送区域不会出现自定义字段(参见 gif)

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

    function launch_shipping_method() {
        if (!class_exists('Launch_Shipping_Method')) {

            class Launch_Shipping_Method extends WC_Shipping_Method {

                public function __construct( $instance_id = 0 ) {
                    $this->id = 'launch_shipping';
                    $this->instance_id          = absint( $instance_id );
                    $this->method_title         = __('Launch Simple Shipping', 'launch_shipping');
                    $this->method_description   = __('Custom Simple Shipping Method', 'launch_shipping');
                    $this->supports             = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );

                    $this->init();
                }

                /**
                 * Initialize Launch Simple Shipping.
                 */
                public function init() {
                    // Load the settings.
                    $this->init_form_fields();
                    $this->init_settings();

                    // Define user set variables.
                    $this->title    = isset($this->settings['title']) ? $this->settings['title'] : __('Launch Shipping', 'launch_shipping');

                    add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                }

                /**
                 * Init form fields.
                 */
                public function init_form_fields() {
                    $this->form_fields = array(
                        'title'      => array(
                            'title'         => __( 'Title', 'launch_shipping' ),
                            'type'          => 'text',
                            'description'   => __( 'This controls the title which the user sees during checkout.', 'launch_shipping' ),
                            'default'       => $this->method_title,
                            'desc_tip'      => true,
                        )
                    );
                }

                /**
                 * Get setting form fields for instances of this shipping method within zones.
                 *
                 * @return array
                 */
                public function get_instance_form_fields() {
                    return parent::get_instance_form_fields();
                }

                /**
                 * Always return shipping method is available
                 *
                 * @param array $package Shipping package.
                 * @return bool
                 */
                public function is_available( $package ) {
                    $is_available = true;
                    return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
                }

                /**
                 * Free shipping rate applied for this method.
                 *
                 * @uses WC_Shipping_Method::add_rate()
                 *
                 * @param array $package Shipping package.
                 */
                public function calculate_shipping( $package = array() ) {
                    $this->add_rate(
                        array(
                            'label'   => $this->title,
                            'cost'    => 0,
                            'taxes'   => false,
                            'package' => $package,
                        )
                    );
                }
            }
        }
    }
    add_action('woocommerce_shipping_init', 'Launch_Shipping_Method');

    function add_launch_shipping_method($methods) {
    $methods[] = 'launch_shipping_method';
    return $methods;
    }
    add_filter('woocommerce_shipping_methods', 'add_launch_shipping_method');

}

enter image description here


尝试这个:

function add_launch_shipping_method($methods) {
    $methods['launch_shipping'] = 'launch_shipping_method';
    return $methods;
}

它应该解决问题。如果您需要调试它或想了解更多信息,请查看:

wp-content/plugins/woocommerce/includes/class-wc-shipping-zone.php

look at

function add_shipping_method($type)

简而言之:您在您的帐户中注册课程的 IDwoocommerce_shipping_inithook 必须与类 id 属性(您设置的属性)匹配$this->id = 'launch_shipping';)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

woocommerce 自定义运输方式未出现在运输区域中 的相关文章

  • Azure 上的“phpcomposer.phar install”出现“无法终止进程”错误

    我正在尝试将我的 Symfony 2 应用程序部署到 Microsoft Azure 网站云 为此 我按照本指南中的步骤操作http symfony com doc current cookbook deployment azure web
  • 将数组拆分为特定数量的块

    我知道array chunk 允许将数组拆分为多个块 但块的数量根据元素的数量而变化 我需要的是始终将数组拆分为特定数量的数组 例如 4 个数组 以下代码将数组分为 3 个块 两个块各有 2 个元素 1 个块有 1 个元素 我想要的是将数组
  • 如何从字符串中删除所有数字?

    我想删除字符串 0 9 中的所有数字 我写了这段有效的代码 words preg replace 0 words remove numbers words preg replace 1 words remove numbers words
  • 如何在 codeigniter 查询中使用 FIND_IN_SET?

    array array classesID gt 6 this gt db gt select gt from this gt table name gt where array gt order by this gt order by q
  • mysqli bind_param 中的 NULL 是什么类型?

    我正在尝试将参数绑定到 INSERT INTO MySQLi 准备好的语句 如果该变量存在 否则插入 null 然后我知道 type variable i corresponding variable has type integer d
  • 在 PHP 中模拟 jQuery.ajax 请求

    我必须在 PHP 中模拟 AJAX 请求 就像在 jQuery 中一样 我当前的代码在这里 原始 AJAX 调用 不得修改 ajax type POST url someFile php data data success function
  • “pdo_mysql”已禁用,我无法启用它。我在 iMac 7.1 OSX 10.6.8 上安装了 MAMP v. 3.0.4

    pdo mysql 已禁用 我无法启用它 我在 iMac 7 1 OSX 10 6 8 上安装了 MAMP v 3 0 4 在我的 phpinfo 页面上 我可以看到唯一启用的 PDO 是 sqlite 如果我查看 php 5 5 10 扩
  • Laravel 5.4 升级 - 违反完整性约束 - 列不能为空

    奇怪的是 所有这些都在 5 2 中工作 但我不知道可以改变什么来实现这一点 下面是错误和正在插入的数组 SQLSTATE 23000 Integrity constraint violation 1048 Column gender can
  • Doctrine 1 和 Symfony 1 的多个主键?

    我已经知道在 Symfony 1 和 Doctrine 1 中不可能使用多个主键 但是你们知道有什么好的解决方法吗 除了多对多关系之外 原则 1 不适用于多列上的主键 但如果你想使用多对多关系 请像这样使用 BlogPost columns
  • Yii2 中 init() 和 __construct() 方法有什么区别

    init 方法 public function init construct method public function construct 那么 它们之间有什么区别 应该使用哪一个呢 init 是从以下对象扩展的任何对象的方法yii b
  • 在 null laravel 上调用成员函数 save()

    大家好 我正在使用 laravel 5 多态关系将数据保存在数据库中 但我遇到了一些问题 当我尝试将数据保存在数据库中时 它会抛出此错误 对 null 调用成员函数 save 我不知道为什么我会遇到这个错误 我正在关注多态关系的本教程在 L
  • 如何解决 Laravel 8 UI 分页问题?

    我在尝试最近发布的 laravel 8 时遇到了问题 我试图找出变化是什么以及它是如何工作的 当我这样做时 我遇到了分页 laravel 8 UI 变得混乱的问题 不知何故它发生了 有人可以帮助我吗 或者经历过同样的事情 像这样我在 lar
  • MySQL 数据库无法在 XAMPP for Mac 上启动

    突然我在 mac 上遇到了这个问题 我无法启动我的 MySQL 数据库 我只能启动 ProFTPD 和 Apache Web Server 这是应用程序日志 Starting all servers Starting MySQL Datab
  • PHP:在脚本完成之前获取输出

    我有一个名为 data php 的脚本 如下所示 do some stuff echo result do some other stuff eg database operations 我需要在另一个脚本中使用 data php 的输出
  • 简单的dom php解析获取自定义数据属性值

    HTML div class something ddsf PHP foreach dom gt find something data rel as this var dump this gt attr 我尝试了这个但错误 在其文档中找不
  • 将按钮文本放在一行上

    我的按钮文本在 safari 中显示在一行上 即使在初次单击后 但是在 google chrome 上 当您第一次到达该按 钮时 我的按钮将显示在一行上 但是当您浏览更多帖子并再次遇到 加载更多 按钮时 文本搞砸了 这只发生在谷歌浏览器上
  • 如何确保在 PHP 的“foreach”循环中重置该值?

    我正在写一个简单的 PHP 页面和一些foreach使用了循环 以下是脚本 arrs array a b c foreach arrs as arr if substr arr 0 1 b echo This is b End of fir
  • 接口中的构造方法

    接口中的构造方法不好吗 为什么人们认为有人想要实例化接口 我们想要做的是强制实现者实现构造函数 就像其他接口方法一样 接口就像一个合同 假设我有一个接口 Queue 并且我想确保实现者创建一个带有一个参数的构造函数 该构造函数创建一个单例队
  • 合并 url 中的 2 个输入值

    我有这样的形式
  • 如何在 PHP 中从字符串类名实例化? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 如何创建返回方法名称的新实例 不幸的是我收到这个错误 错误 类名必须是有效的对象或字符串 这是我的代码 class Foo public f

随机推荐