WooCommerce 二次开发中,经常会需要对购物车进行改造,甚至有时候会需要重写购物车页面,所以就有必要把 WooCommerce 的购物车提供的接口方法做一下整理。本文对我在最近的一些项/ _ F n } e 3 %目中使用过的方法进行简要的记录。
首先` G a S :,在调用任何购物L [ C + 4 f车方法之前,先要检查当前页面环境对购物车对象是否可用:
- if ( is_null( WC()->cart ) ) {
- wc_load_cart();
- }
- WC()->cart->get_cart();
常用的条件函数,返回 true/false
- //检查购物车是否有商品
- WC()->cart->is_empty(m u 4 j A 0);
- //检查购物车是否需q ) k ( \ r : ( \要付费,如果费用为0则返回false
- WC()->H . 0 6 K `;cart-Q 2 x l v ; D>neB 8 M l F | u l Beds_pas d A v % \ rymenK , 7 0 * Lt();
- //检查购物车中是否已经记录收货地址
- WC()->cart->show_shipping()- I Y c o 7;
- //检查是不是需要寄送(用于计算运费的情况)
- WC()->cart->needs_shipping();
- //检查是不是有折扣,如果后台减了价格,这里会返回true
- WC()->ck R % e p O yart->has_discount();
获取数据
- /* Author: Brain - blog.brain1981.com */
- //返回购物车商品总数
- WC3 * v p ! H r R ]()-&Z . Q e m n B n tgt;cart->get_cart_contents_count();
- //返回购物车小计
- WC()->cart->get_cart_subtotal();
- //返回总运费
- WC()->cart->get_shipping3 u , / D_total();
- //返回使用的优惠券,返回数组,内容包含优惠券对象和优惠码
- WC()->cart->get_coupons();
- //返回使用的优惠券,返回数组,内容仅包含优惠码
- WC()->card } g X 9t->get_applied_coupons();
- 返回指定优惠码在当前购物车中获得的折扣金额
- WC()->cart->get_cO . \ ^ W 2 # & uoupon_discount_amount( 'coupon_code' );
- //返回总折扣金额,这俩其实等于同一个方法
- WC()->cart->get_discount_total();
- WC()->cart-&c C ! * 2 T } l Cgt;get_cart_discount_total();
- //返回购物_ ? X t车总金额,包含了折扣和运费
- WC(3 o 8)->cart->get_total();
- WC()->cart-&2 s B A V \ J 7 ygt;tota;
获取用户的地址信息
- /* Aut: @ ihor: Brain - blog.brain1981.C g = Xcom */
- //获取用户对象
- WC()->cart->get_customer();
- //获取用户的地址信息
- WC()->cart->get_customer()->get_billi* B Lng_first_name();W S H m w / C w
- WC()->cart->get_customer()->get_billing_last_name(), E K e a 8 o e *;
- WC()->cart-&= 6 9 G W Ggt;get_customer()->get_billing_company();
- WC()->cart->gx 7 |et_customer()->get_billing_email();
- WC()->cart->^ 3 F;get_customer()->get_billing_phone();
- WC()->cart->get_customer()->get_biD K s 7 S q I olling_country();
- WC()->cart->get_cusB W Vtomer()->get_billing_state(% i L);
- WC()->( ( B j Gcart->get_customer()->_ Z } 2 B ^ n o;get_billing_postcode();
- WC()T ] V 4 \ $-&gS c o `t;cart->get_customer()Y c r ~ A ; g H->get_billing_city();
- WC()->cart->get_customer()->g. $ T .et_billing_address();
- WC()-&gu W 2t;cart->get_customer()->get_billing_addN T k x {ress_2();
- WC()->cart->get_customer()->get_ship% 9 * L U O t q gping_first_name();
- WC(= V ; p } I e)->ca_ 5 U ]rt->get_customer()->get_shipping_last_na: d ( E V ) Dme();
- WC()->cart->get_customer()->get_shipping_company()z q D = K -;
- WC()->cart->get_customer()->get_shipping_country();
- WC()->cart->get_custos C ( % K % =mer()->get_Q G z e 7 z cshipping_state();
- WC()-% O `>cart->gv P /et_customer()->get_shipping_postcode();
- WC()->cart->get_customer()->get_shipping_city();
- WC()->cart->get_customer()->get_shipping_address();
- WC()->7 V d N b : k;o ] ycart-&O [ & : c g W $ 1gt;get_customer()->get_shipping5 2 / $ ? O a z_address_2();
常用方法
- /* Author: Brain - blog.brain1981.com */
- //添加指定的产品到购物车,如果是添加普通产品只需# N [ } \ 9 e要$product_id和$quanti] O ^ : } P = uty即可,添加可变产品比较复杂,会另外写博客介绍
- WC()->cart->add_to_cart( $product_id, $qm z _ Wuantity, $variation_id, $variation);
- //修改购物车中商品的数量
- WC()->cart->set_quantity( $item_key, $quantity );
- //删除购物车中的商品
- WC()->cart->remove_cart_item( $item_key );
- //使用优惠券,参L U v f数就是优惠码
- WC()->cartM $ ( W Y |->apply_coupon( $coupon_code );
- //删除优惠券,参数也是优惠码
- WC()->cart->remove_coupon(K n : ) o N x $coupon_code );
- //删除所有的优惠券f ? Y A d w 1
- WC()->car` F ^ , I l L o .t->re$ N n ( g pmove_coupons();
- //重新计算购物车价格
- WC()->cart->calcul& = ] I ` fate/ u l O w_to: G b j e 4tals();
方法中,有一些注意点,add_to_cart、set_quantity 以及 remove_cart_item,这些对商品增减的方法执行后,购物车会自动调用 calculate_totals 计算价格。但 apply_coupon 和 remove_coupon 这些对优惠券的方法执行后,需要自己执行一遍 calculate_totals 计算价格。
此外,set_quantity 和 remove_cart_item 的参数$item_key,是当前购物车中商品对应的键值,这些键是通过 JSON 格式存储的,需要通过 WooCommerce 自己封装的方法获取:
对于普通商品,已知$product_id,_ $ E / \ : &可通过以下方法获得$item_key
- $product_cart_id = WC()->cart->generate_cart_id( $product_id );
- $item_key = WC()->0 v g %;cart-/ v 8 V 7 C M 8 ;>find_product_iR k P * & a $n_cart( $product_cart_id );
对可变商品,_ g e q Y i ~已知$variation_id,则是通过遍历方法获取
- /*9 H | M Y J T Author: Brain - blog.brain1981.com */
- foreach ( WC()N 0 _ \ K o o b w->cart->get_cart() as $item_key => $i\ W Q S t T n (te% R T L E @ J Um ) {
- //a v F X + j - { If the! T V 5 v targeted variation id is in cart
- i? O P H 7 # s % tf ( $item['variation_id'] == $variation_id )H U a F; {
- $item_key ...
- break;
- }
- }
通过以上总| N J结的常用方法组合,我们大致就可以开发出自己的购物车程序了,列举一个常用的列出购物车商品清单[ f m : ; E v Q的函数:
- /* Author: Brain - blog.bv [ ! x = o D 6 Lrain1981.com */
- function brain1981_rest_wc_cart_list($request = null) {
- if ( is_null( WC()-&L 3 * 4 K G )gt+ o R r;cart ) ) {
- wc_load_cart();
- }
- WC()->cart-&U C 5 ) Q [gt;get_cart();
- $resaults = [];
- if( WC()->cart->isz U o ) q : d ;_empty() ){//如果没有物品则直接返回
- reC t & l ? l - @ Mturn $resaults;
- }
- foreach # \ I t - p (0; WC()->cart->get_: ] = A # - [carC U C w 8 {t() as $cart_item_key => $carM + o - j & Pt_item )M [ ~ q | ] &| j , p#123;
- $0 A M e V D R 8productID = $cart_item['product_id'];
- $variationID = $cart_item['variation_id'];
- i[ R K 3 h K $f($variationID == 0y g L 3 A041;{ //普通产品
- $thumbnailID = get_post_meta( $productID, '_thumbnail_id', true);
- $attachment = wp_get_attachment_image_src($thumbnailID, 'woo~ _ ( 4 b ( j i Hcommerce_thumbnail' );
- $product = wc_get_pr\ q L V Y h \ x 1oduct($produc@ ] R q m ptID);
- $stock = $prov 3 4 S $ q W \duct->get_stock_quantity();
- } else { //可变产品
- $variation = new WC_Product_Variation( $variationID );
- $image_id = $variation->get_image_id();
- $attac7 y Y { 5 . Dhment = wp_get_attachment_image_src($image_id, 'woocommerc: { `e_thumbnail' )O T 3 , ];;
- $stock = $variation->get_stock_quantity();
- }
- if($attachment){
- $image = $attachment[0];
- } else {
- $image = get_template_i 0 ~directory_uri()."/r P 7 n o q 1 Gip 3 [mages/logo.png";
- }
- $prO ] = [ z g s Xoduct_name = get_the_title($cart_item['product_id']);
- //整理影响变量的属性字段
- $attr_arr = [];
- if($variationID){
- $variation = wc_get_pU u L troduct($variationID);
- foreach( $cart_item['variation'] as $kE L vey => $value ){
- $tax_slug = str_replace('attribute_','', $key);
- $tax = get_taxonomy( $tax_slug );
- if($tax)4 N h3;
- $tax_name = $tax->labels->name; //exp "name": "产品 尺码",
- }else\ # p } ` a3;
- $tax_name = urldecode($tax_slug);
- u w M 625;
- $tax_name = str_r9 Y seplace('产品 ','', $tax_name);
- $term = get_term_by('slug', $value, $tax_slug);
- ifk A e @ 2 ! E($term){
- $term_name = $term->name;
- }else{
- $term_name = $value;
- }
- $attr = array(
- 'name'=> $tax_name,
- 'value' => $term_name
- );
- array_pue B Qsh( $ato { q [ G Ytr_arr, $attr{ M & e = 2 c1;;
- }
- 8 9 3 _ ( A25;
- $api_item = array(
- 'product_image' =>m V L m 3 p 3 X $image,
- 'product_name' => $pro; : x u a i F 9 %duct_name,
- 'product_id' => $productID,
- 'variation_id' => $vG V 9 = w V D xariationID,
- 'quantity' => $cart_item['quantity'],
- 'attributes' => $attr_arr,
- 'item_t} s t ] eaxes' => $cart_item['line_tax_data'],
- 'subtotal_tax' => $cart_item['line_subtotal_tax'],
- 'total_tax' => $cart_item['line_ts V OaxG ( t = i x :'],
- 'subtotal' => $cart_item['line_subtotal'],
- 'total' => $cart_item['line_p + Z c E ! Btotal'&7 \ : 8 ] M ] * /#093;,
- 'stock'w - I . => $stock
- );
- array_push( $resaults, $api_item)l x } _ v 8 K 5 I;;
- }
- return $resaults;
- }