This is a public snippet

Sign Up or Sign In to create code like this or edit this code to your needs.

wfsdf.php

Require Phone Number for WooCommerce Subscriptions

This plugin enhances WooCommerce by requiring customers to provide a phone number when purchasing subscription products. Leveraging WooCommerce and Subscription extensions, it intelligently checks the cart contents and applies the requirement only when necessary, maintaining a smooth checkout process for other products.

<?php /** * Plugin Name: Require Phone Number for Subscriptions * Plugin URI: https://codewp.ai * Description: Makes the Phone Number field required only for subscription products. * Version: 1.0 * Author: CodeWP Assistant * Author URI: https://codewp.ai */ /** * Checks if the product in the cart is a subscription and makes phone field required. */ function cwpai_require_phone_for_subscriptions() { if ( class_exists( 'WC_Subscriptions_Product' ) ) { foreach ( WC()->cart->get_cart() as $cart_item ) { $product = $cart_item['data']; if ( WC_Subscriptions_Product::is_subscription( $product ) ) { add_filter( 'woocommerce_billing_fields', 'cwpai_make_phone_field_required' ); break; } } } } add_action( 'woocommerce_before_checkout_form', 'cwpai_require_phone_for_subscriptions' ); /** * Makes the phone field required. * @param array $fields Billing fields. * @return array Modified billing fields. */ function cwpai_make_phone_field_required( $fields ) { $fields['billing_phone']['required'] = true; return $fields; }

Frequently Asked Questions

The plugin checks if any subscription products are in the cart and, if so, makes the phone number field mandatory at checkout.