Woocommerce hook - 完成状态挂钩

时间:2017-05-21 02:32:23

标签: woocommerce hook

我试图调整一段代码,但到目前为止,我唯一能做的就是打破woocommerce的工作流程。

所以,我有一个代码,当他在我的网站上完成购买时,应该将用户发送到我的Sendy列表之一。然后我发现每当有人结束我的结账过程时,这个人就会被添加到列表中,无论他是否还在等待付款"状态。

这是我认为重要的一段代码:

    function __construct() {
    /**
     * Check if WooCommerce is active
     * */
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        add_action( 'woocommerce_review_order_before_submit', array( $this, 'add_optin_checkbox' ), 99 );
        add_action( 'woocommerce_checkout_order_processed', array( $this, 'get_lists_after_checkout' ), 10, 2 );
    }

}
function get_lists_after_checkout( $order_id, $posted ) {
        $settings=get_option( 'woocommerce_sendy_settings' );
        $lists =array();
        $email = $posted['billing_email'];
        $name=$posted['billing_first_name'].' '.$posted['billing_last_name'];

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {

        $product_id = $item['product_id'];
        $list_ids = get_post_meta( $product_id, 'sendy_product_lists', true );
        if ( !empty( $list_ids ) ) {
            $list_array = explode( ',', $list_ids );
            $lists = array_merge( $lists, $list_array );
        }
    }
    if ( !empty( $lists ) ) {
        $lists = array_unique( $lists );
        $this->add_user_to_sendy( $name, $email, $lists );
    }

}


function add_user_to_sendy( $name, $email, $listids, $custom_fields =array( '' ) ) {

if ( ! empty( $email ) ) {
    $settings=get_option( 'woocommerce_sendy_settings' );


    $sendy_url=rtrim( $settings['url'], '/' ).'/subscribe';

    $body= array( 'name' => $name, 'email' => $email, 'boolean' => 'true' );

    if ( !empty( $custom_fields ) ) {
        foreach ( $custom_fields as $key => $value ) {
            $body[$key]= $value;
        }
    }

    foreach ( $listids as $list_id ) {
        $body['list'] = $list_id;
        $response= wp_remote_post( $sendy_url, array( 'body' => $body ) );
    }

    if ( is_wp_error( $response ) ) {

        //return $error_message = $response->get_error_message();

    } else {

    //  return $response['body'];
    }

所以,我猜我的问题是挂钩woocommerce_checkout_order_processed。然后我尝试将其更改为稍后在结帐流程中的某个挂钩,如woocommerce_payment_complete_order_status,但不仅仅是列表未被提供,因为显然订单在收到付款后停止完成。

所以,我猜测我做了一些愚蠢的事情,如果它对于这个论坛来说太棒了,我道歉。任何帮助或指导都将非常感激。

BR,

0 个答案:

没有答案
相关问题