【问题标题】:Set cart item custom prices from session data in WooCommerce 3从 WooCommerce 3 中的会话数据设置购物车项目自定义价格
【发布时间】:2018-05-14 12:15:46
【问题描述】:

我在网站上创建了一个自定义表单选项来更新价格并通过 ajax 发送该数据并尝试进入过滤器 woocommerce_before_calculate_totals 会话。

这是代码

add_action( 'woocommerce_before_calculate_totals', 'calculate_total_price', 99 );

function calculate_total_price( $cart_object ) 
{
        global $woocommerce;
        session_start();    
        $tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
        $target_product_id = $_SESSION['wdm_user_product_id_data'].'<br/>.';
        $_SESSION['productlist'][] =
        [
            'price' => $tac_dd_discounted_price,
            'productid' => $target_product_id
        ];
        $arrys = array_merge( $_SESSION[ "productlist" ]);
        $_SESSION[ "productlist" ] = array_unique($arrys);
        // This unique array created in seesion fro multi product which show correct data.

        $price_blank="1";
         foreach ( $cart_object->get_cart() as $cart_item ) {
               $id= $cart_item['data']->get_id();
               //$target_product_id=$arrys['productlist']['productid'];
               //$tac_dd_discounted_price=$arrys['productlist']['price'];
            if ( $id == $target_product_id ) {
                    $cart_item['data']->set_price($tac_dd_discounted_price);

                }
            else
            {
             $cart_item['data']->set_price($my_price['productlist']['price']); 
            }   

          }

}

但问题是购物车中的一种产品价格显示正确,但是当尝试添加两种产品时,Seession 变量在两个产品中附加相同的值

【问题讨论】:

  • 请编辑您的问题并澄清问题,而不是在答案线程中添加代码和解释,这不是答案。同样,当您这样做时,没有人知道,因为没有通知。您需要在答案上添加评论以通知回答者。

标签: php wordpress session woocommerce cart


【解决方案1】:

首先,您最好使用 WooCommerce 专用的 WC_Session 类,而不是使用 PHP $_SESSION

// Set the data (the value can be also an indexed array)
WC()->session->set( 'custom_key', 'value' );

// Get the data
WC()->session->get( 'custom_key' );

现在在您的代码函数中,您刚刚从 PHP Session one 产品和 one 价格中获得:

$tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
$target_product_id = $_SESSION['wdm_user_product_id_data']; // ==> Removed .'<br/>.'

相反,当购物车中有很多产品时,您应该需要获取一系列产品 ID 和价格。

另外,你不需要global $woocommerce;...

由于您没有显示所有其他相关的 JS/Ajax/PHP 代码,并且您的问题不详细,因此无法为您提供更多帮助。

【讨论】:

    【解决方案2】:

    我做到了,任何使用自定义会话获取价格的人都需要更新购物车和结帐页面,这是一个对您有很大帮助的钩子。

    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 ) {
      session_start();    
      $tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
      $target_product_id = $_SESSION['wdm_user_product_id_data'];
      if ( ! isset($tac_dd_discounted_price ) || empty ($tac_dd_discounted_price ) ) { return $woo_data; }
      $woo_data['data']->set_price( $tac_dd_discounted_price );
      $woo_data['my_price'] = $tac_dd_discounted_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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      相关资源
      最近更新 更多