woocommerce 在添加到购物车时更改价格 [英] woocommerce change price while add to cart

查看:47
本文介绍了woocommerce 在添加到购物车时更改价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品 1€ 并使用 e GET 参数在运行时更改价格:

i have a one product 1€ and use e GET parameter to change the price at runtime:

http://url/warenkorb/?add-to-cart=1539&price=18.45

这个变化不是篮子里的价格(仍然是1€)

This change not the price in the basket (still remain 1€)

如何实现这一目标.

我使用以下钩子:

add_filter( 'woocommerce_add_cart_item', 'my_woocommerce_add_cart_item', 5, 1 );
function my_woocommerce_add_cart_item( $cart_item ) {

if(get_post_meta( $cart_item['data']->id, 'isConfigurableProduct', true)=='1')
{   
    if(isset($_GET['price']))
    {
        $price=$_GET['price'];//keep it simpel for testing
        $cart_item['data']->set_price( $price );
        $_SESSION[$cart_item['data']->id]=$price;
    }
    else
    {
        $cart_item['data']->set_price($_SESSION[$cart_item['data']->id]);   
    }
}
return $cart_item;
}

谢谢

推荐答案

使用 WooCommerce 2.5 我发现这是一个由两部分组成的过程.第一步是在通过 woocommerce_add_cart_item 过滤器添加到购物车时更改定价的运行时显示.第二部分是通过woocommerce_get_cart_item_from_session过滤器设置结账时读取的持久会话数据.

With WooCommerce 2.5 I found this to be a 2-part process. The first step is to change the run-time display of pricing when added to the cart via the woocommerce_add_cart_item filter. The second part is to set the persistent session data which is read during checkoutvia the woocommerce_get_cart_item_from_session filter.

add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');
add_filter( 'woocommerce_get_cart_item_from_session',  'set_session_prices', 20 , 3 );

function set_woo_prices( $woo_data ) {
  if ( ! isset( $_GET['price'] ) || empty ( $_GET['price'] ) ) { return $woo_data; }
  $woo_data['data']->set_price( $_GET['price'] );
  $woo_data['my_price'] = $_GET['price'];
  return $woo_data;
}

function  set_session_prices ( $woo_data , $values , $key ) {
    if ( ! isset( $woo_data['my_price'] ) || empty ( $woo_data['my_price'] ) ) { return $woo_data; }
    $woo_data['data']->set_price( $woo_data['my_price'] );
    return $woo_data;
}

在 woocommerce_add_cart_item 中将 my_price 设置为 woo_data 的一部分允许稍后通过会话价格过滤器检索该数据.更安全的方法是不在 URL 中传递价格并直接设置它以避免 URL 价格操纵.

Setting my_price as part of the woo_data in woocommerce_add_cart_item allows that data to be retrieved later via the session price filter. A more secure method is to not pass price in the URL and set it directly as to avoid URL price manipulation.

在我将 Store Locator Plus 连接到 WooCommerce 的实际实现中,我将每个 WooCommerce 产品的按位置定价数据存储在一个内部表中,并且仅设置/检索位置 ID 来代替本示例中的my_price".内部方法用于从上述两种方法中的数据表中获取设置的价格,使用位置 ID 作为查找,只留下带有位置 ID 的公共 URL,不允许他们修改定价.

In my real-world implementation connecting Store Locator Plus to WooCommerce, I store per-location pricing data for each WooCommerce product in an internal table and only set/retrieve the location ID in place of 'my_price' in this example. Internal methods are used to fetch the set price from the data table in both methods above, using the location ID as a lookup and only leaving the public URL with a location ID which is not going to allow them to modify pricing.

这篇关于woocommerce 在添加到购物车时更改价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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