admin管理员组文章数量:1122846
I'm trying to make this functionality: I have .docx template where I need to pass variables and save it in theme directory when WooCommerce order is created. But the problem is probably, that I'm badly defining path to autoload.php file.
In my theme i have correctly prepared docx templates in: /themes/mytheme/word/vendor/phpoffice/phpword
And the autoload.php is in /theme/mytheme/word/vendor/autoload.php
My PHP function is in functions.php
of mytheme:
add_action('woocommerce_checkout_update_order_meta', function($order_id) {
global $wp, $wpdb, $woocommerce, $post;
require_once '/word/vendor/autoload.php';
$order = wc_get_order($order_id);
$formatted_billing_address = $order->get_formatted_billing_address();
$dateofpurchase = $order->get_date_created();
$result = $wpdb->get_row ( "SELECT order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = $order_id AND order_item_type = 'line_item'" );
$ordered_product = $result->order_item_name;
$fullname = $order->get_billing_first_name() . ' '. $order->get_billing_last_name();
$fullnamenospace = $order->get_billing_first_name().''.$order->get_billing_last_name();
$filename = $fullnamenospace.'.docx';
if($ordered_product == 'GOLD') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-gold.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == 'SILVER') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-silver.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == 'BRONZE') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-bronze.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == '3+KURZ') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-tiket3plus.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == '10+KURZ') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-tiket10plus.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
}
}, 999);
The order will be created with status: Waiting for Payment but in checkout I got error: Payment processing error. Please try again.
and not .docx will be saved in my theme directory. I think that problem is in autoload.php
path on row where I tried require once
but I've also tried full path and it does not worked.
Edit: I've figured it out and changed my require_once
call to:
require __DIR__ . '/word/vendor/autoload.php';
I'm trying to make this functionality: I have .docx template where I need to pass variables and save it in theme directory when WooCommerce order is created. But the problem is probably, that I'm badly defining path to autoload.php file.
In my theme i have correctly prepared docx templates in: /themes/mytheme/word/vendor/phpoffice/phpword
And the autoload.php is in /theme/mytheme/word/vendor/autoload.php
My PHP function is in functions.php
of mytheme:
add_action('woocommerce_checkout_update_order_meta', function($order_id) {
global $wp, $wpdb, $woocommerce, $post;
require_once '/word/vendor/autoload.php';
$order = wc_get_order($order_id);
$formatted_billing_address = $order->get_formatted_billing_address();
$dateofpurchase = $order->get_date_created();
$result = $wpdb->get_row ( "SELECT order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = $order_id AND order_item_type = 'line_item'" );
$ordered_product = $result->order_item_name;
$fullname = $order->get_billing_first_name() . ' '. $order->get_billing_last_name();
$fullnamenospace = $order->get_billing_first_name().''.$order->get_billing_last_name();
$filename = $fullnamenospace.'.docx';
if($ordered_product == 'GOLD') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-gold.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == 'SILVER') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-silver.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == 'BRONZE') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-bronze.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == '3+KURZ') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-tiket3plus.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
} elseif($ordered_product == '10+KURZ') {
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Potvrzeni-o-uzavreni-smlouvy-tiket10plus.docx');
$templateProcessor->setValue('nameorcompany', $fullname);
$templateProcessor->setValue('address', $formatted_billing_address);
$templateProcessor->setValue('dateofpurchase', $dateofpurchase);
$templateProcessor->saveAs($filename);
}
}, 999);
The order will be created with status: Waiting for Payment but in checkout I got error: Payment processing error. Please try again.
and not .docx will be saved in my theme directory. I think that problem is in autoload.php
path on row where I tried require once
but I've also tried full path and it does not worked.
Edit: I've figured it out and changed my require_once
call to:
require __DIR__ . '/word/vendor/autoload.php';
Share
Improve this question
edited Apr 1, 2024 at 20:13
Martin Orešanský
asked Mar 29, 2024 at 12:55
Martin OrešanskýMartin Orešanský
275 bronze badges
1 Answer
Reset to default 1require_once( '/word/vendor/autoload.php' );
will try to load starting from the root directory of your webserver. You're probably looking for something like require_once( './word/vendor/autoload.php' );
or perhaps require_once( plugin_dir_path( __FILE__ ) . 'word/vendor/autoload.php' );
.
(Note that plugin_dir_path()
can be used in themes as well as plugins; the plugin
part of its name is misleading.)
本文标签: WordPress How To create Word document from theme and run it on new order created
版权声明:本文标题:WordPress How To create Word document from theme and run it on new order created 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312068a1934967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论