将带有按钮的订单项目部分添加到 WooCommerce 我的帐户单个订单页面

问题描述 投票:0回答:1

在 WooCommerce 中,当客户购买一种或多种产品时,他可以在单个订单页面的“我的帐户”部分中看到:

我想在单个订单页面上的“我的帐户”部分中添加一个列出当前订单的所有订单项目(购买的产品)的部分,并为每个订单项目添加一个按钮,因此用户默认情况下将看到以下内容:

我使用此代码来显示一个按钮,我希望当他单击他购买的每个产品的按钮时检查以下条件:

  1. 首先,检查该产品是否是由点击(下载该产品发票)按钮的同一用户购买的?

  2. 如果购买了产品,则应生成一个文本文件,以及买家单击相应按钮(下载此产品发票)的同一产品的名称,并将买家的电子邮件地址放入该文件中,然后下载该文件(文本文件如下)

  3. 应在购买的每个产品前面或为每个产品创建一个按钮(下载此产品发票)

您好,亲爱的用户,感谢您购买(虚拟产品1)。你可以 通过(买家的电子邮件)收到您的发票。

您好,亲爱的用户,感谢您购买(虚拟产品2)。你可以 通过(买家的电子邮件)收到您的发票。

还有...

我自己使用了以下命令将按钮添加到 Function.php 文件中,但我想应用上述更改

add_action('woocommerce_order_details_after_order_table', 'add_button');

function add_button($order) {
    /* Your code */
   // echo "Your button html code";
    echo '<form method="post">';
    echo '<input type="submit" name="btn-added" id="btn-added" value="Download this product invoice" /><br/>';
    echo '</form>';
}


function sample_func() {
    // Get Current User Email Address
    $current_user = wp_get_current_user();
    $current_user_email = $current_user->user_email;
    echo $current_user_email;
    
    // Get Order Id
    $order_id_sample = wc_get_order( $order_id );
    echo $order_id_sample;   
}

if(array_key_exists('btn-added',$_POST)){
   sample_func();
}
php wordpress woocommerce hook-woocommerce account
1个回答
3
投票

您需要将函数挂钩到

woocommerce_order_details_after_order_table
操作挂钩中。

请注意,您无需“检查此产品是否由点击(下载此产品发票)的同一用户购买”,因为“我的帐户”部分仅向登录的客户显示其订单和个人数据.

以下功能代码将显示在每个客户的我的帐户单个订单中,附加部分列出了所有订单项目(产品名称)以及您的自定义下载按钮:

add_action('woocommerce_order_details_after_order_table', 'section_order_items_button_invoice');

function section_order_items_button_invoice( $order ) {
    if ( ! is_account_page() ) return; // Only on customer my account section
    if ( $order->has_status('pending') ) return; // Not for "pending" orders

    $user_id   = $order->get_user_id(); // Get the customer ID
    $user      = $order->get_user(); // Get the WP_User object
   ?> 
    <section class="woocommerce-order-details product-invoices">
        <h2 class="woocommerce-order-details__title"><?php esc_html_e( 'Product Order details', 'woocommerce' ); ?></h2>
        <!-- form start -->
        <form method="post" name="<?php echo 'download-' . $user_id; ?>">
            <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                <thead>
                    <tr>
                        <th class="woocommerce-table__product-name product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
                        <th class="woocommerce-table__product-table product-total"><?php esc_html_e( 'Invoice download', 'woocommerce' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    // Loop through order items
                    foreach ( $order->get_items() as $item_id => $item ) :
                        $product            = $item->get_product();
                        $product_id         = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
                    ?>
                    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'woocommerce-table__line-item order_item', $item, $order ) ); ?>">

                        <td class="woocommerce-table__product-name product-name"><?php echo wp_kses_post( $item->get_name() );?></td>

                        <td class="woocommerce-table__product-total product-total"><?php printf( 
                            '<input type="submit" name="%s" value="%s" /><br/>',
                            'product_item-' . $item_id . '-' . $product_id, // Unique imput name with the item ID and the product ID
                            esc_html__(  'Download this product invoice', 'woocommerce')
                        ); ?></td>

                    </tr>
                    <?php endforeach; ?>
                </tbody>
                </thead>
            </table>
        </form>
        <!-- form end -->
    </section>
    <?php
}

您必须自己处理每个订单项目发票的下载功能以及您要求的所有其他内容(因为这太宽泛且复杂,无法在堆栈溢出中处理)。

请注意,

<form>
元素只需要在页面中实现once,并具有唯一的
name
属性。此外,对于每个
<input type="submit" name="">
,每个
name
属性都需要是唯一的。

在主题的functions.php文件中进行测试并有效,显示以下内容:

© www.soinside.com 2019 - 2024. All rights reserved.