简体   繁体   中英

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

I am writing custom woocomerce plugin I have list of Items on a page each has its own add button when I press that in session array I store the that and want to show that that in cart page in one row table like woocommerce plugin does my code is bellow I don't know how to redirect to other page after click in add button. //items list page: when some one click on add button I want to redirect to cart page.

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

Your code is not really testable as wp_orderlist is a custom table and $cat is not defined in your code. So I have tried with simulated fake data and I have set some code in two functions that will:

  • Get the necessary data from wp_orderlist custom table
  • Set the chosen option data in sessions and redirect to cart.

Display: Your page code will be:

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

Functions: (code goes in function.php file of your active child theme (or active theme) :

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

Tested and works with some simulated fake database table data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM