当前位置: 首页 > 知识库问答 >
问题:

Woocommerce设置帐单和发货信息

潘弘博
2023-03-14

更新-简短版本:

将使用什么方法将用户账单/发货信息保存到会话中,以便客人结账?

长版本:

创建严重依赖自定义RESTAPIendpoint和ajax的自定义签出页面。我有WC()中的所有账单和发货字段-

我还通过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());
        }
    }
}

共有1个答案

邰英毅
2023-03-14

获得账单

您可以获得如下所示的账单和发货详细信息。

//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 );

希望这有帮助。

 类似资料:
  • 与随机显示产品的客户网站存在问题 “此产品目前缺货且不可用。” 问题是我们没有打开库存跟踪,所有的产品都应该一直有库存。 当我进入WordPress管理员并单击产品的更新按钮(不做任何更改)时,消息就会消失,“添加到购物车”按钮会按原样显示。不幸的是,这个问题在将来会在这些项目上重复出现,为了解决这个问题,我必须再次更新产品(同时在WooCommerce设置中单击update可以暂时解决这个问题)

  • 可能的重复: 在jasper report中将字符串格式化为货币格式 我正在使用JasperReports创建一个发票文档,该文档需要本地化并支持多种货币。 好吧,我终于想通了,但既然国防部锁定了这个问题,应该是重复的--但它不是...不管怎样,这里有一个答案:

  • 说明 用于将订单发货 请求地址 http://api.dc78.cn/Api/mall_order_deliver 请求方式 GET 请求参数 GET参数 描述 send=1, 1=发货,-1=仅修改快递单号,默认为1 POST数据 描述 order_id=订单编号 logistics_order=发货快递单号 返回 { "status": 1, "info": "发货成功" } 请求方式 INI

  • 我已经使用了一个表率运输插件建立了4个运输区域,每个区域有一个标准和第二天交货选项。 商店里的每一件商品都能在10-15天内买到,但有些产品是库存的,可以在第二天发货。如果推车中的任何产品没有库存,那么只有标准的运输选项才可用。 我很确定我需要使用'Woocommerce_Available_Shipping_Methods'过滤器,就像下面这个类似的问题/答案所示:隐藏Shipping Opti

  • 如何根据订单的发货方式自定义订单感谢页面?因此,例如,如果客户使用请求交付选项,感谢页面将显示不同的标题。

  • 我想在单产品页面上显示运输类,在从可变产品中选择一个产品之后。例如:我有一辆自行车有三种颜色(红、蓝、绿): 自行车红有航运类“免费送货” 蓝色自行车的运输类别为“额外交付” 绿色自行车的运输等级为“正常交付” 我感谢你的任何想法。