更改WooCommerce购物车中产品的价格并结帐 [英] Change price of product in WooCommerce cart and checkout

查看:111
本文介绍了更改WooCommerce购物车中产品的价格并结帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个WooCommerce插件来定制产品,它基于访问者选择的选项,并计算出定制价格并将其存储在会话表中。

我也可以在购物车页面中获得该值。

I'm creating a WooCommerce add-on to customize the product, based on selected options by the visitor and custom price is calculated and stored into session table also.
I am able to get that value in cart page also.

我的问题:
我想更改产品的默认价格,并在WooCommerce流程中将其替换为新的计算值作为购物车,结帐,付款,邮件通知,订单...

My problem:
I would like to change the default price of the product and replace it with new calculated value in the WooCommerce process as cart, checkout, payment, mail notifications, order...

有什么建议吗?

谢谢

推荐答案

Ce使其正常运行的正确方法是 woocommerce_before_calculate_totals 。但是您必须完成(替换)代码才能在下面的挂钩函数中获得新价格:

Ce right hook to get it working is woocommerce_before_calculate_totals. But you will have to complete (replace) the code to get the new price in the hooked function below:

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop Through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get the product id (or the variation id)
        $product_id = $cart_item['data']->get_id();

        // GET THE NEW PRICE (code to be replace by yours)
        $new_price = 500; // <== Add your code HERE

        // Updated cart item price
        $cart_item['data']->set_price( $new_price ); 
    }
}

代码会出现在您活跃孩子的function.php文件中主题(或主题)或任何插件文件中。

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试,可在WooCommerce 3+版本上运行。但是,由于您不提供任何代码,因此我无法对其进行测试以真正从会话中获取新价格…

This code is tested and works on WooCommerce versions 3+. But as you don't give any code I can't test it for real getting the new price from session…

这篇关于更改WooCommerce购物车中产品的价格并结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