gpt4 book ai didi

php - 避免在 woocommerce_thankyou hook 中触发两次操作

转载 作者:行者123 更新时间:2023-12-05 01:45:34 28 4
gpt4 key购买 nike

我正在使用下面的 woocommerce 操作来调用自定义函数,但由于某种原因,它在每个订单上被触发了两次。有谁知道为什么会这样或如何解决它以便每个订单只调用一次?

add_action( 'woocommerce_thankyou', 'parent_referral_for_all', 10, 1 );

function parent_referral_for_all( $order_id ) {
....
}

更新

我以为这个 Action 被触发了两次,但我现在不太确定。我正在使用此操作在 affiliatewp 插件中添加另一个推荐,该插件添加了两次,但我的“谢谢” echo 只出现了一次。

除推荐(及其关联的订单备注)被添加两次外,一切都按预期进行。

如有任何帮助,我们将不胜感激。

完整的功能:

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

最佳答案

为避免这种重复,您可以在第一次添加“另一个”推荐人后,将自定义帖子元数据添加到当前订单。

所以你的代码将是:

function parent_referral_for_all( $order_id ) {

## HERE goes the condition to avoid the repetition
$referral_done = get_post_meta( $order_id, '_referral_done', true );
if( empty($referral_done) ) {

//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 ) );

## HERE you Create/update your custom post meta data to avoid repetition
update_post_meta( $order_id, '_referral_done', 'yes' )
}
}
}
add_action( 'woocommerce_thankyou', 'parent_referral_for_all', 10, 1 );

希望对您有所帮助。

关于php - 避免在 woocommerce_thankyou hook 中触发两次操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284646/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com