php - 在计算总计 Hook 之前在 Woocommerce 中设置自定义计算价格

标签 php wordpress woocommerce cart hook-woocommerce

所以我设置了 woocommerce Advance Seat Reservation Management for WooCommerce插件,但我遇到了定价问题,因为 Minicart 小部件显示项目价格除以 2。

感谢任何帮助。

相关涉及的插件代码如下:

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 Hook 之前完成计算。

代码中似乎有一些错误、错误和重复。例如 $value['data']->price += $price; 不正确,并且在 Woocommerce 3 中不起作用。下面的代码使用正确的方法来实现该功能。

代码:

// 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 文件中。经过测试并有效。

关于php - 在计算总计 Hook 之前在 Woocommerce 中设置自定义计算价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728270/

相关文章:

wordpress - 创建 WooCommerce 暂存站点

php - 如何使用php将上传的图像保存到文件

PHP/MySQL 多个子字符串?

php - WordPress MySQL 查询不工作但类似的查询工作

javascript - WordPress热门帖子如何显示帖子之类的类别?

mysql - SQL查询按发布日期批量删除woocommerce产品

javascript - 无法过滤jcombo中的字符

php - 如何修复通知 : Only variable references should be returned by reference in

php - WordPress 开发工作流程速成类(class)

php - 根据选择的运输有条件地隐藏 WooCommerce 中的“结账”字段