【问题标题】:WooCommerce - Add information to "thank you" page by function hooked in shipping methodWooCommerce - 通过与运输方式挂钩的功能将信息添加到“谢谢”页面
【发布时间】:2021-09-12 11:47:24
【问题描述】:

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

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

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

【问题讨论】:

  • $this-&gt;description 到底是什么?因为一旦你弄清楚如何在课堂之外得到它,你的问题就解决了。您不必在运输类别中致电woocommerce_thankyou。 @Marek
  • 是的,我的类代码示例中省略了该属性(以及更多)。问题是我想在我的运输类中调用那个钩子:-)我不想在其他地方维护它(functions.php,...)。
  • 我认为这是一个错误的添加位置。如果您可以分享描述的目的,我们实际上可以研究如何实现它的解决方案。
  • 这只是带有客户信息的简单文本(我知道“运输实例”更复杂)。我只是不明白为什么不能在运输方法类中处理它。问题出在哪里...
  • 你为什么要扩展运输方式来挂钩thank_you 页面?只需在你的信息中加上谢谢你。

标签: php wordpress woocommerce shipping-method


【解决方案1】:

我认为应该这样做。

  • 您可以创建自定义运输方式。
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 不同。

  • 像这样在课堂外使用thankyou钩子来获取描述。
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

【讨论】:

  • 我不认为这可以解决我的问题,而且我认为在thankyou 钩子中初始化new WC_Your_Shipping_Method() 是不正确的。毕竟,我的运输方式支持实例,任何一个都可以有不同的描述。
  • 好吧,如果description 是动态的,请告诉我们它是如何动态的?另外,我不确定结帐完成后您是否可以访问该实例。还有另一种方法,您可以使用它来完成这项工作,它通过使用订单元来存储值,但您需要告诉用户description 是如何动态的。 @Marek
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 2018-11-05
    • 2019-01-04
    • 2021-10-16
    • 2020-11-03
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多