Add a section of order items with buttons to WooCommerce My Account single order pages.

huangapple go评论79阅读模式
英文:

Add a section of order items with buttons to WooCommerce My Account single order pages

问题

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

> Add a section of order items with buttons to WooCommerce My Account single order pages.

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

> Add a section of order items with buttons to WooCommerce My Account single order pages.

我正在使用以下代码显示一个按钮,当用户点击购买的每个产品的按钮时,我希望检查以下条件:

  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="下载此产品发票" /><br/>';
    echo '</form>';
}

function sample_func() {
    // 获取当前用户的电子邮件地址
    $current_user = wp_get_current_user();
    $current_user_email = $current_user->user_email;
    echo $current_user_email;
    
    // 获取订单ID
    $order_id_sample = wc_get_order( $order_id );
    echo $order_id_sample;   
}

if(array_key_exists('btn-added',$_POST)){
   sample_func();
}
英文:

In WooCommerce, when customers purchase one or more products, he can see in his My Account section on single order pages:

> Add a section of order items with buttons to WooCommerce My Account single order pages.

I would like in this My Account section on single order pages to add a section listing all order items (purchased products) for the current order with a button for each order item, so the user will see, by default, the following:

> Add a section of order items with buttons to WooCommerce My Account single order pages.

I'm using this code to display a button and I want the following conditions to be checked when he clicks on the button for each product he bought:

  1. First, check if this product was purchased by the same user who clicked on the (Download this product invoice) button?

  2. If the product was purchased, a text file should be generated and the name of the same product that the buyer clicks on the corresponding button (Download this product invoice) and puts the buyer's email address in that file and then the file is downloaded (text file like be below)

  3. A button (Download this product invoice) should be created in front of or for each product that was purchased

