php WooCommerce:从Checkout页面删除不必要的账单字段。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WooCommerce:从Checkout页面删除不必要的账单字段。相关的知识,希望对你有一定的参考价值。

<?php

/**
 * WooCommerce: Remove unnecessary billing fields from Checkout page.
 *
 * Billing fields will display if cart total is greater than zero at time of
 * loading the Checkout page. If coupon is added to bring total down to zero,
 * billing fields will remain displayed unless page is also refreshed by user.
 *
 * Billing fields will always display if the order needs shipping information
 * (i.e. at least one item in the cart is not a virtual product).
 *
 * @link https://gist.github.com/cliffordp/f509d9eb82fb57d54c207fbf957808fb
 *
 * @see WC_Countries::get_address_fields
 */
add_filter( 'woocommerce_billing_fields', 'cliff_woo_hide_unnecessary_billing_fields', 20 );

function cliff_woo_hide_unnecessary_billing_fields( $address_fields ) {
	if (
		! function_exists( 'is_checkout' )
		|| ! is_checkout()
		|| ! is_array( $address_fields )
	) {
		return $address_fields;
	}

	global $woocommerce;

	$cart_total = wc_prices_include_tax()
		? $woocommerce->cart->get_cart_contents_total() + $woocommerce->cart->get_cart_contents_tax()
		: $woocommerce->cart->get_cart_contents_total();

	$cart_total = (int) $cart_total;

	// Don't remove any fields if
	if (
		// cart total is greater than zero
		0 < $cart_total
		// or we need to ship something (i.e. not virtual product)
		|| $woocommerce->cart->needs_shipping()
	) {
		return $address_fields;
	}

	/**
	 * Remove all default billing fields except First Name, Last Name, Phone,
	 * and Email address.
	 *
	 * "Additional information Order notes" field will display because it is not
	 * a billing field.
	 */
	unset( $address_fields['billing_country'] );
	unset( $address_fields['billing_company'] );
	unset( $address_fields['billing_address_1'] );
	unset( $address_fields['billing_address_2'] );
	unset( $address_fields['billing_city'] );
	unset( $address_fields['billing_state'] );
	unset( $address_fields['billing_postcode'] );

	return $address_fields;
}

以上是关于php WooCommerce:从Checkout页面删除不必要的账单字段。的主要内容,如果未能解决你的问题,请参考以下文章

php 更改YITH PayPal Express Checkout for WooCommerce的Set Express Checkout Request参数

php 删除WooCommerce Checkout页面上的内联条款和条件

php 删除WooCommerce Checkout页面上的内联条款和条件

php 删除WooCommerce Checkout页面上的内联条款和条件

php 删除WooCommerce Checkout页面上的内联条款和条件

php 删除WooCommerce Checkout页面上的内联条款和条件