如何使用 Hook 有效更改 WooCommerce 3 中的产品价格?
Nov 28, 2024 am 08:38 AM在 WooCommerce 3 中使用 Hooks 更改产品价格
WooCommerce 3 引入了使用 Hooks 操纵产品价格的改进选项,包括变化价格的选项。但是,您尝试的挂钩可能不合适。
用于控制产品价格的推荐挂钩是:
-
简单、分组和外部产品:
- woocommerce_product_get_price
- woocommerce_product_get_regular_price
-
变体:
- woocommerce_product_variation_get_regular_price
- woocommerce_product_variation_get_price
-
变量 (价格范围):
- woocommerce_variation_prices_price
- woocommerce_ variation_prices_regular_price
-
销售额:
- wooco mmerce_product_get_sale_price
- woocommerce_variation_prices_sale_price
- woocommerce_product_variation_get_sale_price
对于变化价格,您还需要管理缓存价格。 woocommerce_get_variation_prices_hash 过滤器对于此目的非常有效。
为了更有效地处理变化,请考虑下面提供的代码的插件版本。它包含一个构造函数并处理价格缓存。
// Plugin Version class MyPlugin { public function __construct() { // Simple, grouped, and external products add_filter('woocommerce_product_get_price', array($this, 'custom_price'), 99, 2); add_filter('woocommerce_product_get_regular_price', array($this, 'custom_price'), 99, 2); // Variations add_filter('woocommerce_product_variation_get_regular_price', array($this, 'custom_price'), 99, 2); add_filter('woocommerce_product_variation_get_price', array($this, 'custom_price'), 99, 2); // Variable (price range) add_filter('woocommerce_variation_prices_price', array($this, 'custom_variable_price'), 99, 3); add_filter('woocommerce_variation_prices_regular_price', array($this, 'custom_variable_price'), 99, 3); // Handling price caching add_filter('woocommerce_get_variation_prices_hash', array($this, 'add_price_multiplier_to_variation_prices_hash'), 99, 3); } // ...functionality as described in the original answer... } new MyPlugin();
登录后复制
对于主题使用,请将以下代码放入您的functions.php文件中:
// Theme Version // ...functionality as described in the original answer...
登录后复制
记住,使用显示html挂钩不会有效改变产品价格。此外,考虑在小部件中过滤产品价格的挂钩,它允许设置最低和最高价格。
以上是如何使用 Hook 有效更改 WooCommerce 3 中的产品价格?的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)