在Woocommerce中在计算总计挂钩之前设置自定义计算价格 [英] Set a custom calculated price in Woocommerce Before Calculate Totals hook

查看:51
本文介绍了在Woocommerce中在计算总计挂钩之前设置自定义计算价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已使用高级座椅预订管理WooCommerce 插件,但是我遇到了定价问题,因为Minicart小部件显示商品价格除以2。

So I have setup woocommerce with Advance Seat Reservation Management for WooCommerce plugin but I'm running into pricing issues as the Minicart widget shows the item price divided by 2.

感谢任何帮助。

以下是相关的插件代码:

Here is the related involved plugin code:

add_action( 'woocommerce_before_calculate_totals', 'srw_add_custom_price' );
function srw_add_custom_price( $cart_object ){
global $wpdb;

foreach( $cart_object->cart_contents as $key => $value ){
    $proId = $value['data']->id;

    if(isset($_SESSION["seats".$proId])) {
        $seatsSec = $_SESSION["seats".$proId];
    }else{
        $seatsSec = '';
    }
    $seatsSecF = explode("@", $seatsSec);
    if(count($seatsSecF) > 0){
        $checkProfileTable = $wpdb->prefix . 'srw_seat_reservation_product';
        $profile = $wpdb->get_results("SELECT * from $checkProfileTable where proId = ".$proId);
        $fileId = $profile[0]->profileID;

        $mapTable = $wpdb->prefix . 'srw_seat_reservation_map';
        $maps = $wpdb->get_results("SELECT * from $mapTable where profileID = ".$fileId);
        $mapId = $maps[0]->profileID;

        $mapValueTable = $wpdb->prefix . 'srw_seat_reservation_map_value';
        $proValueTable = $wpdb->prefix . 'srw_seat_reservation_product_value';

        $checkConfig = $wpdb->prefix . 'srw_seat_reservation_config';
        $profile = $wpdb->get_results("SELECT * from $checkConfig");
        $pricewrong = $profile[0]->price;

        foreach($seatsSecF as $seat){
            $checkSeat = explode(".", $seat);
            $column = $checkSeat[1];
            $row = $checkSeat[0];

            $mapvalues = $wpdb->get_results("SELECT * from $mapValueTable where mapId = ".$mapId." and mapcolumn = '".$column."' and row = '".$row."'");
            $type = $mapvalues[0]->type;

            $checkPrice = $wpdb->get_results("SELECT * from $proValueTable where proId = ".$proId." and color = '".$type."'");
            $price = $checkPrice[0]->price;

            if($pricewrong == "yes"){
                if(is_user_logged_in())
                    $value['data']->price += $price / 2;
                else
                    $value['data']->price += $price;
            }else
                $value['data']->price += $price;
        }
        $value['data']->set_price( $value['data']->price );
    }
  }
}


推荐答案

您应该始终注释您的代码,因为没有人可以理解在这些SQL查询和价格计算中所执行的操作。您需要先在 woocommerce_add_cart_item_data 挂钩上完成计算。

You should always comment your code, as nobody can understand what is done in those SQL queries and price calculations. Your calculations need to be done before on woocommerce_add_cart_item_data hook.

似乎代码中存在一些错误,错误和重复。例如 $ value ['data']-> price + = $ price; 不正确,在Woocommerce 3中将不起作用。下面的代码使用正确的方法

It seems that there is some errors, mistakes and repetitions in the code. for example $value['data']->price += $price; is not correct and will not work in Woocommerce 3. The code below use the correct way to make that work.

代码:

// Calculate custom price and add it as cart item data
add_filter('woocommerce_add_cart_item_data', 'srw_add_custom_price_to_cart_item_data', 30, 2 );
function srw_add_custom_price_to_cart_item_data( $cart_item_data, $product_id ){
    global $wpdb;

    $proId = $product_id;

    if(isset($_SESSION["seats".$proId])) {
        $seatsSec = $_SESSION["seats".$proId];
    }else{
        $seatsSec = '';
    }
    $seatsSecF = explode("@", $seatsSec);
    if(count($seatsSecF) > 0){
        $checkProfileTable = $wpdb->prefix . 'srw_seat_reservation_product';
        $profile = $wpdb->get_results("SELECT * from $checkProfileTable where proId = ".$proId);
        $fileId = $profile[0]->profileID;

        $mapTable = $wpdb->prefix . 'srw_seat_reservation_map';
        $maps = $wpdb->get_results("SELECT * from $mapTable where profileID = ".$fileId);
        $mapId = $maps[0]->profileID;

        $mapValueTable = $wpdb->prefix . 'srw_seat_reservation_map_value';
        $proValueTable = $wpdb->prefix . 'srw_seat_reservation_product_value';

        $checkConfig = $wpdb->prefix . 'srw_seat_reservation_config';
        $profile = $wpdb->get_results("SELECT * from $checkConfig");
        $pricewrong = $profile[0]->price;

        $calculated_price = 0;

        foreach($seatsSecF as $seat){
            $checkSeat = explode(".", $seat);
            $column = $checkSeat[1];
            $row = $checkSeat[0];

            $mapvalues = $wpdb->get_results("SELECT * from $mapValueTable where mapId = ".$mapId." and mapcolumn = '".$column."' and row = '".$row."'");
            $type = $mapvalues[0]->type;

            $checkPrice = $wpdb->get_results("SELECT * from $proValueTable where proId = ".$proId." and color = '".$type."'");
            $price = $checkPrice[0]->price;

            $calculated_price += $price;
        }
        if( $calculated_price > 0 ){
            $cart_item_data['new_price'] = $calculated_price;
            $cart_item_data['unique_key']    = md5( microtime() . rand() );
        }
        return $cart_item_data;

    }
}

// Set the custom cart item calculated price
add_action( 'woocommerce_before_calculate_totals', 'srw_add_custom_price' );
function srw_add_custom_price( $cart_object ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart_object->get_cart() as $cart_item ) {
        if( isset($cart_item['new_price']) )
            $cart_item['data']->set_price( $cart_item['new_price'] );
    }
}

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试,可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce中在计算总计挂钩之前设置自定义计算价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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