0

誰でもこのコードで私を助けることができますか? 一時は動作していましたが、WooCommerce の変更により動作しなくなりました。

コードは注文完了時に実行され、購入によって獲得したクレジット数に基づいてユーザーのメタ フィールドを更新する必要があります。

ありがとう

 function custom_update_user_credits( $order_id = NULL ) {
    global $woocommerce;


    // Set product ids and credits

    $product_id1 = 80;
    $ad_credits1 = 10;

    $product_id2 = 82;
    $ad_credits2 = 20;

    $product_id3 = 83;
    $ad_credits3 = 30;


    $customer_id = get_post_meta( $order_id, '_customer_user', TRUE );
    $user_credit = (int) get_user_meta( $customer_id, 'wpcf-ad_credits', TRUE );

    if ( $order_id && 0 != $customer_id ) :
        $order = &new WC_Order( $order_id );


        // Get the order items

        $items = get_product( $post->ID );

        // Loop through the order items looking for the right products
        foreach($items as $item) :


            // Credits

            if ( $item['id'] == $product_id1 ) :
            $credit_total = $user_credit + $ad_credits1;

            elseif ( $item['id'] == $product_id2 ) :
            $credit_total = $user_credit + $ad_credits2;

            elseif ( $item['id'] == $product_id3 ) :
            $credit_total = $user_credit + $ad_credits3;

            update_user_meta( $customer_id, 'wpcf-ad_credits', $credit_total, $user_credit );                       

            endif;

        endforeach;

    endif;

}

add_action( 'woocommerce_order_status_completed', 'custom_update_user_credits' );
4

1 に答える 1