簡體   English   中英

設置和使用重定向到購物車的自定義表中的會話數據

[英]Set and use session data from a custom table redirecting to cart

我正在編寫自定義 woocomerce 插件我有一個頁面上的項目列表,當我在會話數組中按下它時,每個項目都有自己的添加按鈕如下所示,單擊添加按鈕后,我不知道如何重定向到其他頁面。 //項目列表頁面:當有人點擊添加按鈕時,我想重定向到購物車頁面。

<?php
function register_session(){
    if(!session_id()) session_start();
}
add_action('init','register_session');
?>
 <table border="1">
   <tr>
     <th>Item_ID</th>   
     <th>Item Description</th>  
     <th>Packing Size</th>  
     <th>Cart</th>
   </tr>
 <?php
 $result1 = $wpdb->get_results ( "SELECT * FROM wp_orderlist where 
 category_id = $cat ");
 foreach ( $result1 as $print1 ) {
    echo '<tr>';
    echo '<td>'. $print1->item_id.'</td>';
    echo '<td>'. $print1->Item_Description.'</td>';
    echo '<td>'. $print1->Packing.'</td>';
    echo '<td> <form method="post"> <input type="submit" name="add" 
    href="$print1->item_id" value="ADD"></form> </td>';
    echo '</tr>';
 }
 echo '</tr> ';
 ?>            

 </table>
 </div>
 <?php } 

 if (isset($_POST['add']))
   {
     $cart = array (
     'oid' => $print1->item_id,
     'des' => $print1->Item_Description,
     'pack' =>  $print1->Packing
     );
     $_SESSION['cart'][] = $cart;

     print_r($_SESSION['cart']);
   }
   ?>
 //I place this code on cart page to show data in array just for testing 
 //I am getting empty array.

 <?php
 $cart = ! empty( $_SESSION['cart'] ) ? $_SESSION['cart'] : false;

 $_SESSION['cart'][] = $cart;

 print_r($_SESSION['cart']);
 exit;
 ?>
 // I want automatic redirection to cart and want to show session 
 //     data in one row table.  

您的代碼並不是真正可測試的,因為wp_orderlist是一個自定義表並且$cat未在您的代碼中定義。 因此,我嘗試使用模擬假數據,並在兩個函數中設置了一些代碼:

  • wp_orderlist自定義表中獲取必要的數據
  • 在會話中設置所選選項數據並重定向到購物車。

顯示:您的頁面代碼將是:

<div> <?php // Missing opening div tag 
?>
    <table border="1">
        <tr>
            <th><?php _e("Item id","woocommerce"); ?></th>
            <th><?php _e("Item Description","woocommerce"); ?></th>
            <th><?php _e("Packing Size","woocommerce"); ?></th>
            <th><?php _e("Action","woocommerce"); ?></th>
        </tr>
    <?php
    foreach ( get_packing( $cat ) as $result ) : ?>
        <tr>
            <td><?php echo $result->item_id; ?></td>
            <td><?php echo $result->Item_Description; ?></td>
            <td><?php echo $result->Packing; ?></td>
            <td> <a class="button alt" href="?cat=<?php echo $cat . '&packid=' . $result->item_id; ?>"><?php _e("Add","woocommerce"); ?></a></td>
        </tr>
    <?php endforeach; ?>
    </table>

</div>

功能:(代碼位於您的活動子主題(或活動主題)的 function.php 文件中

// Utility function to get the data from "wp_orderlist" table
function get_packing( $cat, $id = '' ){
    global $wpdb;

    if( empty($id) ) {
        // Get the results from the "category_id"
        return $wpdb->get_results( "SELECT * FROM $wpdb->orderlist WHERE category_id = '$cat'");
    } else {
        // Get the row from the "category_id" and the "item_id"
        return $wpdb->get_row( "SELECT * FROM $wpdb->orderlist WHERE category_id = '$cat' and item_id = '$id'");
    }
}

// Set the chosen packing option data in session and redirect to cart page
add_action('template_redirect', 'grab_packing_option');
function grab_packing_option(){
    if(session_id() == '' )
        session_start();

    if( isset( $_GET['cat'] ) && isset( $_GET['packid'] ) && ! isset($_SESSION['packing_option']) ){
        $result = get_packing( $_GET['cat'], $_GET['packid'] );

        // Set the chosen packing option data in session and redirect to cart page
        if( $result->item_id == $_GET['packid'] && ! is_cart() ) {
            $_SESSION['packing_option'] = $result; // Set data in session
            wp_redirect( wc_get_cart_url() ); // Redirect to cart page
            exit();
        }
    }
}

測試並使用一些模擬的假數據庫表數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM