简体   繁体   English

Woocommerce更新订单钩子“ save_post”不允许保存文件

[英]Woocommerce update order hook “save_post” not allowing file saves

So I find this strange but maybe someone can help. 所以我觉得这很奇怪,但也许有人可以帮忙。
I need to save an incoming file from a remote server when the admin of the wordpress website updates the order in the backend. 当wordpress网站的管理员更新后端的订单时,我需要保存来自远程服务器的传入文件。
The hook I'm using is 我正在使用的钩子是

add_action( 'save_post', 'courier_save_wc_order_fields', 10, 2 );

Now everything works fine and I can update the post_meta as I need. 现在一切正常,我可以根据需要更新post_meta。
When it comes to saving an incoming PDF file things go south. 当要保存传入的PDF文件时,事情就变了。
inside this perticular hook (only one I have ever found) I cannot save files. 在此垂直钩子内(我只发现过一个),我无法保存文件。
Is this because this is when woocommerce is about to save to DB? 这是因为这是woocommerce即将保存到DB的时候吗?

I've even stripped out all other code and left it as 我什至删除了所有其他代码并将其保留为

add_action( 'save_post', 'courier_save_wc_order_fields', 10, 2 );

function courier_save_wc_order_fields( $post_id ) {
    file_put_contents('incommingdata.txt', print_r("This is random text",true));
}

Again, this is the only place this happens.. Is there a better hook that I won't have this issue? 同样,这是唯一发生此情况的地方。是否有一个更好的钩子,使我不会遇到此问题?

I need one that checks a meta_box value in the Admin order page and updates the order meta accordingly while communicating to a remote server to save and retrieve a file.... Everything works except for this file saving. 我需要一个程序来检查“管理订单”页面中的meta_box值,并在与远程服务器进行通信以保存和检索文件时相应地更新订单元。

Side note: 边注:

$path="my_file_received.pdf"
$decodedString = base64_decode($base64string); 
$fileHandle = fopen($path, "w"); 
if ($fileHandle) { 
    fwrite($fileHandle, $decodedString); 
    fclose($fileHandle); 
    return 1; 
}

Does not show an error yet the file does not exist either. 不显示错误,但文件也不存在。

So I found the answer to my issue... 所以我找到了问题的答案...

The save_post hook has a different URL than other hooks that I have used.. save_post挂钩与我使用的其他挂钩具有不同的URL。

So when I normally used 所以当我平常使用
file_put_contents('incommingdata.txt', print_r("This is random text",true));
I would always get the file saved within my plugin folder or theme folder or the base url. 我总是将文件保存在插件文件夹或主题文件夹或基本URL中。

The url for this hook is the wp-admin folder and not the base domain or plugin folder. 该挂钩的url是wp-admin文件夹,而不是基本域或插件文件夹。

to fix this and make sure I save in the right folder the solution is: 要解决此问题,并确保将其保存在正确的文件夹中,解决方案是:

 $this_dir_of_my_file = dirname(__FILE__);
 $this_dir_of_my_file = $this_dir_of_my_file . "/Example.txt";
 file_put_contents($this_dir_of_mine, print_r("This is random text in my file.",true));

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

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