避免在woocommerce_thankyou钩子中触发两次动作

时间:2016-12-22 13:46:55

标签: php wordpress woocommerce orders hook-woocommerce

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

1 个答案:

答案 0 :(得分:2)

为了避免这种重复,您可以在当前订单中添加自定义的后期元数据,一旦您的另一个"另一个"推荐已经第一次添加。

所以你的代码将是:

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

我希望这会有所帮助。