php - 支付完成后将数据写入sql数据库

标签 php sql wordpress paypal woocommerce

我正在尝试从 woocommerce 订单中获取信息,将其放入变量中,然后将其写入 SQL 数据库。我可以正确地检索和处理数据。如果我手动运行它,我什至设法将它写入数据库。

我似乎无法让 Action Hook 与脚本一起工作。

我遵循了我在这里看到的人们做类似事情的方法,但到目前为止它还没有奏效。我有 3 个主要问题:

  1. 我可以在函数内部定义订单id吗,

  2. 我是否需要在其他地方调用该函数,

  3. 此操作 Hook 是否正确设置为在处理付款后运行。

函数如下:

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));
        }
    }
}

最佳答案

此 Hook 仅适用于支付订单(在“COD”、“BACS”和“支票”支付方式之外)并且订单状态始终设置为“处理中”或“已完成”。但是如果您正在测试支付网关帐户上对此进行测试,如果收到的交易反馈参数丢失,订单将看起来已付款,但仍将处于“暂停”状态,因此不会触发 Hook 。

我已经用 Paypal 和其他信用卡支付网关测试了钩子(Hook),当交易被支付并且订单状态设置为“正在处理或”已完成时,它被正确触发。

现在为了避免这些问题,您可以使用 woocommerce_order_status_changed 来定位“处理中”/“已完成”订单状态。

此外,您应该使用专用的 wpdb class,而不是使用经典的 PHP SQL 连接。在 Wordpress/Woocommerce 代码中。

下面我使用 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 文件。

这应该能更好地工作......


关于php - 支付完成后将数据写入sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48031116/

相关文章:

php - 2 分钟后关闭 PHP 连接

php - 为什么我的查询在 phpmyadmin 中有效,但在我的 php 中无效?

wordpress - 如何在购物车总额中添加折扣?

php - 在 WordPress 中将类方法作为回调函数传递

php - Xdebug Profiler 无法在 apache 上工作

php - 如何使用 php 在网站上嵌入 youtube channel 视频

sql - 将连续的日期有效期间隔连接在一起

mysql - 基于动态日期的不同用户计数

php - Woocommerce:如何为首次订单添加额外费用

php - 未捕获的 TypeError : join(): Argument #2 ($array) must be of type ? 数组、给定的字符串/highcharts 的 PHP8 数据