【问题标题】:What Woocommerce function is called on PayPal IPN response?PayPal IPN 响应调用什么 Woocommerce 功能?
【发布时间】:2012-10-09 15:49:56
【问题描述】:

我无法确定当使用 Woocommerce 完成付款并且 PayPal 发送 IPN 时调用哪个函数。

正在接收 IPN,因为当我单击 Pay 时,PayPal 日志文件正在更新,但我无法弄清楚哪个函数正在写入该文件。

我需要弄清楚是否已经有一个内置功能可以在何时创建订单以及在哪里向管理员发送电子邮件。

如果确实存在,我也需要修改它以向其他人发送电子邮件,如果不存在,则我需要自己创建它,但我需要知道将代码放在哪里。

【问题讨论】:

    标签: wordpress paypal-ipn woocommerce


    【解决方案1】:

    检查文件/wp-content/plugins/woocommerce/classes/gateways/paypal/class-wc-paypal.php,我们看到函数check_ipn_response内部有一个动作钩子:

    if ($this->check_ipn_request_is_valid()) :
    
        header('HTTP/1.1 200 OK');
    
        do_action("valid-paypal-standard-ipn-request", $_POST);
    

    你可以像这样挂钩:

    add_action( 'valid-paypal-standard-ipn-request', 'so_12967331_ipn_response', 10, 1 );
    
    function so_12967331_ipn_response( $formdata )
    {
        // do your stuff
    }
    

    【讨论】:

    • 我不久前就想通了,如果我没记错的话,这与我所做的类似。感谢您的回复!
    【解决方案2】:

    基于@brasofilo 的回答,我必须为当前订单的每个产品做额外的工作。

    注意:我是(取消)序列化数据的新手,所以我不知道为什么我必须取消转义双引号才能使 unserialize() 工作。它抛出了一个错误,否则。也许有更好的方法来处理这个问题。

    function so_12967331_ipn_response( $formdata ) {
    
        if ( !empty( $formdata['invoice'] ) && !empty( $formdata['custom'] ) ) {
    
            if( $formdata['payment_status'] == 'Completed' ) {
    
                if( is_serialized( $posted['custom'] ) ) {
    
                    // backwards compatible
                    // unserialize data
                    $order_data = unserialize( str_replace('\"', '"', $posted['custom'] ) );
                    $order_id = $order_data[0];
    
                } else {
    
                    // custom data was changed to JSON at some point
                    $order_data = (array)json_decode( $posted['custom'] );
                    $order_id = $order_data['order_id'];
    
                }
    
                // get order
                $order = new WC_Order( $order_id );
    
                // got something to work with?
                if ( $order ) {
    
                    // get user id
                    $user_id = get_post_meta( $order_id, '_customer_user', true );
    
                    // get user data
                    $user = get_userdata( $user_id );
    
                    // get order items
                    $items = $order->get_items();
    
                    // loop thru each item
                    foreach( $items as $order_item_id => $item ) {
    
                        $product = new WC_Product( $item['product_id'] );
    
                        // do extra work...
    
                    }   
                }   
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-14
      • 2016-08-09
      • 2020-07-19
      • 2021-04-21
      • 2012-04-09
      • 2012-10-31
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多