woocommerce在添加到购物车时更改价格

woocommerce在添加到购物车时更改价格,woocommerce,Woocommerce,我有一个1欧元的产品,并使用e GET参数在运行时更改价格: http://url/warenkorb/?add-to-cart=1539&price=18.45 谢谢首先,我想提醒您,如果您在“添加到购物车”链接的url中使用价格,可能会使您的商店易受攻击。如果您在产品meta中使用此选项,则会更加安全。无论如何,我将向您展示如何从url设置价格的代码 add_filter( 'woocommerce_add_cart_item', 'c_other_options_add_cart_item

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

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

首先,我想提醒您,如果您在“添加到购物车”链接的url中使用价格,可能会使您的商店易受攻击。如果您在产品meta中使用此选项,则会更加安全。无论如何,我将向您展示如何从url设置价格的代码

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;
}

这对我来说不起作用,我修改了以修改通过参数传递的价格

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;
}
?>

使用WooCommerce 2.5,我发现这是一个由两部分组成的过程。第一步是在通过woocommerce\u add\u cart\u项目过滤器添加到购物车时更改定价的运行时显示。第二部分是设置在签出期间通过woocommerce\u get\u cart\u item\u from\u 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_项目中的woo_数据的一部分,允许稍后通过会话价格过滤器检索该数据。一种更安全的方法是不在URL中传递价格,并将其直接设置为避免URL价格操纵


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

在woocommerce 3.0.0版发布后,产品价格在添加到购物车时使用设定价格($price)进行更新功能。示例如下所示:

add_action( 'woocommerce_before_calculate_totals', 'mj_custom_price' );

function mj_custom_price( $cart_object ) {
   $woo_ver = WC()->version; 
   $custom_price = 10;
   foreach ( $cart->cart_contents as $key => $value )
   {
       if($woo_ver < "3.0.0" && $woo_ver < "2.7.0")
       {
           $value['data']->price = $custom_price;
       }
       else
       {
           $value['data']->set_price($custom_price);
       }
   }            
}
add_action('woocommerce_-before_-calculate_-totals','mj_-custom_-price');
函数mj_自定义_价格($cart_对象){
$woo_ver=WC()->版本;
$custom_价格=10;
foreach($cart->cart\u内容为$key=>$value)
{
如果($woo_ver<“3.0.0”和&$woo_ver<“2.7.0”)
{
$value['data']->price=$custom\u price;
}
其他的
{
$value['data']->set_price($custom_price);
}
}            
}

非常感谢

您好,谢谢您的评论。那不行,购物车的价格是1欧元,好吧,但那辆对我有用。如果价格传递给$cart\u item\u meta[“其他选项”][“产品价格”],则应尝试调试。如果价格未在此处传递或未设置,则调整将不起作用。这是一个非常简单和通用的解决方案。购物车是空的:
code
c_其他_选项\u添加_购物车\u项目\u数据:数组(1){[“产品价格”]=>string(5)“17.45”}c_其他_选项\u添加_购物车\u项目:数组(1){[“产品价格”]=>string(5)“17.45”}
code
您使用的是哪个版本的woocommerce?此代码在2.1.9版本2.1.12中运行良好。使用您的代码,无论我将哪个产品放入购物车,购物车都将保持为空。+1。我只是将价格设置为“woocommerce\u add\u cart\u item”挂钩,但它不起作用。当我在“woocommerce\u get\u cart\u item\u from\u session”钩子上设置价格时,它也工作了,并在购物车页面上显示了更新的价格。您好,请更新您的答案。第二行是
add\u filter('woocommerce\u get\u cart\u item\u from\u session','set\u session\u prices'),20,3)应更改为:
添加过滤器('woocommerce\u get\u cart\u item\u from\u session','set\u session\u prices',20,3)if语句在大括号前缺少右括号。也谢谢你,伙计:)它帮了我很多忙!这个答案应该被接受,非常感谢。当我以这种方式设置价格时,导航栏中的购物车小部件显示双倍的价格(尽管结帐/购物车页面中的价格是正确的)。有人能告诉我问题出在哪里吗?
add_action( 'woocommerce_before_calculate_totals', 'mj_custom_price' );

function mj_custom_price( $cart_object ) {
   $woo_ver = WC()->version; 
   $custom_price = 10;
   foreach ( $cart->cart_contents as $key => $value )
   {
       if($woo_ver < "3.0.0" && $woo_ver < "2.7.0")
       {
           $value['data']->price = $custom_price;
       }
       else
       {
           $value['data']->set_price($custom_price);
       }
   }            
}