我有产品页面列出所有产品都有添加按钮,当它点击它应该拿起会话阵列中的产品相关数据带入购物车页面并在购物车页面中显示如woocommmerce所做的。
这是定制的独立迷你插件。
我的代码和输出是下面的。
2个自定义表:
wp_productlist
wp_product_category。
在评论中我已经解释了关于代码的所有事情:
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * from wp_product_category "); //Select all data from wp_product_category which is item_id and category columns.
//main loop
foreach ( $result as $print){ // loop the wp_product_category table.
$cat = $print->item_id; ?> //pick item_id store in $cat variable.
<button class="collapsible"><?=$print->category?></button>
//show the category name in accordion through loop.
<div class="content">
<table border="1"> // table is start here.
<tr>
<th>Item_ID</th>
<th>Item Description</th> // three columns of table
<th>Packing Size</th>
<th>Cart</th> //Custom column for add to cart button.
</tr>
<?php
$result1 = $wpdb->get_results ( "SELECT * FROM wp_productlist where
category_id = $cat "); //Select everthing from wp_productlist tabl where category_id = $cat. (above $cat= item_id).
foreach ( $result1 as $print1 ) { //Nested loop to show data under category in tables.
echo '<tr>';
echo '<td>'. $print1->item_id.'</td>';
echo '<td>'. $print1->Item_Description.'</td>';// showing data from wp_productlist
echo '<td>'. $print1->Packing.'</td>';
echo '<td> <form method="post"> <input type="submit" name="add" href="$print1->item_id" value="ADD"></form> </td>'; // add to cart button its not taking id to cart page.
echo '</tr>';
}
echo '</tr> ';
?>
</table> //table end here
</div>
<?php }
if (isset($_POST['add'])) // when press add to cart button.
{
//Store the related row data in session array which is store but the last one row of the table not the one I click.
$_SESSION['cart_items'] = array(
'oid' => $print1->item_id,
'des' => $print1->Item_Description,
'pack' => $print1->Packing
) ;
$cart_items = ! empty( $_SESSION['cart_items'] ) ? // show data for testing.
$_SESSION['cart_items'] : false;
print_r($cart_items);
}
?>
产品列表页面输出
现在当有人按下添加按钮时,我希望从产品列表页面重定向数据,并希望在购物车页面中显示像woocommerce这样做,但我的插件没有与woocommerce集成,它是独立的自定义插件。
购物车页面代码在购物车页面中显示,它显示会话数据,但我表格中的最后一行,我想显示行,只需点击添加到购物车按钮,显示上面的输出
单击表中的“添加到购物车”按钮后,它不会重定向到购物车页面,请重定向。
用我的表格行显示像woocommerce购物车页面这样的数据
<?php
$cart_items = ! empty( $_SESSION['cart_items'] ) ? // accessing the data in the cart page
$_SESSION['cart_items'] : false;
print_r($cart_items);
?>
我希望这个数据在这个购物车页面中排成一行,就像woocommerce做得好。
输出CART页面,但我收到错误的数据无关紧要产品有哪些选择它将在表格中取出最后的项目,并在一个表格中显示我在一个表格中删除项目或继续检查的选项。
@kashalo这家伙是惊人的给出正确的答案但现在看不到因此我发布它来帮助其他人::
<?php
$result1 = $wpdb->get_results( "SELECT * FROM wp_ORDERlist where category_id
= $cat " );
?>
<div class="content">
<table border="1">
<tr>
<th>Item_ID</th>
<th>Item Description</th>
<th>Packing Size</th>
<th>Cart</th>
</tr>
<?php
foreach ( $result1 as $print1 ) { //Nested loop to show data under category in
tables.
echo "<form method='post'><tr><td><input type='hidden' name='id'
value='{$print1->item_id}'>{$print1->item_id}</td>";
echo "<td><input type='hidden' name='Desc' value='{$print1-
>Item_Description}'>
{$print1->Item_Description}</td>";
echo "<td><input type='hidden' name='size' value='{$print1->Packing}'>
{$print1-
>Packing}</td>";
echo '<td><input type="submit" name="add" value="ADD"></td> </form></tr>';
}
?>
</table>
</div>
<?php
if ( isset( $_POST['add'] ) ) {
$data = array(
'oid' => $_POST['id'],
'des' => $_POST['Desc'],
'pack' => $_POST['size'],
);
$cart_items = ! empty( $_SESSION['cart'] ) ? $_SESSION['cart'] : false;
if ( ! is_array( $_SESSION['cart'] ) ) { $_SESSION['cart'] = []; }
array_push( $_SESSION['cart'], $data );
echo '<pre>';
print_r( $_SESSION['cart'] );
}
}
?>