英文:
Add a section of order items with buttons to WooCommerce My Account single order pages
问题
在WooCommerce中,当客户购买一个或多个产品时,他可以在他的“我的账户”部分的单个订单页面上看到:
我想在这个“我的账户”部分的单个订单页面上添加一个部分,列出当前订单的所有订单项目(购买的产品),每个订单项目都有一个按钮,所以用户将默认看到以下内容:
我正在使用以下代码显示一个按钮,当用户点击购买的每个产品的按钮时,我希望检查以下条件:
-
首先,检查是否购买了这个产品的用户与点击“下载此产品发票”按钮的用户是同一个人?
-
如果购买了产品,应生成一个文本文件,将与买家点击的相应按钮(“下载此产品发票”)的同一产品的名称以及买家的电子邮件地址放入该文件中,然后下载该文件(文本文件如下所示)
-
应在每个已购买的产品前或为每个已购买的产品创建一个按钮(“下载此产品发票”)
> 亲爱的用户,您好,感谢您购买(虚拟产品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:
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:
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:
-
First, check if this product was purchased by the same user who clicked on the (Download this product invoice) button?
-
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)
-
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('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();
}
答案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
文件中进行测试并运行,会显示如下内容:
英文:
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('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
// Only for 'completed' and 'processing' orders
if ( $order->has_status( array('completed', 'processing') ) ) :
$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
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 <form>
element just need to be implemented once in the page, with a unique name
attribute. Also, for each <input type="submit" name="">
each name
attribute need to be unique.
Tested in the theme's functions.php file and works, displaying the following:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论