Woocommerce 设置计费和运输信息
Posted
技术标签:
【中文标题】Woocommerce 设置计费和运输信息【英文标题】:Woocommerce set billing and shipping information 【发布时间】:2019-02-14 04:36:33 【问题描述】:更新 - 短版:
将使用什么方法将用户帐单/运输信息保存到会话以供客人结帐?
长版:
创建一个严重依赖于自定义 REST API 端点和 ajax 的自定义结帐页面。我有来自WC()->checkout()->checkout_fields;
的所有计费和运输字段从一个电话返回,将其呈现给用户等等。
我还通过 API 调用返回了计算出的运费。但是,这仅在设置用户地址时才有效 - 这是预期的。
我一生都无法弄清楚我可以在 API 中调用什么方法来保存用户账单和运输信息,以便我可以计算运输成本。现在我只能获取现有用户帐户的运输信息。即使只是一个手指在正确的方向上,也可以节省我剩下的头发。
一些代码
我如何收货(没有收货地址就无法工作,无法弄清楚如何设置帐单或送货信息)
function mytheme_get_shipping()
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate )
if( WC()->session->get('chosen_shipping_methods')[0] == $method_id )
$rate_label = $rate->label; // The shipping method label name
$rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
// The taxes cost
$rate_taxes = 0;
foreach ($rate->taxes as $rate_tax)
$rate_taxes += floatval($rate_tax);
// The cost including tax
$rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;
return array('label' => $rate_label, 'total' => WC()->cart->get_cart_shipping_total());
【问题讨论】:
当有人以“客人”身份结账时,您是否无法获取账单和运输信息?您可以在这里发布您的完整代码 - 包括您在其中调用自定义 API 的“钩子”吗? @OutsourceWordPress 不,我无法设置计费和运输信息期。这就是我想要弄清楚的。没有钩子 - 将整个东西构建为带有休息 api 端点的单页应用程序,以添加/删除/更新购物车物品等。我需要创建一个休息 api 端点,我可以在其中传递计费/运输信息以保存它 -但我不知道在其中调用什么方法让他们进入会话。 有人下单时你是实时调用API吗? @OutsourceWordPress 这是我的计划...看起来我可能只需要发布帐单/运输信息并且它没有存储在会话中? 【参考方案1】:获取帐单和运输详细信息:
您可以获得如下所示的帐单和运输详细信息。
//whole customer details
print_r(WC()->customer);
//billing details
$billing = WC()->customer->get_billing();
$billing_first_name = WC()->customer->get_billing_first_name();
$billing_last_name = WC()->customer->get_billing_last_name();
$billing_company = WC()->customer->get_billing_company();
$billing_address = WC()->customer->get_billing_address();
$billing_address_1 = WC()->customer->get_billing_address_1();
$billing_address_2 = WC()->customer->get_billing_address_2();
$billing_city = WC()->customer->get_billing_city();
$billing_state = WC()->customer->get_billing_state();
$billing_postcode = WC()->customer->get_billing_postcode();
$billing_country = WC()->customer->get_billing_country();
//shipping details
$shipping = WC()->customer->get_shipping();
$shipping_first_name = WC()->customer->get_shipping_first_name();
$shipping_last_name = WC()->customer->get_shipping_last_name();
$shipping_company = WC()->customer->get_shipping_company();
$shipping_address = WC()->customer->get_shipping_address();
$shipping_address_1 = WC()->customer->get_shipping_address_1();
$shipping_address_2 = WC()->customer->get_shipping_address_2();
$shipping_city = WC()->customer->get_shipping_city();
$shipping_state = WC()->customer->get_shipping_state();
$shipping_postcode = WC()->customer->get_shipping_postcode();
$shipping_country = WC()->customer->get_shipping_country();
设置帐单和运输详细信息:
您可以设置帐单和送货详情,如下所示。
//billing details
$billing_first_name = $_POST['billing_first_name'];
$billing_last_name = $_POST['billing_last_name'];
$billing_company = $_POST['billing_company'];
$billing_address_1 = $_POST['billing_address_1'];
$billing_address_2 = $_POST['billing_address_2'];
$billing_city = $_POST['billing_city'];
$billing_state = $_POST['billing_state'];
$billing_postcode = $_POST['billing_postcode'];
$billing_country = $_POST['billing_country'];
WC()->customer->set_billing_first_name(wc_clean( $billing_first_name ));
WC()->customer->set_billing_last_name(wc_clean( $billing_last_name ));
WC()->customer->set_billing_company(wc_clean( $billing_company ));
WC()->customer->set_billing_address_1(wc_clean( $billing_address_1 ));
WC()->customer->set_billing_address_2(wc_clean( $billing_address_2 ));
WC()->customer->set_billing_city(wc_clean( $billing_city ));
WC()->customer->set_billing_state(wc_clean( $billing_state ));
WC()->customer->set_billing_postcode(wc_clean( $billing_postcode ));
WC()->customer->set_billing_country(wc_clean( $billing_country ));
//shipping details
$shipping_first_name = $_POST['shipping_first_name'];
$shipping_last_name = $_POST['shipping_last_name'];
$shipping_company = $_POST['shipping_company'];
$shipping_address_1 = $_POST['shipping_address_1'];
$shipping_address_2 = $_POST['shipping_address_2'];
$shipping_city = $_POST['shipping_city'];
$shipping_state = $_POST['shipping_state'];
$shipping_postcode = $_POST['shipping_postcode'];
$shipping_country = $_POST['shipping_country'];
WC()->customer->set_shipping_first_name(wc_clean( $shipping_first_name ));
WC()->customer->set_shipping_last_name(wc_clean( $shipping_last_name ));
WC()->customer->set_shipping_company(wc_clean( $shipping_company ));
WC()->customer->set_shipping_address_1(wc_clean( $shipping_address_1 ));
WC()->customer->set_shipping_address_2(wc_clean( $shipping_address_2 ));
WC()->customer->set_shipping_city(wc_clean( $shipping_city ));
WC()->customer->set_shipping_state(wc_clean( $shipping_state ));
WC()->customer->set_shipping_postcode(wc_clean( $shipping_postcode ));
WC()->customer->set_shipping_country(wc_clean( $shipping_country ));
创建新客户并设置帐单和运输详细信息:
您可以创建新客户,设置帐单和运输详细信息,如下所示。
//create new customer
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$user_id = wc_create_new_customer( $email, $username, $password );
//billing details
$billing_first_name = $_POST['billing_first_name'];
$billing_last_name = $_POST['billing_last_name'];
$billing_company = $_POST['billing_company'];
$billing_address_1 = $_POST['billing_address_1'];
$billing_address_2 = $_POST['billing_address_2'];
$billing_city = $_POST['billing_city'];
$billing_state = $_POST['billing_state'];
$billing_postcode = $_POST['billing_postcode'];
$billing_country = $_POST['billing_country'];
update_user_meta( $user_id, "billing_first_name", $billing_first_name );
update_user_meta( $user_id, "billing_last_name", $billing_last_name );
update_user_meta( $user_id, "billing_company", $billing_company );
update_user_meta( $user_id, "billing_address_1", $billing_address_1 );
update_user_meta( $user_id, "billing_address_2", $billing_address_2 );
update_user_meta( $user_id, "billing_city", $billing_city );
update_user_meta( $user_id, "billing_state", $billing_state );
update_user_meta( $user_id, "billing_postcode", $billing_postcode );
update_user_meta( $user_id, "billing_country", $billing_country );
//shipping details
$shipping_first_name = $_POST['shipping_first_name'];
$shipping_last_name = $_POST['shipping_last_name'];
$shipping_company = $_POST['shipping_company'];
$shipping_address_1 = $_POST['shipping_address_1'];
$shipping_address_2 = $_POST['shipping_address_2'];
$shipping_city = $_POST['shipping_city'];
$shipping_state = $_POST['shipping_state'];
$shipping_postcode = $_POST['shipping_postcode'];
$shipping_country = $_POST['shipping_country'];
update_user_meta( $user_id, "shipping_first_name", $shipping_first_name );
update_user_meta( $user_id, "shipping_last_name", $shipping_last_name );
update_user_meta( $user_id, "shipping_company", $shipping_company );
update_user_meta( $user_id, "shipping_address_1", $shipping_address_1 );
update_user_meta( $user_id, "shipping_address_2", $shipping_address_2 );
update_user_meta( $user_id, "shipping_city", $shipping_city );
update_user_meta( $user_id, "shipping_state", $shipping_state );
update_user_meta( $user_id, "shipping_postcode", $shipping_postcode );
update_user_meta( $user_id, "shipping_country", $shipping_country );
希望这会有所帮助。
【讨论】:
如何。做。我设置。他们。在。这。第一的。地点? 您只能在客人的结帐页面中检索这些数据。您是否只想在最初设置这些?你可以使用 WooCommerce 钩子吗? 我没有“结帐页面” - 我有一个单页应用程序,它使用自定义 API 端点来处理添加/删除数据和更新 UI。我有表格。我只需要将输入的数据保存到我的 API 函数中的会话中。我确实找到了WC()->checkout->process_checkout()
,它经过一些修改将获取已发布和已发布的计费/运输/付款数据并处理结帐,但我仍然希望将信息正确保存到会话
您可以用同样的方式设置帐单和发货详情。你能试试我的答案的第二部分,让我知道它是否有帮助?
如果之前没有创建客户,可以尝试第三部分。以上是关于Woocommerce 设置计费和运输信息的主要内容,如果未能解决你的问题,请参考以下文章