![](/img/trans.png)
[英]How to add custom plugin data to the cart in woocommerce using custom button on product page?
[英]WordPress bring session data from product page to cart page in custom plugin?
我列出了所有產品的產品頁面,所有產品都有添加按鈕,單擊它們應將會話數組中與產品相關的數據提取到購物車頁面中,並像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一樣處理得很好。
在沒有購物車頁面輸出的情況下,我什么都沒得到,任何選擇的產品都會拿起表中的最后一個項目,並在購物車頁面中向我顯示一個行表,以選擇刪除項目或繼續進行結帳。
@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'] );
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.