设置和使用来自自定义 table 重定向到购物车的会话数据

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

我正在编写自定义 woocomerce 插件我在页面上有项目列表,当我在会话数组中按下它时,每个项目都有自己的添加按钮我存储它并想在购物车页面中显示它在一行中table 就像 woocommerce 插件一样,我的代码如下 我不知道如何在单击添加按钮后重定向到其他页面。 //项目列表页面:当有人点击添加按钮时,我想重定向到购物车页面。

<?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.  

您的代码并不是真正的 testable,因为 wp_orderlist 是自定义的 table 并且 $cat 未在您的代码中定义。所以我尝试了模拟假数据,并在两个函数中设置了一些代码:

  • wp_orderlist 自定义中获取必要的数据 table
  • 在会话中设置所选选项数据并重定向到购物车。

显示: 您的页面代码将是:

<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();
        }
    }
}

测试并使用一些模拟的假数据库 table 数据。