Php 避免在woocommerce\u Thankyu钩子中触发两次操作

Php 避免在woocommerce\u Thankyu钩子中触发两次操作,php,wordpress,woocommerce,orders,hook-woocommerce,Php,Wordpress,Woocommerce,Orders,Hook Woocommerce,我正在使用下面的woocommerce操作来调用自定义函数,但由于某些原因,它会在每个订单上触发两次。有没有人知道为什么会这样,或者如何修复它,使它每次订单只调用一次 add_action( 'woocommerce_thankyou', 'parent_referral_for_all', 10, 1 ); function parent_referral_for_all( $order_id ) { .... } 更新 我以为这个动作被触发了两次,但我现在不太确定。我用这个动作在a

我正在使用下面的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 );

为了避免这种重复,您可以在第一次添加“另一个”推荐后,向当前订单添加自定义的post元数据

因此,您的代码将是:

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

我希望这会有所帮助。

谢谢,我看到了。我尝试在过滤器中使用订单状态,但没有成功。是否有不涉及修改Woocomerce模板的修复程序?您的主题文件夹中是否有woocomnerce?是的。我修改了thankyou.php并注释掉了dou操作('woocommerce\u thankyou',$order->id);但它仍然发射了两次…你确定你不会叫那个钩子两次吗?这是一个有趣的工作!我试试看,然后回来汇报。非常感谢。工作得很漂亮!再次感谢!!