php - 操纵购物车中的 woocommerc 价格
问题描述
我试图操纵购物车中的计算价格,但没有运气......我希望有人可以在这里帮助我。
我找到了这篇文章并实现了写在页面上的代码,这 98% 与我搜索的内容完全相同。如果价格类型是每 100 克,我需要添加购物车计算。
所以这是实际的工作代码:
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html', 10, 2 );
// Adding a custom field to the price markup
function wb_change_product_html( $price, $product ) {
//$wb_price_type = get_field('product_price_type');
$wb_price_type = get_post_meta( $product->get_id(), 'product_price_type', true);
if($wb_price_type) {
$price_html = '<span class="amount">' . $price . ' ' . $wb_price_type . '</span>';
}
else {
$price_html = '<span class="amount">' . $price . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3 );
// Adding a custom field to the price in the cart
function wb_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true );
if ($wb_price_type) {
$price = $price . ' ' . $wb_price_type;
}
else {
$price = $price;
}
return $price;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'wb_checkout_review', 10, 3 );
// Adding a custom field to the price in the checkout items
function wb_checkout_review ( $quantity, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true);
if ( $wb_price_type ) {
$cart_item = ' ' . sprintf( '× %s ', $cart_item['quantity'] ) . $wb_price_type . '';
}
else {
$cart_item = ' ' . sprintf( '× %s', $cart_item['quantity'] ) . '';
}
return $cart_item;
}
好吧,我认为它真的很容易计算,所以我做了这样的事情:
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3 );
// Adding a custom field to the price in the cart
function wb_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true );
if ($wb_price_type) {
if ($wb_price_type == "per 100g") {
$price = $price / 100 . ' ' . $wb_price_type;
}
else {
$price = $price . ' ' . $wb_price_type;
}
}
else {
$price = $price;
}
return $price;
}
但这没有用......我也试图直接操纵价格,但我随时都会得到 0 的价格价值......
所以我多谷歌了一下,发现了这篇文章,这最终正是我想要的。在最后一个打印屏幕中,我们可以看到价格将显示为每公斤(这完全没问题),并且在计算中它使用了其他价格(我尝试使用上面的代码)。
我认为问题在于 $price 具有例如值“20$”,这是一个字符串,我无法用字符串计算。拆分这个字符串是没有意义的,应该有另一种方式。
为了更好地理解这里的图片。在上半部分,我们看到了工作计算(这很好),但这并不是我所需要的。在下面的部分中,我们看到它只会计算计算出的价格(右侧)。下降部分的左侧保持正确的价格。
解决方案
我认为问题在于 $price 具有例如值“20$”,这是一个字符串,我无法用字符串计算。拆分这个字符串是没有意义的,应该有另一种方式。
这是真的,它实际上是一个完整的 html 字符串。尝试更改 woocommerce_cart_item_price 过滤器以从字符串中获取价格:
add_filter('woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3);
// Adding a custom field to the price in the cart
function wb_change_product_price_cart($price, $cart_item, $cart_item_key)
{
$wb_price_type = get_post_meta($cart_item['product_id'], 'product_price_type', true);
if ($wb_price_type) {
if ($wb_price_type == "per 100g") {
preg_match("/([0-9]+\.[0-9]+)/", $price, $matches);
$_price = $matches[0];
$per100 = $_price / 100;
$price = $per100 . ' ' . $wb_price_type;
} else {
$price = $price . ' ' . $wb_price_type;
}
} else {
$price = $price;
}
return $price;
}
推荐阅读
- typescript - 基于Typescript接口中另一个字段值的条件字段
- android - 使用 Ifstream c++ 打开 csv 文件
- java - 如何从字符串中提取用户名?
- swift - 获得贝塞尔曲线的 N 个点
- c# - 如何为 LEFT JOIN 为 GROUP BY 的表编写 LINQ?
- java - LibGDX - 平铺地图不渲染
- python - GET 请求中的请求表单烧瓶为空
- go - HDFS 客户端拨号 tcp:在 127.0.0.11:53 上查找 xxx(我的主机名):没有这样的主机
- python - 我在运行 MIT 6.0001 F16 pset5 ps5.py 时遇到了 RecursionError
- django - Django从模型对象过滤后删除重复项