2017-12-30 31 views
1

woocommerceの注文から情報を取り出し、変数に入れてSQLデータベースに書き込もうとしています。データを適切に取得して処理することができます。私はそれを手動で実行するとデータベースに書き込むことさえできました。支払い時にSQLデータベースにデータを書き込む

私はまだスクリプトで動作するアクションフックを得ることができないようです。

私はここで似たようなことをする人々のために私が見たことに従っていますが、それまではそれは働いていません。私は主に3つの質問があります。

  1. は私が

  2. が、私はどこか別の関数を呼び出す必要がありますか、関数の内部で注文IDを定義することができますが、

  3. が適切に設定されたこのアクションフックです支払いが処理された後に実行されます。

機能は以下の通りです:

add_action('woocommerce_payment_complete', 'returnAttributesFromCheckout'); 
function returnAttributesFromCheckout ($order_id) { 

    $connection = mysqli_connect('localhost', 'root', '', 'apptest'); 

    if(!$connection) { 

     die('ded' . mysqli_error($connection)); 
    } 

    global $woocommerce, $post; 

    $order = new WC_Order($order_id); 

    foreach ($order->get_items() as $item_id => $item_data) { 

     // Get an instance of corresponding the WC_Product object 
     $product = $item_data->get_product(); 

     $rank = $product->get_attribute('pa_rank'); 
     $money = $product->get_attribute('pa_money'); 
     $spawner = $product->get_attribute('pa_spawner'); 
     $permission = $product->get_attribute('pa_permission'); 
     $kit = $product->get_attribute('pa_kit'); 
     $crate = $product->get_attribute('pa_crate'); 
     $tag = $product->get_attribute('pa_stag'); 
     $duration = $product->get_attribute('pa_duration'); 
     $d = strtotime("+ $duration Months"); 
     $endDate = date("d/m/Y", $d); 

     $query = "INSERT INTO checkout(rank, money) "; 
     $query .= "VALUES('$rank', '$money')"; 

     $result = mysqli_query($connection, $query); 

     if(!$result) { 

      die('its dead jim' . mysqli_error($connection)); 
     } 
    } 
} 
+0

はどのように正確に私はそれが正しく付けられていることを確認します – Strawberry

答えて

1

このフックは唯一は(外「代金引換」、「BACS」と支払方法「チェック」)と注文状況が常に設定されている有料の注文に適用されます「処理中」または「完了」ですが、テスト支払いゲートウェイアカウントでこれをテストしていて、受信したトランザクションフィードバックパラメータがない場合、注文は有料のように見えますが、「保留中」の状態になりますフックは発砲されません。

私は、Paypalと別のクレジットカード決済ゲートウェイでフックをテストしました。トランザクションが支払われ、注文ステータスが「処理中」または「完了」に設定されていると正しく起動します。

今問題を回避するには、代わりにwoocommerce_order_status_changedを使用して、「処理中」/「完了済み」の注文状況をターゲットにすることができます。

また、古典的なPHP SQL接続を使用する代わりに、Wordpress/Woocommerceコードで専用のwpdb classを使用する必要があります。

以下は、wpdb classを使用してコードを再訪しました。ここでは、コードは次のとおりです。

add_action('woocommerce_order_status_changed', 'checkout_custom_table_insert', 20, 4); 
function checkout_custom_table_insert($order_id, $old_status, $new_status, $order){ 

    // Only for 'processing' and 'completed' order status changes 
    $statuses = array('processing', 'completed'); 
    if (! in_array($new_status, $statuses)) return; 

    // Check if data has been already updated (avoid repetitions) 
    $is_done = get_post_meta($order_id, '_checkout_table_updated', true); 
    if(! empty($is_done)) return; // We exit if it has been already done 

    global $wpdb; 

    // Loop through order items 
    foreach ($order->get_items() as $item){ 
     $product = $item->get_product(); // Get the variation product object 

     // Choose in the array which data you want to insert (each line is a table column) 
     $args = array(
      'rank'   => $product->get_attribute('pa_rank'), 
      'money'   => $product->get_attribute('pa_money'), 
      'spawner'  => $product->get_attribute('pa_spawner'), 
      'permission' => $product->get_attribute('pa_permission'), 
      'kit'   => $product->get_attribute('pa_kit'), 
      'crate'   => $product->get_attribute('pa_crate'), 
      'stag'   => $product->get_attribute('pa_stag'), 
      'duration'  => $product->get_attribute('pa_duration'), 
      'end_date'  => date("d/m/Y", strtotime("+ $duration Months")), 
     ); 

     // The SQL INSERT query 
     $table = "checkout" // or "{$wpdb->prefix}checkout"; 
     $wpdb->insert( $table, $args); // Insert the data 
    } 

    // Mark this task as done for this order 
    update_post_meta($order_id, '_checkout_table_updated', '1'); 
} 

コードはfunction.phpのあなたのアクティブな子テーマのファイル(またはアクティブテーマ)になります。

これは、より良い動作するはず...


+0

parametrisedクエリについてを参照してください?私はあなたが何を指しているかは完全にはわかりません。私の元のプログラムのリンク先のデータベースは実際にはデータベースとは別にウェブサイトの作成時に設定されていますが、違いがあるかどうかわかりません – Bman76

+0

もう1つのことは、それ自体で渡されるか、それともその値を渡す必要がありますか? – Bman76

+0

私は注文を受け取ることに問題があります。私はPayPalサンドボックスを使用しています。注文が受け付けられたときには、それは処理中または完了ステータスに移行しません。私が手動で行うと、イベントはまだ起きますか? – Bman76

関連する問題