在WooCommerce订单处于待处理状态时允许减少库存

如何解决在WooCommerce订单处于待处理状态时允许减少库存

如果客户在线付款(例如使用PayPal),则WooCommerce创建订单,然后等待付款。此“等待中”是订单状态pending。在此期间,没有库存减少,这是非常糟糕的。通过将订单状态更改为on-hold,可以在成功付款后减少库存。但是有时付款可能要花30分钟,在这30分钟内,订单会保持pending状态,并且库存不会减少,因此,如果其库存的最后一件产品在此期间仍然可用。因此,如果我的每种产品只有1-3件库存,那么很有可能,如果我只有最后一种产品库存,那么在这30分钟内会有其他人来购买它,这导致根据情况,最后一件可以卖两次,这是不可接受的。因此,我需要在创建任何付款方式和运输方式的订单后立即减少库存。因此,我尝试创建一个片段,该片段将使用一个钩子woocommerce_order_status_changed,并且当订单状态更改为pending时,它应该总是减少库存,因为pending状态不会减少库存。我不知道这是不是正确的态度如何解决。有人可以帮忙吗?我尝试了这两个代码段,但行为没有任何变化:

add_filter( 'woocommerce_can_reduce_order_stock','wcs_do_not_reduce_onhold_stock',10,2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock,$order ) {
    if ( $order->has_status( 'pending' )) {
        $reduce_stock = true;
    }
    return $reduce_stock;
}
add_action( 'woocommerce_order_status_pending','wc_maybe_reduce_stock_levels' );
add_action( 'woocommerce_order_status_changed','order_stock_reduction_based_on_status',20,4 );
function order_stock_reduction_based_on_status( $order_id,$old_status,$new_status,$order ){
    if ( $new_status == 'pending'){
    $stock_reduced = get_post_meta( $order_id,'_order_stock_reduced',true );
        if( empty($stock_reduced) && $order->get_payment_method() == 'barion' ){
            wc_reduce_stock_levels($order_id);
        }
    }
}

解决方法

您可以简单地添加以下代码行,以减少待处理订单状态(WooCommerce会在未完成订单上触发the function wc_maybe_reduce_stock_levels()的工作):

add_action( 'woocommerce_order_status_pending','wc_maybe_reduce_stock_levels' );

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。

,

这是我现在发现的唯一可行的解​​决方案:

add_action( 'woocommerce_checkout_create_order','force_new_order_status',20,1 );
function force_new_order_status( $order )
{ 
if( ! $order->has_status('on-hold') ) 
        $order->set_status( 'on-hold','Forced status by a custom script (this note is not neccessary and can be deleted)' ); 
}

只有一个问题:如果付款失败,则库存不会退回。因此,必须采取其他措施,例如(例如)在60分钟后自动取消未付款订单,或使用来自付款网关的特定挂钩。 无论如何,我今天所学到的,set_statusupdate_status之间存在巨大差异。像我这样的初学者,要当心! :-) 因此,通过这种方式,我自动将WooCommerce处于pending状态(不会减少订购的库存)的每个新订单切换到on-hold状态(确实减少了订购的库存),这是解决我的问题的方法。对于高级程序员来说,这可能太容易了,但是由于我在搜索的两天内都没有找到问题的正确解决方案,因此我发布了它,即使节省另一个初学者的时间也是如此,因为我在搜索过程中发现:我不是下订单时只有一个需要立即减少库存的人。就是这样! :-)

,

您需要添加库存减少挂钩(正如 LoicTheAztec 已经回答的那样),但您还需要删除相反的 wc_maybe_increase_stock_levels 挂钩。要正确移除它,你需要在它已经被hook的时候运行它,所以你需要把它包装到init hook中。

以下对我有用(Woocommerce 5.4.1) - 确保待处理状态降低库存水平:

add_action( 'init',function() {
    add_action('woocommerce_order_status_pending','wc_maybe_reduce_stock_levels');
    remove_action('woocommerce_order_status_pending','wc_maybe_increase_stock_levels');
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-