允许客户删除 Woocommerce 中的订单商品
Allow customer to remove order items in Woocommerce
你好我有这段代码应该从订单中删除一个项目
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 );
$order->calculate_totals();
}
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="Complete Cancellation" />
</form>';
}
但是当我按下完全取消页面刷新但没有删除任何内容 & 我再次刷新没有删除任何内容
我做错了什么?
您的代码中存在多个错误,例如:
- 条件
$_POST["remove_item_$item_id"] == 'Remove this item'
始终为假。
- 您需要在页面开始加载之前使用挂钩删除项目,以获取刷新。否则您将看不到该项目已被删除,您将需要重新加载页面一次。
所以试试下面的方法:
// Displaying the form fields (buttons and hidden fields)
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;
echo '<form class="cart item-'.$item_id.'" method="post" style= "margin-top:12px;">
<input type="hidden" name="item_id" value="'.$item_id.'" />
<input type="hidden" name="order_id" value="'.$order->get_id().'" />
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Complete Cancellation" />
</form>';
}
// Processing the request
add_action( 'template_redirect', 'process_remove_order_item' );
function process_remove_order_item(){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) )
return;
if( isset($_POST['item_id']) && isset($_POST['remove_item_'.$_POST['item_id']]) && isset($_POST['order_id'])
&& is_numeric($_POST['order_id']) && get_post_type($_POST['order_id']) === 'shop_order' ) {
// Get the WC_Order Object
$order = wc_get_order( absint($_POST['order_id']) );
// Remove the desired order item
if( is_a($order, 'WC_Order') && is_numeric($_POST['item_id']) ) {
$order->remove_item( absint($_POST['item_id']) );
$order->calculate_totals();
// Optionally display a notice
wc_add_notice( __('Order item removed successfully'), 'success' );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
你好我有这段代码应该从订单中删除一个项目
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 );
$order->calculate_totals();
}
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="Complete Cancellation" />
</form>';
}
但是当我按下完全取消页面刷新但没有删除任何内容 & 我再次刷新没有删除任何内容
我做错了什么?
您的代码中存在多个错误,例如:
- 条件
$_POST["remove_item_$item_id"] == 'Remove this item'
始终为假。 - 您需要在页面开始加载之前使用挂钩删除项目,以获取刷新。否则您将看不到该项目已被删除,您将需要重新加载页面一次。
所以试试下面的方法:
// Displaying the form fields (buttons and hidden fields)
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;
echo '<form class="cart item-'.$item_id.'" method="post" style= "margin-top:12px;">
<input type="hidden" name="item_id" value="'.$item_id.'" />
<input type="hidden" name="order_id" value="'.$order->get_id().'" />
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Complete Cancellation" />
</form>';
}
// Processing the request
add_action( 'template_redirect', 'process_remove_order_item' );
function process_remove_order_item(){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) )
return;
if( isset($_POST['item_id']) && isset($_POST['remove_item_'.$_POST['item_id']]) && isset($_POST['order_id'])
&& is_numeric($_POST['order_id']) && get_post_type($_POST['order_id']) === 'shop_order' ) {
// Get the WC_Order Object
$order = wc_get_order( absint($_POST['order_id']) );
// Remove the desired order item
if( is_a($order, 'WC_Order') && is_numeric($_POST['item_id']) ) {
$order->remove_item( absint($_POST['item_id']) );
$order->calculate_totals();
// Optionally display a notice
wc_add_notice( __('Order item removed successfully'), 'success' );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。