【问题标题】:woocommerce change price while add to cartwoocommerce 在添加到购物车时更改价格
【发布时间】:2014-09-04 01:11:50
【问题描述】:

我有一个产品 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;
}

谢谢

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    对于 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。我只是将价格设置为“woocommerce_add_cart_item”挂钩,但它不起作用。当我在 'woocommerce_get_cart_item_from_session' 挂钩上设置价格时,它也会起作用并在购物车页面上显示更新的价格。
    • 您好,请更新您的答案。您的第二行是 add_filter( 'woocommerce_get_cart_item_from_session', 'set_session_prices' ) , 20 , 3 ); 应更改为:add_filter( 'woocommerce_get_cart_item_from_session', 'set_session_prices' , 20 , 3 ); if 语句缺少大括号前的右括号。
    • 谢谢你,伙计 :) 这对我帮助很大!
    • 这个答案应该被接受,非常感谢@LanceCleveland
    • 非常感谢。当我以这种方式设置价格时,导航栏中的购物车小部件显示价格的两倍(尽管结帐/购物车页面中的价格是正确的)。有人可以建议可能是什么问题吗?
    【解决方案2】:

    首先,我想警告您,如果您在 add_to_cart 链接的 url 中使用价格,可能会使您的商店易受攻击。如果您在产品元数据中使用它会更安全。无论如何,我正在向您展示实现如何从 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;
    }
    

    【讨论】:

    • 您好,感谢您的评论。那不行,购物车显示旧价格 1€
    • 好的,但是那个对我有用。如果将价格传递给 $cart_item_meta['_other_options']['product-price'],您应该尝试调试。如果此处未通过价格或未设置价格,则调整将不起作用。这是一个非常简单和通用的解决方案。
    • 购物车为空:code c_other_options_add_cart_item_data: array(1) {["product-price"]=> string(5) "17.45" } c_other_options_add_cart_item: array(1) { ["product-价格"]=> 字符串(5) "17.45" } code
    • 您使用的是哪个版本的 woocommerce?此代码在 2.1.9 中运行良好
    • 版本 2.1.12。使用您的代码,无论我将哪种产品放入购物车,购物车都是空的。
    【解决方案3】:

    woocommerce 3.0.0 版发布后,使用 set_price($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);
           }
       }            
    }
    

    非常感谢

    【讨论】:

      【解决方案4】:

      它不适合我,我修改以修改参数传递的价格

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 2016-09-15
        • 2017-04-06
        • 2015-11-28
        • 2017-09-10
        • 1970-01-01
        相关资源
        最近更新 更多