【问题标题】:Adding data in a custom database table for paid orders in WooCommerce在 WooCommerce 中的已付款订单的自定义数据库表中添加数据
【发布时间】:2018-01-31 17:17:38
【问题描述】:

当用户购买产品时,我想将一系列数据保存在数据库的自定义表中。这些数据将是产品 ID、我拥有的自定义字段以及其他一些数据。

我的想法是,这应该在产品付款正确的时候进行,也就是说,在付款的时候。

我希望您给我建议,我已经创建了一种方法,但我不知道它是否正确,或者您是否会推荐任何其他方法。

我已经编辑了感谢页面,并插入了以下代码:

$order = new WC_Order ($order->get_id ();
$check_payment = $order->payment_complete ();

if ($check_payment) {
    global $wpdb;
    wpdb->insert (/* CODE DATABASE*/);
} 

【问题讨论】:

  • 使用 cheque 付款方式,您将使用 第二个功能(正如我在回答中提到的那样)......我有一个更新为您提供与处理和暂停状态相关的其他钩子……

标签: php sql wordpress woocommerce payment-gateway


【解决方案1】:

由于woocommerce订单收到(谢谢)页面可以重新加载,这并不是一个好方法。

你可以在WC_Orderpayment_complete()方法中找到正确的钩子是woocommerce_payment_complete。因此,您的代码应该适用于大多数支付网关:

add_action('woocommerce_payment_complete', 'action_payment_complete', 30, 1 );
function action_payment_complete( $order_id ){
    global $wpdb;

    // Get an instance of the WC_Order object (if needed)
    $order = wc_get_order( $order_id );

    // Your database actions code
    wpdb->insert (/* CODE DATABASE*/);
}

代码进入活动子主题(或活动主题)的 function.php 文件中。


对于需要“完成”的(CHEQUE HERE)'cheque'、'bacs' 和 'cod' 的付款方式由店长代替,您将使用:

add_action( 'woocommerce_order_status_completed', 'action_order_status_completed', 20, 2 );
function action_payment_complete( $order_id, $order ){
    // The specific payment methods to be target
    $payment_methods = array('bacs','cheque','cod');

    // Only for specific payment methods
    if( ! in_array( $order->get_payment_method(), $payment_methods ) return;

    global $wpdb;

    // Your database actions code
    wpdb->insert (/* CODE DATABASE*/);
}

因此,当这种特定付款方式的订单状态更改为已完成时,将触发此钩子......

如果您以 Processing 订单状态为目标,您也可以使用 woocommerce_order_status_processing 代替;如果您以 On Hold 为目标,您也可以使用 woocommerce_order_status_on-hold 订单状态

代码进入活动子主题(或活动主题)的 function.php 文件中。

这应该可行。

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2016-06-11
    • 2021-12-19
    • 2021-06-01
    • 2016-10-12
    • 2021-02-22
    相关资源
    最近更新 更多