WordPress教程

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.  
  15. 		if($variationID == 0){ //普通产品
  16. 			$thumbnailID = get_post_meta( $productID, '_thumbnail_id', true);
  17. 			$attachment = wp_get_attachment_image_src($thumbnailID, 'woocommerce_thumbnail' );
  18. 			$product = wc_get_product($productID);
  19. 			$stock = $product->get_stock_quantity();
  20. 		} else { //可变产品
  21. 			$variation = new WC_Product_Variation( $variationID );
  22. 			$image_id = $variation->get_image_id();
  23. 			$attachment = wp_get_attachment_image_src($image_id, 'woocommerce_thumbnail' );
  24. 			$stock = $variation->get_stock_quantity();
  25. 		}
  26. 		if($attachment){
  27. 			$image = $attachment[0];
  28. 		} else {
  29. 			$image = get_template_directory_uri()."/images/logo.png";
  30. 		}
  31.  
  32. 		$product_name = get_the_title($cart_item['product_id']);
  33.  
  34. 		//整理影响变量的属性字段
  35. 		$attr_arr = [];
  36. 		if($variationID){
  37. 			$variation = wc_get_product($variationID);
  38. 			foreach( $cart_item['variation'] as $key => $value ){
  39. 				$tax_slug = str_replace('attribute_','', $key);
  40. 				$tax = get_taxonomy( $tax_slug );
  41. 				if($tax){
  42. 					$tax_name = $tax->labels->name; //exp "name": "产品 尺码",
  43. 				}else{
  44. 					$tax_name = urldecode($tax_slug);
  45. 				}
  46. 				$tax_name = str_replace('产品 ','', $tax_name);
  47.  
  48. 				$term = get_term_by('slug', $value, $tax_slug);
  49. 				if($term){
  50. 					$term_name = $term->name;
  51. 				}else{
  52. 					$term_name = $value;
  53. 				}
  54.  
  55. 				$attr = array(
  56. 					'name'=> $tax_name,
  57. 					'value' => $term_name
  58. 				);
  59. 				array_push( $attr_arr, $attr);
  60. 			}
  61. 		}
  62.  
  63. 		$api_item = array(
  64. 			'product_image' => $image,
  65. 			'product_name' => $product_name,
  66. 			'product_id'   => $productID,
  67. 			'variation_id' => $variationID,
  68. 			'quantity'     => $cart_item['quantity'],
  69. 			'attributes'   => $attr_arr,
  70. 			'item_taxes'   => $cart_item['line_tax_data'],
  71. 			'subtotal_tax' => $cart_item['line_subtotal_tax'],
  72. 			'total_tax'    => $cart_item['line_tax'],
  73. 			'subtotal'     => $cart_item['line_subtotal'],
  74. 			'total'        => $cart_item['line_total'],
  75. 			'stock'        => $stock
  76. 		);
  77.  
  78. 		array_push( $resaults, $api_item);
  79. 	}
  80. 	return $resaults;
  81. }

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

已有 402 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!