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