i need to edit woocommerce cart manually,
my function is work but it have a bug, sometimes not edit or edit empty value
this is my function :

<?php
class modElementFromCart {
    static protected function cost (){
        define('q1', "SELECT * FROM wp_woocommerce_sessions WHERE session_key = ':id'");
        define ('q2', "UPDATE wp_woocommerce_sessions SET session_value = ':sv' WHERE session_key = ':id'");
        define ('id', ':id');
        define('sv', ':sv');
    }
    protected function execute() {
        $cookie = '';
        foreach($_COOKIE as $val => $t){ if (substr($val,0,22) == "wp_woocommerce_session"){$cookie = $t; break;} }
        $cookie = explode('||', $cookie);
        return  $cookie[0];
    }
    public function exe ($a) {
        try{

            $this->cost();
            $cookie = $this->execute();     
            if (!class_exists('connect_db')){ require_once('mysql class'); }
            $c = new connect_db;
            $co = $c->getShopdatabase();
            $str = str_replace(id, $cookie, q1);
            $q = $co->prepare($str);
            $q->execute();
            if ($ar = $q->fetch(PDO::FETCH_ASSOC)){
                $dato = $ar['session_value'];
                $data = explode('"removed_cart_contents";', $dato);
                $data22 = explode('"cart_content_total";', $data[count($data)-1]);
                $data22 = $data22[count($data22)-1];
                $data2 = explode(';', $data22);
                $dat = explode(';', $data[0]);
                $path = "path";
                $prezzo = 0;
                $ttq = 0;
                $ttp = 0;
                $str = '';
                $str2 = '';
                $quant = 1;
                $pd = false;
                $pv = false;
                $change = 0;
                for ($i = 0; $i < count($dat); $i++){
                    $fat = explode(':', $dat[$i]);

                    for ($j = 0; $j < count($fat); $j++){
                        if ($fat[$j] == '"product_id"'){
                            $tv =  str_replace('i:', '',$dat[$i + 1]);
                            $pd = $tv == $a[0] ? $pd = true : false;
                        }

                        if ($fat[$j] == '"variation_id"'){
                            $vid = str_replace('i:', '',$dat[$i + 1]);
                            $pv = $pd == true && $vid == $a[1] ? true : false;
                        }

                        if ($fat[$j] == '"quantity"'){
                            if ($pd && $pv){
                                $qta = $dat[$i + 1];
                                $qta = explode(':', $qta);
                                if (is_array($qta)){
                                    $dat[$i + 1]= $a[2] == 'add' 
                                    ? 
                                        str_replace($qta[count($qta) -1], $qta[count($qta) -1] + 1,$dat[$i + 1]) 
                                    :
                                        str_replace($qta[count($qta) -1], $qta[count($qta) -1] - 1,$dat[$i + 1]);
                                    $quant =$qta[count($qta) -1];
                                }
                            }
                        }
                        if ($fat[$j] == '"line_subtotal"'){
                            if ($pd && $pv){
                                $qta = $dat[$i + 1];
                                $qta = explode(':', $qta);
                                if (is_array($qta)){
                                    $ttp = $qta[count($qta) -1];
                                    $prezzo = $ttp;
                                    $change = $a[2] == 'add'  ?  $ttp +  $ttp/$quant : $ttp - $ttp/$quant;
                                    $dat[$i + 1]= $change > 999.99 
                                    ? 
                                        str_replace($ttp,$change,$dat[$i + 1])
                                    :
                                        str_replace($ttp, $change,$dat[$i + 1]);
                                }
                            }
                        }

                        if ($fat[$j] == '"line_total"'){
                            if ($pd && $pv){
                                $qta = $dat[$i + 1];
                                $qta = explode(':', $qta);
                                if (is_array($qta)){
                                    $ttp = $qta[count($qta) -1];
                                    $prezzo = $ttp;
                                    $change = $a[2] == 'add'  ?  $ttp +  $ttp/$quant : $ttp - $ttp/$quant;
                                    $dat[$i + 1]= $change > 999.99 
                                    ? 
                                            str_replace($ttp,$change,$dat[$i + 1])
                                    :
                                        str_replace($ttp, $change,$dat[$i + 1]);
                                }
                            }
                        }
                    }
                    if (count($dat) > $i + 1){ $str .= $dat[$i] != ''  ? $dat[$i].';' : '';}
                    else{$str .= $dat[$i] != ''  ? $dat[$i] : '';}
                }
                $string = substr($str,0, strlen($str) ).'"removed_cart_contents";'.$data[1];//.'"cart_content_total";'.$str2;
                $str = str_replace(id, $cookie, q2);
                $str = str_replace(sv, $string, $str);
                $u = $co->prepare($str);
                $q = $u->execute();
                if (!$q){return 'database error';}
                return 'true';
            }
        }
        catch (Exeception $e){
            error_log(date('[Y-m-d H:i e] ') . "Error: " . $e->getMessage() . PHP_EOL, 3, "error.log");
            return $e->getMessage();
        }
    }
}

/post[a] => product_id, post[b] => variation id, post[c] => add or remove/
if (isset($_POST[‘p’]) && isset($_POST[‘a’]) && isset($_POST[‘b’]) && isset($_POST[‘c’]) && $_POST[‘p’] == ‘p’){
$a = new modElementFromCart;
print $a->exe(array($_POST[‘a’], $_POST[‘b’], $_POST[‘c’]));
}
?>

Read more here: mysql -php woocommerce cart content edit


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: