Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 设置并使用自定义表中重定向到购物车的会话数据_Php_Wordpress_Session_Woocommerce_Cart - Fatal编程技术网

Php 设置并使用自定义表中重定向到购物车的会话数据

Php 设置并使用自定义表中重定向到购物车的会话数据,php,wordpress,session,woocommerce,cart,Php,Wordpress,Session,Woocommerce,Cart,我正在编写自定义Woocomece插件我在一个页面上有一个项目列表,每个项目都有自己的添加按钮,当我在会话数组中按下该按钮时,我将其存储在购物车页面的一行表中,就像Woocomece插件一样。我的代码如下,我不知道在单击添加按钮后如何重定向到其他页面。 //项目列表页面:当有人点击添加按钮时,我想重定向到购物车页面 <?php function register_session(){ if(!session_id()) session_start(); } add_action('

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

<?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\u orderlist
是一个自定义表,并且
$cat
未在代码中定义。因此,我尝试了模拟假数据,并在两个函数中设置了一些代码,它们将:

  • 从自定义表中获取必要的数据
  • 在会话中设置所选选项数据并重定向到购物车
显示:您的页面代码将是:

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

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

使用
session_start()
在WordPress中,您要访问会话数据的每个页面上,我们不能使用会话_start();我只是像这样注册了会话功能:读:我正在处理插件而不是主题,我在下一页得到了一个空数组,我想访问表中的会话数据。我从头开始编写插件,就像迷你woocommerce插件一样,我没有使用woocommerce的任何挂钩,我的插件与woocommerce无关,那么我应该在我的插件中使用哪些代码呢?我还没有安装woocommerce插件。@MohammadSulaiman那么你为什么要给你的问题加上woocommerce的标签(见标签“wordpress”“php”)?你开始说:“我正在编写自定义woocomerce插件”…对不起,英语不好,这是错误的意思,如WoocmercePlease帮助我,如果不完成此插件,我将被解雇90%已完成,现在我真的需要自定义购物车在我的插件中,我会为你祈祷,如果你帮助我。好的,我现在就试试,但看看ny cide,还有一件事,当我从老师那里学到一个词时,我尊敬他我的一生。
// 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();
        }
    }
}