Add Billing Phone to WooCommerce Orders Page
Add Billing Phone and Shipping phone to WooCommerce Orders Page
Add Shipping phone Field to Checkout Page under Shipping Section
Display Woocommerce Orders Summary Page For Payment Method And Shipping Method
Shipping Fee Depending on Order Value
Shipping Fee Depending on Order Value and Extra Charge for Specific Postal Code
Replace Read More to Out of Stock
WooCommerce Specific Product for Minimum Quantity Function for Option Products
Add Billing Phone to WooCommerce Orders Page
Insert into function.php file
// Add Billing Phone to WooCommerce Orders Page
add_filter('manage_edit-shop_order_columns', 'add_custom_shop_order_column', 20);
add_action('manage_shop_order_posts_custom_column', 'custom_shop_order_column_content', 20, 2);
function add_custom_shop_order_column($columns) {
// Add Billing Phone after Order Total column
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ('order_total' === $column_name) {
$new_columns['billing_phone'] = __('Billing Phone', 'woocommerce');
}
}
return $new_columns;
}
function custom_shop_order_column_content($column, $post_id) {
$order = wc_get_order($post_id);
switch ($column) {
case 'billing_phone':
echo $order->get_billing_phone();
break;
}
}
Add Billing Phone and Shipping phone to WooCommerce Orders Page
Insert into function.php file
// Add Billing Phone and Shipping Phone to WooCommerce Orders Page
add_filter('manage_edit-shop_order_columns', 'add_custom_shop_order_column', 20);
add_action('manage_shop_order_posts_custom_column', 'custom_shop_order_column_content', 20, 2);
function add_custom_shop_order_column($columns) {
// Add Billing Phone and Shipping Phone after Order Total column
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ('order_total' === $column_name) {
$new_columns['billing_phone'] = __('Billing Phone', 'woocommerce');
$new_columns['shipping_phone'] = __('Shipping Phone', 'woocommerce');
}
}
return $new_columns;
}
function custom_shop_order_column_content($column, $post_id) {
$order = wc_get_order($post_id);
switch ($column) {
case 'billing_phone':
echo $order->get_billing_phone();
break;
case 'shipping_phone':
echo $order->get_shipping_phone();
break;
}
}
Add Shipping Phone Field to Checkout Page under Shipping Section
Insert into function.php file
/* add a phone number field in the Shipping section */
add_filter( 'woocommerce_checkout_fields', 'ts_shipping_phone_checkout' );
function ts_shipping_phone_checkout( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => 'Phone',
'type' => 'tel',
'required' => true,
'class' => array( 'form-row-wide' ),
'validate' => array( 'phone' ),
'autocomplete' => 'tel',
'priority' => 25,
);
return $fields;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'ts_shipping_phone_checkout_display' );
function ts_shipping_phone_checkout_display( $order ){
echo 'Shipping Phone: ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '
';
}
Display Woocommerce Orders Summary Page For Payment Method And Shipping Method
To replace “flatsome-child” with your theme folder name, to check via file manager > wp-content > themes.
Insert into function.php file
// display woocommerce orders summary page for payment method and shipping method
// Add custom columns to the WooCommerce orders page
add_filter('manage_edit-shop_order_columns', 'add_custom_shop_order_columns');
function add_custom_shop_order_columns($columns) {
// Insert the new columns after the order status column
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ('order_status' === $column_name) {
$new_columns['payment_method'] = __('Payment Method', 'flatsome-child');
$new_columns['shipping_method'] = __('Shipping Method', 'flatsome-child');
}
}
return $new_columns;
}
// Populate custom columns with data
add_action('manage_shop_order_posts_custom_column', 'populate_custom_shop_order_columns', 10, 2);
function populate_custom_shop_order_columns($column, $post_id) {
$order = wc_get_order($post_id);
if ('payment_method' === $column) {
$payment_method = $order->get_payment_method_title();
echo esc_html($payment_method);
}
if ('shipping_method' === $column) {
$shipping_methods = $order->get_shipping_methods();
$shipping_method_names = array();
foreach ($shipping_methods as $shipping_method) {
$shipping_method_names[] = $shipping_method->get_method_title();
}
echo esc_html(implode(', ', $shipping_method_names));
}
}
Shipping Fee Depending on Order Value
Exact shipping name is required for the code to work together for “Free delivery”, “Standard Shipping (Orders under $50)” and “Discounted Shipping (Orders $50+)”.
Insert into function.php file
// shipping fee depending on order value
add_filter( 'woocommerce_package_rates', 'conditional_shipping_based_on_cart_total', 10, 2 );
function conditional_shipping_based_on_cart_total( $rates, $package ) {
$cart_total = WC()->cart->subtotal;
$standard_rate_id = '';
$discounted_rate_id = '';
$free_delivery_id = '';
$self_collection_id = '';
// Find the correct rate IDs
foreach ( $rates as $rate_id => $rate ) {
switch ($rate->label) {
case 'Standard Shipping (Orders under $50)':
$standard_rate_id = $rate_id;
break;
case 'Discounted Shipping (Orders $50+)':
$discounted_rate_id = $rate_id;
break;
case 'Free Delivery':
$free_delivery_id = $rate_id;
break;
case 'Self Collection':
$self_collection_id = $rate_id;
break;
}
}
if ( $cart_total >= 200 ) {
// For orders $200 and above
if ( $free_delivery_id && isset( $rates[$free_delivery_id] ) ) {
$rates[$free_delivery_id]->cost = 0;
}
// Remove standard and discounted shipping
if ( $standard_rate_id && isset( $rates[$standard_rate_id] ) ) {
unset( $rates[$standard_rate_id] );
}
if ( $discounted_rate_id && isset( $rates[$discounted_rate_id] ) ) {
unset( $rates[$discounted_rate_id] );
}
} elseif ( $cart_total >= 50 ) {
// For orders $50 to $199.99
if ( $discounted_rate_id && isset( $rates[$discounted_rate_id] ) ) {
$rates[$discounted_rate_id]->cost = 6;
}
// Remove standard shipping and free delivery
if ( $standard_rate_id && isset( $rates[$standard_rate_id] ) ) {
unset( $rates[$standard_rate_id] );
}
if ( $free_delivery_id && isset( $rates[$free_delivery_id] ) ) {
unset( $rates[$free_delivery_id] );
}
} else {
// For orders below $50
if ( $standard_rate_id && isset( $rates[$standard_rate_id] ) ) {
$rates[$standard_rate_id]->cost = 8;
}
// Remove discounted shipping and free delivery
if ( $discounted_rate_id && isset( $rates[$discounted_rate_id] ) ) {
unset( $rates[$discounted_rate_id] );
}
if ( $free_delivery_id && isset( $rates[$free_delivery_id] ) ) {
unset( $rates[$free_delivery_id] );
}
}
// Always keep Self Collection option
if ( $self_collection_id && isset( $rates[$self_collection_id] ) ) {
// You can set a cost for self collection here if needed
// $rates[$self_collection_id]->cost = 0;
}
return $rates;
}
Shipping Fee Depending on Order Value and Extra Charge for Specific Postal Code
This function checks if the first three digits of the postal code are “098” or “099” to be charged an additional $4.
Insert into function.php file
// shipping fee depending on order value and postal code
add_filter( 'woocommerce_package_rates', 'conditional_shipping_based_on_cart_total', 10, 2 );
function conditional_shipping_based_on_cart_total( $rates, $package ) {
$cart_total = WC()->cart->subtotal;
$destination_postal_code = $package['destination']['postcode'];
$extra_charge = 4;
$standard_rate_id = '';
$discounted_rate_id = '';
$free_delivery_id = '';
$self_collection_id = '';
// Find the correct rate IDs
foreach ( $rates as $rate_id => $rate ) {
switch ($rate->label) {
case 'Standard Shipping (Orders under $50)':
$standard_rate_id = $rate_id;
break;
case 'Discounted Shipping (Orders $50+)':
$discounted_rate_id = $rate_id;
break;
case 'Free Delivery':
$free_delivery_id = $rate_id;
break;
case 'Self Collection':
$self_collection_id = $rate_id;
break;
}
}
// Function to check if postal code requires extra charge
function requires_extra_charge($postal_code) {
$first_three_digits = substr($postal_code, 0, 3);
return $first_three_digits === '098' || $first_three_digits === '099';
}
if ( $cart_total >= 200 ) {
// For orders $200 and above
if ( $free_delivery_id && isset( $rates[$free_delivery_id] ) ) {
$rates[$free_delivery_id]->cost = 0;
}
// Remove standard and discounted shipping
if ( $standard_rate_id && isset( $rates[$standard_rate_id] ) ) {
unset( $rates[$standard_rate_id] );
}
if ( $discounted_rate_id && isset( $rates[$discounted_rate_id] ) ) {
unset( $rates[$discounted_rate_id] );
}
} elseif ( $cart_total >= 50 ) {
// For orders $50 to $199.99
if ( $discounted_rate_id && isset( $rates[$discounted_rate_id] ) ) {
$rates[$discounted_rate_id]->cost = 6;
// Add extra charge if postal code matches
if ( requires_extra_charge($destination_postal_code) ) {
$rates[$discounted_rate_id]->cost += $extra_charge;
}
}
// Remove standard shipping and free delivery
if ( $standard_rate_id && isset( $rates[$standard_rate_id] ) ) {
unset( $rates[$standard_rate_id] );
}
if ( $free_delivery_id && isset( $rates[$free_delivery_id] ) ) {
unset( $rates[$free_delivery_id] );
}
} else {
// For orders below $50
if ( $standard_rate_id && isset( $rates[$standard_rate_id] ) ) {
$rates[$standard_rate_id]->cost = 8;
// Add extra charge if postal code matches
if ( requires_extra_charge($destination_postal_code) ) {
$rates[$standard_rate_id]->cost += $extra_charge;
}
}
// Remove discounted shipping and free delivery
if ( $discounted_rate_id && isset( $rates[$discounted_rate_id] ) ) {
unset( $rates[$discounted_rate_id] );
}
if ( $free_delivery_id && isset( $rates[$free_delivery_id] ) ) {
unset( $rates[$free_delivery_id] );
}
}
// Always keep Self Collection option
if ( $self_collection_id && isset( $rates[$self_collection_id] ) ) {
// You can set a cost for self collection here if needed
// $rates[$self_collection_id]->cost = 0;
}
return $rates;
}
Replace Read More to Out of Stock
Change “Read more” button in Woocommerce
Credit: https://generatepress.com/forums/topic/change-read-more-button-in-woocommerce/
Insert into function.php file
// Replace Read More to Out of Stock
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
if ( 'Read more' == $text ) {
$text = __( 'Out of Stock', 'woocommerce' );
}
return $text;
} );
WooCommerce Specific Product for Minimum Quantity Function for Option Products
Insert into function.php file
// WooCommerce Specific Product for Minimum Quantity Function for Option Products
// Check if WooCommerce is active
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Hook into WooCommerce checkout process
add_action( 'woocommerce_check_cart_items', 'check_products_minimum_quantity' );
function check_products_minimum_quantity() {
// Set the product IDs and their respective minimum quantities
$product_rules = array(
14714 => 10, // Min order is 10 sets
54731 => 2, // Min order is 2 sets
44720 => 2 // Min order is 2 sets
);
// Initialize quantity counters
$quantities = array_fill_keys(array_keys($product_rules), 0);
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
if ( array_key_exists($product_id, $product_rules) ) {
$quantities[$product_id] += $cart_item['quantity'];
}
}
// Check if the minimum quantity is met for each product
$errors = array();
foreach ( $product_rules as $product_id => $min_quantity ) {
if ( $quantities[$product_id] > 0 && $quantities[$product_id] < $min_quantity ) {
$product = wc_get_product($product_id);
$errors[] = sprintf(
'You must order at least %d sets of %s (Product ID: %d). You currently have %d in your cart.',
$min_quantity,
$product->get_name(),
$product_id,
$quantities[$product_id]
);
}
}
// If there are errors, display them and prevent checkout
if ( !empty($errors) ) {
foreach ( $errors as $error ) {
wc_add_notice( $error, 'error' );
}
// Optionally, you can remove the checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}