久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用會話中保存的 Url 變量預填充 Woocommerce 結賬

Pre-fill Woocommerce checkout fields with Url variables saved in session(使用會話中保存的 Url 變量預填充 Woocommerce 結賬字段)
本文介紹了使用會話中保存的 Url 變量預填充 Woocommerce 結賬字段的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當人們通過銷售電子郵件中的鏈接以電子郵件和姓名作為參數進入我的 woocommerce 商店時,我想在結帳頁面中預先填寫姓名和電子郵件.

When people enter the my woocommerce shop following a link in an sales email with email and name as parameters I would like to prefill the name and email in the checkout page.

因此我創(chuàng)建了一個動作和過濾器.這按預期工作,但前提是我在銷售頁面上進行了硬刷新 (ctrl + f5)

Therefore I created an action and filter. This works as expected but only if I do a hard refresh on the sales page (ctrl + f5)

我已經從緩存和清漆緩存中排除了銷售頁面和結賬頁面,但這并沒有解決問題.

I've excluded the sales page and the checkout page from the cache and varnish cache but this didn't fix the issue.

我在這里遺漏了什么嗎?你知道為什么這只適用于硬刷新嗎?

Am I missing something here? Do you have any idea why this only works with a hard refresh?

非常感謝任何幫助.

代碼:

    function save()
    {
    if ( is_page( 'sales-page' ) )
    {
        if ( isset( $_GET['tu_em'] ) ) {
            global $woocommerce;
            $woocommerce->session->set( 'tu_em', $_GET['tu_em'] );
        }
        if ( isset( $_GET['tu_name'] ) ) {
            global $woocommerce;
            $woocommerce->session->set( 'tu_name', $_GET['tu_name'] );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'save_email' , 1100);

function override_checkout_email_field( $fields ) {
    global $woocommerce;
    $email = $woocommerce->session->get('tu_em');
    if(!is_null($email)) {
      $fields['billing']['billing_email']['default'] = $email;
    }
    $name = $woocommerce->session->get('tu_name');
    if(!is_null($name)) {
      $fields['billing']['billing_first_name']['default'] = $name;
    }
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );

推薦答案

在下面,您將找到正確的工作代碼,將來自 URL (GET) 的用戶數據保存到 WooCommerce 會話中,并使用該數據自動填充相關結帳字段.

Here below, you will find the correct working code, to save user data from an URL (GET) into WooCommerce sessions and to autofill with that data the related checkout fields.

URL 將類似于:http://example.com/sales-page/?tu_em=name@example.com&tu_name=theFirstName

這可以從任何 URL 完成,因為代碼會在設置 URL 變量時檢測它們.

This can be done from any URL as the code will detect the URL variable, when they are set.

代碼;

// Save user data from URL to Woocommerce session
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
    if ( isset( $_GET['tu_em'] ) || isset( $_GET['tu_name'] ) ) {
        $em   = isset( $_GET['tu_em'] )   ? esc_attr( $_GET['tu_em'] )   : '';
        $name = isset( $_GET['tu_name'] ) ? esc_attr( $_GET['tu_name'] ) : '';

        // Set the session data
        WC()->session->set( 'custom_data', array( 'email' => $em, 'name' => $name ) );
    }
}

// Autofill checkout fields from user data saved in Woocommerce session
add_filter( 'woocommerce_billing_fields' , 'prefill_billing_fields' );
function prefill_billing_fields ( $address_fields ) {
    // Get the session data
    $data = WC()->session->get('custom_data');

    // Email
    if( isset($data['email']) && ! empty($data['email']) )
        $address_fields['billing_email']['default'] = $data['email'];

    // Name
    if( isset($data['name']) && ! empty($data['name']) )
        $address_fields['billing_first_name']['default'] = $data['name'];

    return $address_fields;
}

代碼位于您的活動子主題(或活動主題)的 function.php 文件中.經過測試并有效.

這篇關于使用會話中保存的 Url 變量預填充 Woocommerce 結賬字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 国产成人叼嘿视频在线观看 | 求个av网址 | 欧美精品一区二区在线观看 | 久国产视频 | 久久国产精品免费 | 欧美日韩中文在线观看 | 日韩中文字幕久久 | 国产乱码精品一品二品 | 玖玖在线免费视频 | 毛片软件 | 欧美日韩大片 | 女人夜夜春 | 国产精品国产精品国产专区不卡 | 91观看 | 在线视频h | 色综合久| 久久精品免费 | 婷婷亚洲综合 | 国产一区免费 | 久久久久久久久国产 | 国产精品美女久久久久aⅴ国产馆 | 91麻豆精品国产91久久久久久久久 | 日韩精品在线网站 | 中文字幕日韩专区 | 一级毛片大全免费播放 | 国产精品一区二区三区四区 | 日本激情一区二区 | 密室大逃脱第六季大神版在线观看 | 久久成人精品视频 | 欧美日韩黄色一级片 | 亚洲播放一区 | 亚洲精品自在在线观看 | 久久久久久国产 | 日韩毛片在线视频 | 男女一区二区三区 | 成人h动漫精品一区二区器材 | 九九精品在线 | 国产精品色| 羞羞的视频网站 | 午夜性色a√在线视频观看9 | 日韩视频在线免费观看 |