当前位置:首页文章笔记建站教程WooCommerce购物车对象使用以及方法函数概括

WooCommerce购物车对象使用以及方法函数概括

WooCommerce 二次开发中,经常会需要对购物车进行改造,甚至有时候会需要重写购物车页面,所以就有必要把 WooCommerce 的购物车提供的接口方法做一下整理。本文对我在最近的一些项目中使用过的方法进行简要的记录。

首先,在调用任何购物车方法之前,先要检查当前页面环境对购物车对象是否可用:

  1. if ( is_null( WC()->cart ) ) {
  2. wc_load_cart();
  3. }
  4. WC()->cart->get_cart();

常用的条件函数,返回 true/false

  1. //检查购物车是否有商品
  2. WC()->cart->is_empty();
  3. //检查购物车是否需要付费,如果费用为0则返回false
  4. WC()->cart->needs_payment();
  5. //检查购物车中是否已经记录收货地址
  6. WC()->cart->show_shipping();
  7. //检查是不是需要寄送(用于计算运费的情况)
  8. WC()->cart->needs_shipping();
  9. //检查是不是有折扣,如果后台减了价格,这里会返回true
  10. WC()->cart->has_discount();

获取数据

  1. /* Author: Brain – blog.brain1981.com */
  2. //返回购物车商品总数
  3. WC()->cart->get_cart_contents_count();
  4. //返回购物车小计
  5. WC()->cart->get_cart_subtotal();
  6. //返回总运费
  7. WC()->cart->get_shipping_total();
  8. //返回使用的优惠券,返回数组,内容包含优惠券对象和优惠码
  9. WC()->cart->get_coupons();
  10. //返回使用的优惠券,返回数组,内容仅包含优惠码
  11. WC()->cart->get_applied_coupons();
  12. 返回指定优惠码在当前购物车中获得的折扣金额
  13. WC()->cart->get_coupon_discount_amount( ‘coupon_code’ );
  14. //返回总折扣金额,这俩其实等于同一个方法
  15. WC()->cart->get_discount_total();
  16. WC()->cart->get_cart_discount_total();
  17. //返回购物车总金额,包含了折扣和运费
  18. WC()->cart->get_total();
  19. WC()->cart->tota;

获取用户的地址信息

  1. /* Author: Brain – blog.brain1981.com */
  2. //获取用户对象
  3. WC()->cart->get_customer();
  4. //获取用户的地址信息
  5. WC()->cart->get_customer()->get_billing_first_name();
  6. WC()->cart->get_customer()->get_billing_last_name();
  7. WC()->cart->get_customer()->get_billing_company();
  8. WC()->cart->get_customer()->get_billing_email();
  9. WC()->cart->get_customer()->get_billing_phone();
  10. WC()->cart->get_customer()->get_billing_country();
  11. WC()->cart->get_customer()->get_billing_state();
  12. WC()->cart->get_customer()->get_billing_postcode();
  13. WC()->cart->get_customer()->get_billing_city();
  14. WC()->cart->get_customer()->get_billing_address();
  15. WC()->cart->get_customer()->get_billing_address_2();
  16. WC()->cart->get_customer()->get_shipping_first_name();
  17. WC()->cart->get_customer()->get_shipping_last_name();
  18. WC()->cart->get_customer()->get_shipping_company();
  19. WC()->cart->get_customer()->get_shipping_country();
  20. WC()->cart->get_customer()->get_shipping_state();
  21. WC()->cart->get_customer()->get_shipping_postcode();
  22. WC()->cart->get_customer()->get_shipping_city();
  23. WC()->cart->get_customer()->get_shipping_address();
  24. WC()->cart->get_customer()->get_shipping_address_2();

常用方法

  1. /* Author: Brain – blog.brain1981.com */
  2. //添加指定的产品到购物车,如果是添加普通产品只需要$product_id和$quantity即可,添加可变产品比较复杂,会另外写博客介绍
  3. WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation);
  4. //修改购物车中商品的数量
  5. WC()->cart->set_quantity( $item_key, $quantity );
  6. //删除购物车中的商品
  7. WC()->cart->remove_cart_item( $item_key );
  8. //使用优惠券,参数就是优惠码
  9. WC()->cart->apply_coupon( $coupon_code );
  10. //删除优惠券,参数也是优惠码
  11. WC()->cart->remove_coupon( $coupon_code );
  12. //删除所有的优惠券
  13. WC()->cart->remove_coupons();
  14. //重新计算购物车价格
  15. WC()->cart->calculate_totals();

