Php 在WooCommerce中下订单后删除产品
我正在使用WordPress和WooCommerce的最新版本 我知道这在上下文中可能看起来很奇怪,但我想在functions.php中添加一个钩子,在下单后按ID从订单中删除某个产品 以下是我目前的工作内容:Php 在WooCommerce中下订单后删除产品,php,wordpress,woocommerce,product,orders,Php,Wordpress,Woocommerce,Product,Orders,我正在使用WordPress和WooCommerce的最新版本 我知道这在上下文中可能看起来很奇怪,但我想在functions.php中添加一个钩子,在下单后按ID从订单中删除某个产品 以下是我目前的工作内容: add_action('woocommerce_new_order','custom_process_order',10,1); 函数自定义处理订单($order\U id){ $order=新WC\U订单($order\U id); $items=$order->get_items()
add_action('woocommerce_new_order','custom_process_order',10,1);
函数自定义处理订单($order\U id){
$order=新WC\U订单($order\U id);
$items=$order->get_items();
foreach($items作为$item){
如果($item->get_id()==10766){
$order->remove_item($item->get_id());
}
}
返回$order\u id;
}
如果所述产品是订单的一部分,我将尝试从订单中删除ID为10766的产品。
我从另一个更老的帖子中得到了这个,所以代码本身可能并不完美。它肯定不起作用
经过一些实验后更新:在Cameron Hurd的帮助下,我尝试了以下代码:
add_action('woocommerce_new_order','custom_process_order',10,1);
函数自定义处理订单($order\U id){
$order=新WC\U订单($order\U id);
$items=$order->get_items();
foreach($items作为$item){
如果($item->get_id()==10766){
$order->remove_item($item->get_id());
}
}
$order->save_items();
返回$order\u id;
}
这似乎很有希望,但按下结帐按钮将提交一个空订单,同时还会抛出一条空的错误消息,将用户留在结帐页面上
在此之后,我有两个想法:1.)将
remove_item
更改为wc_delete_order_item
。我真的不知道有什么不同,改变它似乎也没有什么不同。2.)更改挂钩,使其在订单位于后端后发生。我试着把它从
woocommerce\u new\u order
改为woocommerce\u thankyou
。然而,除了对订单不做任何修改外,这还破坏了感谢页面
这就是我现在面对的问题:
add\u action('woocommerce\u checkout\u create\u order','custom\u process\u order',10,1);
函数自定义处理订单($order\U id){
$order=新WC\U订单($order\U id);
$items=$order->get_items();
foreach($items作为$item){
$current_item_id=$item->get_id();
如果($current_item_id==10766){
$order->remove_item($current_item_id);
}
}
$order->save_items();
返回$order\u id;
}
仍在寻找有效答案。浏览
摘要/abstract wc order.php
中的代码,其中删除项
方法位于。。。我看到项目被添加到一个名为items\u to\u delete
的数组中。该变量再次出现在save_items
中,其中要删除的每个item类都会调用其方法
<?php
// abstracts/abstract-wc-order.php
abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Remove item from the order.
*
* @param int $item_id Item ID to delete.
* @return false|void
*/
public function remove_item( $item_id ) {
$item = $this->get_item( $item_id, false );
$items_key = $item ? $this->get_items_key( $item ) : false;
if ( ! $items_key ) {
return false;
}
// Unset and remove later.
$this->items_to_delete[] = $item;
unset( $this->items[ $items_key ][ $item->get_id() ] );
}
// ...
/**
* Save all order items which are part of this order.
*/
protected function save_items() {
$items_changed = false;
foreach ( $this->items_to_delete as $item ) {
$item->delete();
$items_changed = true;
}
$this->items_to_delete = array();
// ...
}
// ...
}
从
abstracts/abstractwcorder.php
浏览代码,其中remove\u item
方法位于。。。我看到项目被添加到一个名为items\u to\u delete
的数组中。该变量再次出现在save_items
中,其中要删除的每个item类都会调用其方法
<?php
// abstracts/abstract-wc-order.php
abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Remove item from the order.
*
* @param int $item_id Item ID to delete.
* @return false|void
*/
public function remove_item( $item_id ) {
$item = $this->get_item( $item_id, false );
$items_key = $item ? $this->get_items_key( $item ) : false;
if ( ! $items_key ) {
return false;
}
// Unset and remove later.
$this->items_to_delete[] = $item;
unset( $this->items[ $items_key ][ $item->get_id() ] );
}
// ...
/**
* Save all order items which are part of this order.
*/
protected function save_items() {
$items_changed = false;
foreach ( $this->items_to_delete as $item ) {
$item->delete();
$items_changed = true;
}
$this->items_to_delete = array();
// ...
}
// ...
}
您可以使用
woocommerce\u checkout\u order\u processed
操作挂钩,
然后指定产品id
所以你得到:
function自定义处理订单($order\u id){
如果(!$order\u id)返回;
//获取订单对象
$order=新WC\U订单($order\U id);
//get order items=订单中的每个产品
$items=$order->get_items();
foreach($items作为$item){
$product=wc\U get\U product($item['product\U id');
如果($product->get_id()==10766){
$order->remove_item($item->get_id());
}
}
//算计
$order->计算_总计();
//拯救
$order->save();
}
添加操作('woocmerce\u checkout\u order\u processed'、'custom\u process\u order',10,1);
您可以使用woocommerce\u checkout\u order\u processed
操作挂钩,
然后指定产品id
所以你得到:
function自定义处理订单($order\u id){
如果(!$order\u id)返回;
//获取订单对象
$order=新WC\U订单($order\U id);
//get order items=订单中的每个产品
$items=$order->get_items();
foreach($items作为$item){
$product=wc\U get\U product($item['product\U id');
如果($product->get_id()==10766){
$order->remove_item($item->get_id());
}
}
//算计
$order->计算_总计();
//拯救
$order->save();
}
添加操作('woocmerce\u checkout\u order\u processed'、'custom\u process\u order',10,1);
这很有效,感谢您的深入回答!我仍然有一个问题,如果所说的产品是购物车中唯一的产品,用户就不能签出(这是有道理的)。Woocommerce显示一条空的错误消息。是否有办法在下完完整订单后删除产品或填写错误消息?我已对此进行了重新测试,即使在添加更多产品时,也会收到一条空的错误消息,订单已下,但完全为空。我试着同时使用woocommerce\u checkout\u update\u order\u meta
和woocommerce\u thankyou
,但这两种方法都没有奏效。也许代码中还存在问题?这很有效,感谢您的深入回答!我仍然有一个问题,如果所说的产品是购物车中唯一的产品,用户就不能签出(这是有道理的)。Woocommerce显示一条空的错误消息。是否有办法在下完完整订单后删除产品或填写错误消息?我已对此进行了重新测试,即使在添加更多产品时,也会收到一条空的错误消息,订单已下,但完全为空。我尝试