【问题标题】:Add new shipping method to Woocommerce programmatically以编程方式向 Woocommerce 添加新的运输方式
【发布时间】:2015-12-07 05:15:21
【问题描述】:

我正在尝试向我的 Woocommerce 套件添加新的运输方式。

基本上,我尝试根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个。

我已尝试使用the shipping api 创建一个新类,但似乎没有任何显示。

<?php namespace MySite;

use WC_Shipping_Method;

    function your_shipping_method_init() {
        if ( ! class_exists( 'CustomShipping' ) ) {

            class CustomShipping extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct() {
                    $this->id                 = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'Your Shipping Method' );  // Title shown in admin
                    $this->method_description = __( 'Description of your shipping method' ); // Description shown in admin

                    $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                    $this->title              = "My Shipping Method"; // This can be added as an setting but for this example its forced.

                    $this->init();
                }

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

                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 * @param mixed $package
                 * @return void
                 */
                public function calculate_shipping( $package ) {
                    $rate = array(
                        'id' => $this->id,
                        'label' => $this->title,
                        'cost' => '10.99',
                        'calc_tax' => 'per_item'
                    );

                    // Register the rate
                    $this->add_rate( $rate );
                }
            }
    }

    add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

    function add_your_shipping_method( $methods ) {
        $methods[] = 'CustomShipping';
        return $methods;
    }

    add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}

我使用 Composer 加载我的类。

我不应该在 Woocommerce Shipping 设置中看到新选项吗?任何指导表示赞赏。

另一种方法是截取会话数据并在那里更改运费总额和税金。然而,这似乎不起作用。我在哪里可以找到有关运输信息来源的更多数据;何时何地调用它?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您的代码中有两个问题。第一个是您在附加到它的函数中添加woocommerce_shipping_init 的操作(因此它永远不会被调用),第二个是您没有使用您指定的命名空间namespace MySite

    如果您检查代码的缩进,第一个问题更容易看到,它都包含在 init 函数中。

    一旦 WooCommerce 尝试加载类,第二个问题就会在您的日志中出现,并出现如下错误,但由于第一个问题,它没有这样做。

    PHP 致命错误:在第 136 行的 /wp-content/plugins/woocommerce/includes/class-wc-shipping.php 中找不到类 'WC_Your_Shipping_Method'

    PHP 警告:call_user_func_array() 期望参数 1 是有效回调,未找到函数“add_your_shipping_method”或 /wp-includes/plugin.php 第 213 行中的函数名称无效

    我在下面删除了您的实现代码,但在开发网站上对此进行了测试,它在 WooCommerce 设置中显示为一种运输方式。

    /*
      Plugin Name: WooCommerce Your Shipping Method
      Description: Test shipping method
      Version: 1.0
     */
    
    namespace MySite;
    
    use WC_Shipping_Method;
    
    function your_shipping_method_init() {
        if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
    
            class WC_Your_Shipping_Method extends WC_Shipping_Method {
                // ... all your code for the implementation
            }
        }
    }  // <-- note that the function is closed before the add_action('woocommerce_shipping_init')
    
    // note "MySite\" prepended to the attached function
    add_action( 'woocommerce_shipping_init', 'MySite\your_shipping_method_init' );
    
    // note "MySite\" prepended to the shipping method class name
    function add_your_shipping_method( $methods ) {
        $methods[] = 'MySite\WC_Your_Shipping_Method';
        return $methods;
    }
    
    // note "MySite\" prepended to the attached function
    add_filter( 'woocommerce_shipping_methods', 'MySite\add_your_shipping_method' );
    

    【讨论】:

    • 啊,是的,我的操作是嵌套的。 @doublesharp 我已经实现了你的代码,似乎your_shipping_method_init 甚至从未被调用过。死在里面并倾倒它不会做任何事情。我的add_actionadd_filter 调用不应该在这个文件中吗?
    • 你把代码放在哪里了?唯一的区别是我在文件中添加了一个标题以使其成为插件,然后启用它,否则它在我的测试站点上工作正常。你也可以把它放到mu-plugins
    • 我用插件头更新了示例代码。创建一个名为/wp-content/plugins/my-shipping/ 的文件夹并将文件放入其中,然后在您的插件中启用“WooCommerce Your Shipping Method”,它应该会显示在 WooCommerce 设置中。
    • 这是一个基础的 WP 站点。我所有的课程都存在于app/plugins/myfunctions/src/ 中,其中myfunctions 是启用的插件。这个类文件是CustomShipping.php。我使用 pimple 容器进行依赖注入。
    • 当我把所有东西都放在我的functions.php文件中时它可以工作,但是我将如何使用上述结构来完成这个?
    【解决方案2】:

    我不确定您的代码的确切问题,但其他运输插件如下所示,所以我可能会复制它们:

    先把你的方法放到另一个文件classes/class-wc-your-shipping-method.php

    if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
    
        class WC_Your_Shipping_Method extends WC_Shipping_Method {
            // your class stuff truncated for brevity
        }
    
    }
    

    然后在插件中的某个位置包含运输类文件:

    function so_32509983_init() {
        include_once( 'classes/class-wc-your-shipping-method.php' );
    }
    add_action( 'woocommerce_shipping_init', 'so_32509983_init' );
    

    最后告诉 WooCommerce 将您的方法初始化为其活动方法之一:

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

    【讨论】:

    • 函数内部的类很好,问题是add_action() 调用在它所附加的函数内部,所以它永远不会被调用。您还必须在 classes\functions 上指定命名空间,例如 $methods[] = 'MySite\WC_Your_Shipping_Method'
    • 我想我不知道命名空间。没有命名空间,这正是 UPS 和 USPS 功能的工作方式,因此它确实回答了“如何向 WooCommerce 添加新的运输方式”的问题。
    • 有点,只是它没有回答所提出的问题。主要问题是钩子函数内部的添加操作调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2021-01-30
    • 2016-10-20
    • 2015-03-06
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多