G
G
Gleb Lukin2020-04-28 04:11:32
Sessions
Gleb Lukin, 2020-04-28 04:11:32

How to add an image to cart in Wordpress (without plugins)?

Please help me add an image to the cart in Wordpress using the POST method. Perhaps I am "reinventing the wheel", but still I want to solve this problem in principle.

On each page, the image is retrieved from the post through the ID of the post itself:

<?php
                session_start();
                    // Получим ID вложения поста
                    //$id = the_id();
                    $attachment_image = get_children( array(
                    	//'numberposts' => 3,
                    	'post_mime_type' => 'image',
                    	'post_parent' => $id,
                    	'post_type' => 'attachment'
                    ) );
                    // вынимаем первую картинку из массива
                    $attachment_image = array_shift($attachment_image);
                    $img = '<img src="'.wp_get_attachment_url( $attachment_image->ID).'" height="" width="150" alt=""/>';
                    $_SESSION['img'] = $img;
?>


Further, on the same page, we send it for processing to the cart.php file:

<form method="post" class="addtocart" action=".../cart.php">
                    Цена товара: <?=$_SESSION['price']." за 1 единицу"?><br/>
                    Изображение: <label for="picture"><?=$_SESSION['img']?></label>
                    Наименование: <input name="item" type="text" size="14"/><br/>
                    Кол-во товара: <input name="count" type="text" size="14"/><br/>
                    Добавить товар: <button type="submit" value="Добавить">Add</button>
</form>


In the same file (cart.php) we display the received data. Everything is output except for the image thumbnail.

<?PHP
        session_start();
        // Название товара
        $_SESSION['item'] = $_POST['item'];
        // Кол-во товара
        $_SESSION['count'] = $_POST['count'];
        // Цена товара
        $_SESSION['price'] = 100;
        // 
        $_SESSION['img'] = $_POST['picture'];

    
        // if(isset($_SESSION['item']) and is_numeric($_SESSION['count'])){
            echo $_SESSION['item']."<br/>";
            echo "Вы добавили товар в карзину!<br/>";
            echo "Его кол-во ".$_SESSION['count']."<br/>";
            echo "Вы должны заплатить: ".$_SESSION['count'] * $_SESSION['price']." рублей";
            echo $_SESSION['img'];
        // }
?>


Thank you in advance for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yarovikov, 2020-04-28
@websafer

First, do it humanly - pass the id of the picture, and in the basket already get its url by id.
Well, check what data you have is transmitted, perhaps they simply do not exist.

G
Gleb Lukin, 2020-04-29
@websafer

I agree, everything has changed. Got result
Sending to cart:

<?php
session_start();
$_SESSION['id'] = $id;
?>

<form name="addtocart" id="addtocart" class="addtocart" method="post">
<input type="hidden" name="id" value="<?=the_id()?>"/>
<button type="submit" value="Добавить" class="img_bottom order" id="order"><p>Добавить<br>в избранное</p></button>
</form>

Output in modal window:
<?php
            session_start();
            $_SESSION['id'] = $_POST['id']; // ID image
            $id = $_POST['id'];
            echo "Изображение добавлено!<br/><br/>";

    	    // Получим ID вложения поста 5
            $id = $_POST['id'];
            $attachment_image = get_children( array(
            	'numberposts' => 3,
            	'post_mime_type' => 'image',
            	'post_parent' => $id,
            	'post_type' => 'attachment'
            ) );
            // вынимаем первую картинку из массива
            $attachment_image = array_shift($attachment_image);
            $img = '<img src="' . wp_get_attachment_url( $attachment_image->ID).'" height="" width="150" alt=""/>';
            echo $img;
      ?>

Now it remains to figure out how to add multiple images. Now one is added and it is updated when a new one is added.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question