admin管理员组

文章数量:1388664

i create a custom form at a custom template file domain.php where i put three input hidden fields showing you that form :

<?php
/* Template Name: domain-checker */
 echo get_header();
 $_product = wc_get_product( $_GET['addtocart'] );

 // session_start();
 // print_r($_SESSION);
    // die;
?>

<div class="container">
    <div class="row">
        <div class="col-md-2"> </div>
         <div class="col-md-8">
            <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="POST">
                I will use my existing domain and update my nameservers www:<input type="text" class="form-control" name="damain_name" id="damain_name">

                <input type="hidden" name="product_name" value="<?php echo $_product->name; ?>" id="product_name">
                <input type="hidden" name="product_id" value="<?php echo $_GET['addtocart']; ?>" id="product_id">
                <input type="hidden" name="product_price" value="<?php echo $_product->price; ?>" id="product_price">
                <input type="hidden" name="action" value="contact_form">
                <input type="submit" name="submit" class="form-control btn-primary" value="SUBMIT">
            </form>
        </div>
        <div class="col-md-2"> </div>
        </div>
    </div>

and while this is the code what i am written in functions.php file:

/*===========================ADDING CUSTOM DATA====================*/
add_action( 'admin_post_nopriv_contact_form', 'prefix_send_email_to_admin' );
function prefix_send_email_to_admin() {

    //Custom data 
    $product_id = $_POST['product_id']; //This is product ID
    $damain_name =  $_POST['damain_name']; //This is User custom value sent via AJAX
    $action = $_POST['action'];
    $product_name = $_POST['product_name'];
    $product_price = $_POST['product_price'];
    $submit = $_POST['submit'];
    session_start();
    $_SESSION['product_id'] = $product_id;
    $_SESSION['damain_name'] = $damain_name;
    $_SESSION['action'] = $action;
    $_SESSION['product_name'] = $product_name;
    $_SESSION['product_price'] = $product_price;
    $_SESSION['submit'] = $submit;
    die();
    //session_destroy();

}
add_action('init', 'dump_woocommerce_cart');
function dump_woocommerce_cart() {
    global $woocommerce;
    var_dump(WC()->cart->get_cart());
     // session_start();
    // echo "this is session data";
    //print_r($woocommerce->cart->get_cart());
    // session_destroy();
}

// step 2==================================We are adding item in WooCommerce session with, wdm_user_custom_data_value name=============== //
add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,2);
if(!function_exists('wdm_add_item_data'))
{
    function wdm_add_item_data($cart_item_data,$product_id)
    {

        global $woocommerce;
        session_start();
        $data = array();
        if (isset($_SESSION['product_id'])) {
            $option1 = $_SESSION['product_id'];
            $cart_item_data['product_id'] =  $option1;
        }
        if (isset($_SESSION['damain_name'])) {
            $option2 = $_SESSION['damain_name'];
            $cart_item_data['damain_name'] =  $option2;
        }
        if (isset($_SESSION['action'])) {
            $option3 = $_SESSION['action'];
            $cart_item_data['action'] =  $option3;
        }
        if (isset($_SESSION['product_name'])) {
            $option4 = $_SESSION['product_name'];
            $cart_item_data['product_name'] =  $option4;
        }
        if (isset($_SESSION['product_price'])) {
            $option5 = $_SESSION['product_price'];
            $cart_item_data['product_price'] =  $option5;
        }

                    return $cart_item_data ;
                    unset($_SESSION['product_id']);
                    unset($_SESSION['damain_name']);
                    unset($_SESSION['action']);
                    unset($_SESSION['product_name']);
                    unset($_SESSION['product_price']);

        }




        //Unset our custom session variable, as it is no longer needed.


}

// step 3==========Add the custom data to WooCommerce cart object=====/
add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
    function wdm_get_cart_items_from_session($item,$values,$key)
    {


        if (array_key_exists( 'product_id', $values ) )
        {
            $item['product_id'] = $values['product_id'];
        }
        if (array_key_exists( 'damain_name', $values ) )
        {
            $item['damain_name'] = $values['damain_name'];
        }
        if (array_key_exists( 'action', $values ) )
        {
            $item['action'] = $values['action'];
        }
        if (array_key_exists( 'product_name', $values ) )
        {
            $item['product_name'] = $values['product_name'];
        }
        if (array_key_exists( 'product_price', $values ) )
        {
            $item['product_price'] = $values['product_price'];
        }
        return $item;

    }
}

i want to print_r my custom data at cart page so that i can ensure that the custom data is in woocommerce session at cart page .. your any help to be helpful for me..

本文标签: pluginsCustom form data not displaying in cart page