繁体   English   中英

插件中 woocommerce 上的新订单没有挂钩

[英]No hook for new order on woocommerce in a plugin

我尝试了大约 5 个钩子来获得完成的订单钩子,并且 function 根本没有运行,但 woocommerce_add_to_cart 例如正在工作!

1. woocommerce_order_status_changed

2. woocommerce_new_order

我只是提醒知道 function 是否运行,但功能非常大,我在 index.ZE1BFD762321E496CEEZAC48 中的插件中部署此index.php中的插件之前进行了命令manullay作为测试

    function SP_order_token($order_id)
    {
    
    
    ?>
        <script>
            alert("hello");
            alert('<?php echo $order_id; ?>');
        </script>
        <?php
        echo "hello";
    
        global $woocommerce, $post;
  
      
        echo $order_id;
    
        $order =    wc_get_order($order_id);
    
        $order_data = $order->get_data(); // The Order data
    
        var_dump($order_data);
     }
// the final hook is when an order successfully paid
        add_action('woocommerce_order_status_changed', 'SP_order_token',10,1);

请用这个替换并再次检查。

function woo_order_status_change_custom_check($order_id) {

    ?>
    <script>
        alert("hello");
        alert('<?php echo $order_id; ?>');
    </script>
    <?php
    
    // echo "hello";

    $order = new WC_Order( $order_id );
    $orderstatus = $order->status;

    var_dump($orderstatus);

}

add_action('woocommerce_order_status_changed', 'woo_order_status_change_custom_check', 10, 1);

您调试 php 的方法是错误的。 你不能在服务器端提醒或做 JS 的事情。 此外, var_dumpecho可以工作,但你不知道他们会在哪里回显或转储 output。

php 调试的正确方法是将 output 写入外部文件或错误日志中。 但我更喜欢在 wordpress 的根目录中写入文件。

这是您可以使用的代码片段:

function sp_order_token( $order_id ) {
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data();

    /**
     * We're using file_put_contents to create a debug.txt file in the root of wordpress where your wp-config.php exists.
     * 
     * we're also passing true in print_r so that print_r returns the output instead of printing it during code execution.
     * 
     * ABSPATH is a constant of wordpress root defined by core WordPress.
     */
    file_put_contents( ABSPATH . 'debug.txt', print_r($order_data, true ) );
}
add_action( 'woocommerce_order_status_changed', 'sp_order_token', 10, 1 );

一旦您的操作将在 wordpress 根目录中运行,您将找到带有预期debug.txt的 debug.txt。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM