gpt4 book ai didi

php - 订单完成时写入 SQL 表

转载 作者:行者123 更新时间:2023-11-29 18:01:25 27 4
gpt4 key购买 nike

我最近将我的 WordPress 网站迁移到托管服务器,但在使一项功能正常工作时遇到问题。当在我的本地服务器上测试它时,它工作得很好,但现在我似乎无法弄清楚为什么它不会写入我的 SQL 表。该功能如下,任何帮助将不胜感激。我还想知道是否有一种方法可以只在我收到 PayPal 付款时运行 SQL 查询,如果需要对此程序进行大量更改,这不是问题。

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("Y-m-d", strtotime("+$duration Months")),
'username' => get_post_meta( $order_id, 'My Field', true ),
'executed' => "false",
);

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

最佳答案

哦,您的订单状态如下,可以检查订单状态:

$statuses = array( 'processing', 'completed' ); 

但 woocommerce 订单状态如下:

'wc-pending','wc-processing', 'wc-on-hold' ,'wc-completed'  , 'wc-cancelled', 'wc-refunded' , 'wc-failed'

将数组更新为:

$statuses = array( 'wc-processing', 'wc-completed' ); 

关于php - 订单完成时写入 SQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com