【发布时间】:2015-05-26 12:22:52
【问题描述】:
我目前正在清空并在访问该网站时将产品添加到用户购物车中 - 因为他们将永远只有一个产品(捐赠),如下所示:
function add_donation_to_cart() {
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart('195', 1, null, null, null);
}
我使用自定义表单来获取$_POST 信息 - 金额已发布到捐赠页面,实际上是用户购物车,其中已经有产品。自定义金额在以下函数中用于更改价格。价格正确显示在购物车、结帐页面以及重定向支付网关(在重定向页面本身内)上。
但是,一旦您被重定向,woocommerce 就会创建一个订单并将其标记为“处理中”。订单上显示的金额不正确。
我用来改价的功能如下图所示:
add_action('woocommerce_before_calculate_totals', 'add_custom_total_price');
function add_custom_total_price($cart_object)
{
session_start();
global $woocommerce;
$custom_price = 100;
if($_POST)
{
if(!empty($_POST['totalValue']))
{
$theVariable = str_replace(' ', '', $_POST['totalValue']);
if(is_numeric($theVariable))
{
$custom_price = $theVariable;
$_SESSION['customDonationValue'] = $custom_price;
}
else
{
$custom_price = 100;
}
}
}
else if(!empty($_SESSION['customDonationValue']))
{
$custom_price = $_SESSION['customDonationValue'];
}
else
{
$custom_price = 100;
}
var_dump($_SESSION['customDonationValue']);
foreach ( $cart_object->cart_contents as $key => $value )
{
$value['data']->price = $custom_price;
}
}
现在我不完全确定它是否与我的 if 语句有关,但即使产品价格设置为 0,价格总是错误地设置为 100。
任何帮助或见解将不胜感激!
【问题讨论】:
标签: php wordpress woocommerce