> Hello, dear user Thank you for purchasing (virtual product 1). You can
> receive your invoice by (buyer's email).

or

> Hello, dear user Thank you for purchasing (virtual product 2). You can
> receive your invoice by (buyer's email).

and...

I myself have used the following commands to add the button to the Function.php file, but I want to apply the above changes

add_action(&#39;woocommerce_order_details_after_order_table&#39;, &#39;add_button&#39;);

function add_button($order) {
    /* Your code */
   // echo &quot;Your button html code&quot;;
    echo &#39;&lt;form method=&quot;post&quot;&gt;&#39;;
    echo &#39;&lt;input type=&quot;submit&quot; name=&quot;btn-added&quot; id=&quot;btn-added&quot; value=&quot;Download this product invoice&quot; /&gt;&lt;br/&gt;&#39;;
    echo &#39;&lt;/form&gt;&#39;;
}


function sample_func() {
    // Get Current User Email Address
    $current_user = wp_get_current_user();
    $current_user_email = $current_user-&gt;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(&#39;btn-added&#39;,$_POST)){
   sample_func();
}

答案1

得分: 5

需要将您的函数挂钩到woocommerce_order_details_after_order_table动作挂钩中,而不是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; // 仅在客户的“我的帐户”部分显示

    // 仅适用于“已完成”和“处理中”的订单
    if ( $order->has_status( array('completed', 'processing') ) ) :

    $user_id   = $order->get_user_id(); // 获取客户ID
    $user      = $order->get_user(); // 获取WP_User对象
   ?> 
    <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
                    // 循环订单项目
                    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, // 带有项目ID和产品ID的唯一输入名称
                            esc_html__( 'Download this product invoice', 'woocommerce')
                        ); ?></td>

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

您将需要自己处理每个订单项目发票的下载功能以及您提出的其他所有内容(因为这太广泛和复杂,无法在Stack Overflow中处理)。

请注意,<form>元素只需在页面中实现一次,带有唯一的name属性。另外,每个<input type="submit" name="">name属性都需要是唯一的。

在主题的functions.php文件中进行测试并运行,会显示如下内容:

Add a section of order items with buttons to WooCommerce My Account single order pages.

英文:

You need to hook your function in woocommerce_order_details_after_order_table action hook instead.

Note that you don't need to "check if this product was purchased by the same user who clicked on the (Download this product invoice)" as My Account section is only displayed to Logged in Customers with their orders and personal data.

The following function code will display in each customer My account single orders, an additional section with all order items listed (products names) with your custom download button:

add_action(&#39;woocommerce_order_details_after_order_table&#39;, &#39;section_order_items_button_invoice&#39;);
function section_order_items_button_invoice( $order ) {
if ( ! is_account_page() ) return; // Only on customer my account section
// Only for &#39;completed&#39; and &#39;processing&#39; orders
if ( $order-&gt;has_status( array(&#39;completed&#39;, &#39;processing&#39;) ) ) :
$user_id   = $order-&gt;get_user_id(); // Get the customer ID
$user      = $order-&gt;get_user(); // Get the WP_User object
?&gt; 
&lt;section class=&quot;woocommerce-order-details product-invoices&quot;&gt;
&lt;h2 class=&quot;woocommerce-order-details__title&quot;&gt;&lt;?php esc_html_e( &#39;Product Order details&#39;, &#39;woocommerce&#39; ); ?&gt;&lt;/h2&gt;
&lt;!-- form start --&gt;
&lt;form method=&quot;post&quot; name=&quot;&lt;?php echo &#39;download-&#39; . $user_id; ?&gt;&quot;&gt;
&lt;table class=&quot;woocommerce-table woocommerce-table--order-details shop_table order_details&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class=&quot;woocommerce-table__product-name product-name&quot;&gt;&lt;?php esc_html_e( &#39;Product&#39;, &#39;woocommerce&#39; ); ?&gt;&lt;/th&gt;
&lt;th class=&quot;woocommerce-table__product-table product-total&quot;&gt;&lt;?php esc_html_e( &#39;Invoice download&#39;, &#39;woocommerce&#39; ); ?&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;?php
// Loop through order items
foreach ( $order-&gt;get_items() as $item_id =&gt; $item ) :
$product            = $item-&gt;get_product();
$product_id         = $item-&gt;get_variation_id() ? $item-&gt;get_variation_id() : $item-&gt;get_product_id();
?&gt;
&lt;tr class=&quot;&lt;?php echo esc_attr( apply_filters( &#39;woocommerce_order_item_class&#39;, &#39;woocommerce-table__line-item order_item&#39;, $item, $order ) ); ?&gt;&quot;&gt;
&lt;td class=&quot;woocommerce-table__product-name product-name&quot;&gt;&lt;?php echo wp_kses_post( $item-&gt;get_name() );?&gt;&lt;/td&gt;
&lt;td class=&quot;woocommerce-table__product-total product-total&quot;&gt;&lt;?php printf( 
&#39;&lt;input type=&quot;submit&quot; name=&quot;%s&quot; value=&quot;%s&quot; /&gt;&lt;br/&gt;&#39;,
&#39;product_item-&#39; . $item_id . &#39;-&#39; . $product_id, // Unique imput name with the item ID and the product ID
esc_html__(  &#39;Download this product invoice&#39;, &#39;woocommerce&#39;)
); ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php endforeach; ?&gt;
&lt;/tbody&gt;
&lt;/thead&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;!-- form end --&gt;
&lt;/section&gt;
&lt;?php
endif;
}

You will have to handle yourself the download functionality of every order item invoice and everything else you asked (as this is simply too broad and complicated to be handled in Stack overflow).

Note that the &lt;form&gt; element just need to be implemented once in the page, with a unique name attribute. Also, for each &lt;input type=&quot;submit&quot; name=&quot;&quot;&gt; each name attribute need to be unique.

Tested in the theme's functions.php file and works, displaying the following:

Add a section of order items with buttons to WooCommerce My Account single order pages.

huangapple
  • 本文由 发表于 2023年6月12日 11:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定