【发布时间】:2016-12-22 13:46:54
【问题描述】:
我正在使用下面的 woocommerce 操作来调用自定义函数,但由于某种原因,每个订单都会触发两次。有谁知道为什么会这样或如何解决它,以便每个订单只调用一次?
add_action( 'woocommerce_thankyou', 'parent_referral_for_all', 10, 1 );
function parent_referral_for_all( $order_id ) {
....
}
更新
我认为该操作被触发了两次,但我现在不太确定。我正在使用此操作在affiliatewp 插件中添加另一个推荐,该插件添加了两次,但我的“谢谢”回声只出现了一次。
除了推荐(及其相关的订单注释)被添加两次之外,一切都按预期工作。
任何帮助将不胜感激。
完整的功能:
function parent_referral_for_all( $order_id ) {
//Direct referral
$existing = affiliate_wp()->referrals->get_by( 'reference', $order_id );
$affiliate_id = $existing->affiliate_id;
//Total amount
if( ! empty( $existing->products ) ) {
$productsarr = maybe_unserialize( maybe_unserialize( $existing->products ) );
foreach( $productsarr as $productarr ) {
$bigamount = $productarr['price'];
}
}
//Parent amount
$parentamount = $bigamount * .1;
$affiliate_id = $existing->affiliate_id;
$user_info = get_userdata( affwp_get_affiliate_user_id( $existing->affiliate_id ) );
$parentprovider = $user_info->referral;
//Affiliate id by username
$userparent = get_user_by('login',$parentprovider);
$thisid = affwp_get_affiliate_id($userparent->ID);
$args = array(
'amount' => $parentamount,
'reference' => $order_id,
'description' => $existing->description,
'campaign' => $existing->campaign,
'affiliate_id' => $thisid,
'visit_id' => $existing->visit_id,
'products' => $existing->products,
'status' => 'unpaid',
'context' => $existing->context
);
$referral_id2 = affiliate_wp()->referrals->add( $args );
echo "Thank you!";
if($referral_id2){
//Add the order note
$order = apply_filters( 'affwp_get_woocommerce_order', new WC_Order( $order_id ) );
$order->add_order_note( sprintf( __( 'Referral #%d for %s recorded for %s', 'affiliate-wp' ), $referral_id2, $parentamount, $parentamount ) );
}
}
add_action( 'woocommerce_thankyou', 'parent_referral_for_all', 10, 1 );
【问题讨论】:
-
谢谢,我看到了。我尝试使用过滤器中的订单状态但不起作用。是否有不涉及修改 woocoomerce 模板的修复?
-
你在你的主题文件夹中 woocomnerce 吗?
-
是的。我修改了thankyou.php并注释掉了do_action('woocommerce_thankyou', $order->id);但它仍在发射两次......
-
你确定你没有调用那个钩子两次吗?
标签: php wordpress woocommerce orders hook-woocommerce