简体   繁体   English

WooCommerce记住结帐字段

[英]WooCommerce Remember Checkout Fields

I have a page where I send hidden form field values to the WooCommerce checkout page. 我有一个页面,我将隐藏的表单字段值发送到WooCommerce结帐页面。

$my_form = '<form action="/checkout" method="post">';
$m_form .= '<input type="hidden" name="company" value="<%= company %>" />';
$my_form .= '<input type="hidden" name="address" value="<%= address %>" />';
$my_form .= '<input type="hidden" name="address2" value="<%= address2 %>" />';
$my_form .= '<input type="submit" name="mysubmit" Value="Send to checkout">';
$my_form .= '</form>';

On the checkout page, I read these values and populate the shipping fields. 在结帐页面上,我阅读了这些值并填充了运送字段。 This works good. 这很好。

add_filter('woocommerce_checkout_get_value', 'populate_checkout_fields', 10, 2);
function populate_checkout_fields($value, $input) {
    $company = !empty($_POST['company']) ? $_POST['company'] : '';
    $address = !empty($_POST['address']) ? $_POST['address'] : '';
    $address_2 = !empty($_POST['address2']) ? $_POST['address2'] : '';

    $checkout_fields = array(
        'shipping_company'    => $company,
        'shipping_address_1' =>  $address,
        'shipping_address_2' =>  $address_2
    );

    foreach ($checkout_fields as $key_field => $field_value) {
        if ($input == $key_field && !empty($field_value)) {
            $value = $field_value;
        }
    }

    return $value;
}

The problem I am having is that if the user leaves the checkout page and then comes back, the shipping fields are blank again or show the default values if logged in. I need the shipping fields to be blank on first load and then save whatever values get put into those fields, either by the user typing or by using my form page. 我遇到的问题是,如果用户离开结帐页面然后又回来,则送货字段再次为空,或者如果登录则显示默认值。我需要在首次加载时将送货字段为空,然后保存任何值可以通过用户输入或使用我的表单页面输入到这些字段中。

A possible solution can be with the customer session. 可能的解决方案可以是与客户会话。 When you use your hook, try to save the values in their session like this: 使用挂钩时,请尝试将值保存在其会话中,如下所示:

WC()->customer->set_shipping_address_1($address);
WC()->customer->set_shipping_address_2($address_2);
WC()->customer->set_shipping_company($company);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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