在Woocommerce Before Calculate Totals hook

时间:2018-06-06 19:37:29

标签: php wordpress woocommerce cart hook-woocommerce

因此,我使用Advance Seat Reservation Management for WooCommerce插件设置了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 );
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您应始终对代码进行注释,因为没有人能够理解在这些SQL查询和价格计算中所做的工作。您的计算需要在woocommerce_add_cart_item_data挂钩之前完成。

似乎代码中存在一些错误,错误和重复。例如$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文件中。经过测试和工作。