Answer the question
In order to leave comments, you need to log in
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;
?>
<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>
<?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'];
// }
?>
Answer the question
In order to leave comments, you need to log in
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.
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>
<?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;
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question