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
Add a comment  | 

1 Answer 1

Reset to default 1

require_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