【问题标题】:Woocommerce - Copy New Order Details to New TableWoocommerce - 将新订单详细信息复制到新表
【发布时间】:2014-11-10 12:30:19
【问题描述】:

我之前在这里的某个地方找到了以下脚本,但无法使其正常工作。基本上我想将基本订单详细信息复制到一个新表中(在同一个数据库中),所以我可以从非 wordpress 页面运行一些报告。我已经更改了表名和字段,并将其复制到主题中的 functions.php 中。但它似乎没有做任何事情。任何帮助都会很棒,我真的卡住了。如果这有什么不同,我正在运行 woocommerce 2.2.8。

function add_neworders ($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$total = $order->order_total;
$date = $order->order_date;

global $wpdb;
$table =  $wpdb->prefix . 'finacedata';

$wpdb->insert($table, array( 

'desctipion' => $name, 
'status' => '2',
'date' => $date,
'amount' => $total,
'invoice' => $order_id,
));

add_action( 'woocommerce_new_order', 'add_neworders' );}

【问题讨论】:

    标签: php mysql wordpress woocommerce


    【解决方案1】:

    这可能是一个迟到的答案,但woocommerce_new_order 操作在创建订单时运行,但尚未填充数据,所以一切都是空的。

    请改用woocommerce_checkout_order_processed 操作,该操作在创建订单并存储所有订单数据(如订单项)时运行。

    【讨论】:

      【解决方案2】:

      您不能将 add_action 添加到函数中。放在函数之外:

      function add_neworders ($order_id) {
          global $woocommerce;
          $order = new WC_Order( $order_id );
          $total = $order->order_total;
          $date = $order->order_date;
      
          global $wpdb;
          $table =  $wpdb->prefix . 'finacedata';
      
          $wpdb->insert($table, array(
      
          'desctipion' => $name,
          'status' => '2',
          'date' => $date,
          'amount' => $total,
          'invoice' => $order_id,
          ));
      }
      add_action( 'woocommerce_new_order', 'add_neworders' );
      

      【讨论】:

      • 谢谢...由于某种原因,创建新订单时它仍然没有做任何事情。无论如何都要看看它是否会产生错误?
      • 你可以打印 $order_id;exit;因为它会起作用。 $name 没有定义。所以检查一下
      • 嗨,这似乎仍然没有打印任何内容。不确定是否正在调用 woocommerce_new_order..
      猜你喜欢
      • 2014-08-16
      • 2017-01-17
      • 2023-01-22
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2019-03-17
      • 2015-01-28
      相关资源
      最近更新 更多