方法中,有一些注意点,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,可通过以下方法获得$item_key

  1. $product_cart_id = WC()->cart->generate_cart_id( $product_id );
  2. $item_key = WC()->cart->find_product_in_cart( $product_cart_id );

对可变商品,已知$variation_id,则是通过遍历方法获取

  1. /* Author: Brain – blog.brain1981.com */
  2. foreach ( WC()->cart->get_cart() as $item_key => $item ) {
  3. // If the targeted variation id is in cart
  4. if ( $item[‘variation_id’] == $variation_id ) {
  5. $item_key
  6. break;
  7. }
  8. }

通过以上总结的常用方法组合,我们大致就可以开发出自己的购物车程序了,列举一个常用的列出购物车商品清单的函数:

  1. /* Author: Brain – blog.brain1981.com */
  2. function brain1981_rest_wc_cart_list($request = null) {
  3. if ( is_null( WC()->cart ) ) {
  4. wc_load_cart();
  5. }
  6. WC()->cart->get_cart();
  7. $resaults = [];
  8. if( WC()->cart->is_empty() ){//如果没有物品则直接返回
  9. return $resaults;
  10. }
  11. foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  12. $productID = $cart_item[‘product_id’];
  13. $variationID = $cart_item[‘variation_id’];
  14. if($variationID == 0){ //普通产品
  15. $thumbnailID = get_post_meta( $productID, ‘_thumbnail_id’, true);
  16. $attachment = wp_get_attachment_image_src($thumbnailID, ‘woocommerce_thumbnail’ );
  17. $product = wc_get_product($productID);
  18. $stock = $product->get_stock_quantity();
  19. } else { //可变产品
  20. $variation = new WC_Product_Variation( $variationID );
  21. $image_id = $variation->get_image_id();
  22. $attachment = wp_get_attachment_image_src($image_id, ‘woocommerce_thumbnail’ );
  23. $stock = $variation->get_stock_quantity();
  24. }
  25. if($attachment){
  26. $image = $attachment[0];
  27. } else {
  28. $image = get_template_directory_uri()."/images/logo.png";
  29. }
  30. $product_name = get_the_title($cart_item[‘product_id’]);
  31. //整理影响变量的属性字段
  32. $attr_arr = [];
  33. if($variationID){
  34. $variation = wc_get_product($variationID);
  35. foreach( $cart_item[‘variation’] as $key => $value ){
  36. $tax_slug = str_replace(‘attribute_’,, $key);
  37. $tax = get_taxonomy( $tax_slug );
  38. if($tax){
  39. $tax_name = $tax->labels->name; //exp "name": "产品 尺码",
  40. }else{
  41. $tax_name = urldecode($tax_slug);
  42. }
  43. $tax_name = str_replace(‘产品 ‘,, $tax_name);
  44. $term = get_term_by(‘slug’, $value, $tax_slug);
  45. if($term){
  46. $term_name = $term->name;
  47. }else{
  48. $term_name = $value;
  49. }
  50. $attr = array(
  51. ‘name’=> $tax_name,
  52. ‘value’ => $term_name
  53. );
  54. array_push( $attr_arr, $attr);
  55. }
  56. }
  57. $api_item = array(
  58. ‘product_image’ => $image,
  59. ‘product_name’ => $product_name,
  60. ‘product_id’ => $productID,
  61. ‘variation_id’ => $variationID,
  62. ‘quantity’ => $cart_item[‘quantity’],
  63. ‘attributes’ => $attr_arr,
  64. ‘item_taxes’ => $cart_item[‘line_tax_data’],
  65. ‘subtotal_tax’ => $cart_item[‘line_subtotal_tax’],
  66. ‘total_tax’ => $cart_item[‘line_tax’],
  67. ‘subtotal’ => $cart_item[‘line_subtotal’],
  68. ‘total’ => $cart_item[‘line_total’],
  69. ‘stock’ => $stock
  70. );
  71. array_push( $resaults, $api_item);
  72. }
  73. return $resaults;
  74. }
温馨提示:

文章标题:WooCommerce购物车对象使用以及方法函数概括

文章链接:https://www.wuyanshuo.cn/808.html

更新时间:2022年05月25日

本站资源均为两层压缩,第一层7z(后缀若为wys,请自行修改为7z)有解压密码;第二层zip或cbz,无解压密码,可直接使用漫画类软件程序查看;详情可参考解压教程

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

给TA打赏
共{{data.count}}人
人已打赏
建站教程

WordPress 创建自定义文章类型设置固定链接

2022-5-25 13:59:45

建站教程

WordPress禁止生成缩略图一段代码搞定

2022-5-25 16:58:15

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'