我有一个产品1€并使用e GET参数在运行时更改价格:
http://url/warenkorb/?add-to-cart=1539&price=18.45
这个改变不是篮子里的价格(仍然是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;
}
由于
答案 0 :(得分:9)
使用WooCommerce 2.5,我发现这是一个由两部分组成的过程。第一步是通过woocommerce_add_cart_item过滤器添加到购物车时更改定价的运行时显示。第二部分是通过woocommerce_get_cart_item_from_session过滤器设置在结账时读取的持久会话数据。
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;
}
将my_price设置为woocommerce_add_cart_item中woo_data的一部分,允许稍后通过会话价格过滤器检索该数据。更安全的方法是不在URL中传递价格并直接设置它以避免URL价格操纵。
在将Store Locator Plus连接到WooCommerce的实际实现中,我将每个WooCommerce产品的每位置定价数据存储在内部表中,并且在此示例中仅设置/检索位置ID以代替“my_price”。内部方法用于在上述两种方法中从数据表中获取设定价格,使用位置ID作为查找,只留下公共URL,其位置ID不允许他们修改定价。
答案 1 :(得分:0)
首先,我想提醒您,如果您在add_to_cart链接的网址中使用价格可能会使您的商店容易受到攻击。如果你在产品元中使用它会更安全。无论如何,我向您展示了如何从网址设置价格的代码。
add_filter( 'woocommerce_add_cart_item', 'c_other_options_add_cart_item', 20, 1 );
function c_other_options_add_cart_item( $cart_item ) {
if (isset($cart_item['_other_options'])) :
if( isset($cart_item['_other_options']['product-price']) )
$extra_cost = floatval($cart_item['_other_options']['product-price']);
$cart_item['data']->adjust_price( $extra_cost );
// here the real adjustment is going on...
endif;
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'c_other_options_add_cart_item_data', 10, 2 );
function c_other_options_add_cart_item_data($cart_item_meta, $product_id){
global $woocommerce;
$product = new WC_Product( $product_id);
$price = $product->price;
if(empty($cart_item_meta['_other_options']))
$cart_item_meta['_other_options'] = array();
$cart_item_meta['_other_options']['product-price'] = esc_attr($_REQUEST['price']) - $price;
// as woocommerce allows to adjust the price (i don't know if there is any way to reset it, but this procedure works), we need to return the adjustable price
return $cart_item_meta;
}
答案 2 :(得分:0)
它不适合我,我修改为修改参数传递的价格
spainbox.com/carro/?add-to-cart=28792&shippingprice=141
产品的价格为1欧元,我需要添加到购物车这个产品,因为它的服务将有运费的价格
<?
add_filter( 'woocommerce_add_cart_item', 'c_other_options_add_cart_item', 20, 1 );
function c_other_options_add_cart_item( $cart_item ) {
if (isset($cart_item['_other_options'])) :
if( isset($cart_item['_other_options']['product-price']) )
$extra_cost = floatval($cart_item['_other_options']['product-price']);
$cart_item['data']->adjust_price( $extra_cost );
// here the real adjustment is going on...
endif;
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'c_other_options_add_cart_item_data', 10, 2 );
function c_other_options_add_cart_item_data($cart_item_meta, $product_id){
global $woocommerce;
$product = new WC_Product( $product_id);
$price = $product->price;
if(empty($cart_item_meta['_other_options']))
$cart_item_meta['_other_options'] = array();
$cart_item_meta['_other_options']['product-price'] = esc_attr($_REQUEST['shippingprice']);
// as woocommerce allows to adjust the price (i don't know if there is any way to reset it, but this procedure works), we need to return the adjustable price
return $cart_item_meta;
}
?>