WooCommerce - 通过在运输方式中挂钩的功能将信息添加到“谢谢”页面

问题描述

我有一个自定义运输方式,我希望它在感谢页面和发送给客户的电子邮件显示其描述(运输说明)。就像支付网关一样。我在我的运输方式 woocommerce_thankyou 函数中挂接到 init() 钩子。它有效,但是......它像八次一样有效。这些挂钩函数多次触发。我的送货方式如下所示:

<?PHP
class WC_Shipping_XY extends WC_Shipping_Method {

    function __construct( $instance_id = 0 ) {
        
        $this->id = 'my-shipping';
        // etc etc
        
        $this->init();
    }
    

    function init() {

        $this->init_form_fields();
        // etc
        
        add_action( 'woocommerce_thankyou',array( $this,'woocommerce_thankyou' ),5,1 ); 
    }
    
    
    function woocommerce_thankyou( $order_id ) {
        
        $order = wc_get_order( $order_id );
        
        if( $order->has_shipping_method('my-shipping') ) {
            
            echo wpautop( wptexturize( $this->description ) );
            
        }
    }   
        
}

在感谢页面上多次重复运送方法描述。我不知道为什么。运输方法构造函数是否被多次调用? (显然是……)

解决方法

我认为这是应该的。

  • 您创建自定义送货方式。
if (in_array('woocommerce/woocommerce.php',apply_filters('active_plugins',get_option('active_plugins')))) {

    function your_shipping_method_init()
    {
        if (!class_exists('WC_Your_Shipping_Method')) {
            class WC_Your_Shipping_Method extends WC_Shipping_Method
            {

                public $description = null;


                /**
                 * 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->description = __('Some other description'); // Description shown in admin

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

                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 * @param mixed $package
                 * @return void
                 */
                public function calculate_shipping($package = array())
                {
                    $rate = array(
                        '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['your_shipping_method'] = 'WC_Your_Shipping_Method';
        return $methods;
    }

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

请注意,它内部有一个 description 类变量,这与 WooCommerce 使用的 method_description 不同。

  • 像这样在课堂外使用谢谢钩子来获取描述。
add_action('woocommerce_thankyou','woocommerce_thankyou',5,1);


function woocommerce_thankyou($order_id)
{
    $order = wc_get_order($order_id);

    if ($order->has_shipping_method('your_shipping_method')) {
        $shipping = new WC_Your_Shipping_Method();
        $description = $shipping->description;
        echo wpautop(wptexturize($description));
    }
}

我已经测试了这段代码并且可以正常工作。

截图如下。您可以在感谢页面上看到 Some other description

enter image description here

,

好吧,我终于发现这种行为是“设计使然”,有人在 WooCommerce Github (issue) 上提出了同样的问题。

传送方法/实例不是单例,可以根据发生的情况多次初始化/加载/构造。

不可能在运输方法类中挂钩任何函数 - 如果是这样,它将导致多个函数调用。期间。