用于从 WooCommerce 中的订单中删除项目的按钮
A Button to remove an item from the order in WooCommerce
我正在寻找一种方法,在每件商品旁边添加一个按钮,以便将其从订单中删除。
模板是:woocommerce/order/order-details-item.php
我找到了一个可以使用的函数,它是:wc_delete_order_item()
如果我将它添加到循环中,它会删除所有项目。
但是我如何 运行 单击按钮的功能?我试过使用 ajax,但是在外部文件中使用 wc_delete_order_item()
会得到 "Undefined function"。
请帮忙。
首先(只是为了解决问题)模板 order-details-item.php
用于:
- 订单已收到页面(谢谢)
- 我的账户查看订单页面
现在要使用 wc_delete_order_item()
,您需要项目 ID 作为参数。
您可以使用挂钩在 woocommerce_order_item_meta_end
操作挂钩中的自定义函数来显示功能按钮以删除订单项目,而不是覆盖模板,但它会删除订单项,将需要第二次刷新。
视觉上你会得到:
这是部分代码:
add_action( 'woocommerce_order_item_meta_end', 'display_remove_order_item_button', 10, 3 );
function display_remove_order_item_button( $item_id, $item, $order ){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;
if( isset($_POST["remove_item_$item_id"]) && $_POST["remove_item_$item_id"] == 'Remove this item' ){
wc_delete_order_item( $item_id );
}
echo '<form class="cart" method="post" enctype="multipart/form-data" style= "margin-top:12px;">
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Remove this item" />
</form>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
它将删除订单项。
您还需要使用 WC_Abstract_Order
方法 calculate_totals()
到 refresh/calculate 新订单总数:
$order->calculate_totals();
But the best and completely functional way should be to use Ajax, which is much more complicated, and a real development.
To use correctly Ajax here, you should need to add a hidden field with the Item ID, to get it with ajax and give it to your PHP function (the missing argument, to make wc_delete_order_item()
working fine without any error) and also the Order ID…
下次,如果你尝试 Ajax 方式,你应该问一个新问题,添加你自己的代码,解释什么有效,什么无效…
我正在寻找一种方法,在每件商品旁边添加一个按钮,以便将其从订单中删除。
模板是:woocommerce/order/order-details-item.php
我找到了一个可以使用的函数,它是:wc_delete_order_item()
如果我将它添加到循环中,它会删除所有项目。
但是我如何 运行 单击按钮的功能?我试过使用 ajax,但是在外部文件中使用 wc_delete_order_item()
会得到 "Undefined function"。
请帮忙。
首先(只是为了解决问题)模板 order-details-item.php
用于:
- 订单已收到页面(谢谢)
- 我的账户查看订单页面
现在要使用 wc_delete_order_item()
,您需要项目 ID 作为参数。
您可以使用挂钩在 woocommerce_order_item_meta_end
操作挂钩中的自定义函数来显示功能按钮以删除订单项目,而不是覆盖模板,但它会删除订单项,将需要第二次刷新。
视觉上你会得到:
这是部分代码:
add_action( 'woocommerce_order_item_meta_end', 'display_remove_order_item_button', 10, 3 );
function display_remove_order_item_button( $item_id, $item, $order ){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;
if( isset($_POST["remove_item_$item_id"]) && $_POST["remove_item_$item_id"] == 'Remove this item' ){
wc_delete_order_item( $item_id );
}
echo '<form class="cart" method="post" enctype="multipart/form-data" style= "margin-top:12px;">
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Remove this item" />
</form>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
它将删除订单项。
您还需要使用 WC_Abstract_Order
方法 calculate_totals()
到 refresh/calculate 新订单总数:
$order->calculate_totals();
But the best and completely functional way should be to use Ajax, which is much more complicated, and a real development.
To use correctly Ajax here, you should need to add a hidden field with the Item ID, to get it with ajax and give it to your PHP function (the missing argument, to make
wc_delete_order_item()
working fine without any error) and also the Order ID…
下次,如果你尝试 Ajax 方式,你应该问一个新问题,添加你自己的代码,解释什么有效,什么无效…