【问题标题】:Avoiding action Being Triggered Twice in woocommerce_thankyou hook避免在 woocommerce_thankyou 钩子中触发两次操作
【发布时间】: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


【解决方案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 );

我希望这会有所帮助。

【讨论】:

  • 这是一项有趣的工作!我会尝试并报告。谢谢!!
  • 干得漂亮!再次感谢!!
  • 为我工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
相关资源
最近更新 更多