Skip to content

Instantly share code, notes, and snippets.

@harishankerr
harishankerr / functions.php
Created April 24, 2020 19:43
Belgium Zip code validation for WooCommerce
add_filter('woocommerce_validate_postcode','woo_validate_belgium_postcode',10,3);
function woo_validate_belgium_postcode($valid, $postcode, $country){
if($country=="BE")
$valid = (bool) preg_match( '/^([0-9]{4})$/', $postcode );
// Checks your postcode to seee if it matches belgium (4 digits). If not, it will reject validation.
return $valid;
}
@harishankerr
harishankerr / functions.php
Created April 23, 2020 04:59
Add custom images (e.g. A Secure Checkout image) to the bottom of your Single Product Pages
/*
This snippet helps you add custom images (like "Secure Checkout") to single product pages. You can add it to your site at the bottom of your theme's `functions.php` file, or by using a Code Snippets Plugin: https://wordpress.org/plugins/code-snippets/
The end result would look like this: https://d.pr/i/nUk1Bm
*/
function woo_add_security_image() {
echo '<img src="https://example.com/security-image-url.jpg" />'; // Replace the URL here with the URL of your image.
/* Remove the `//` Comments and update the URL(s) below if you would like to add multiple images to your single product page */
@harishankerr
harishankerr / functions.php
Created April 17, 2020 07:08
Hide Add to cart button in the Shop page, Home page, Category Page, and in Related Products
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
/*Removes add to cart buttons from shop, category, and home pages */
if( is_product_category() || is_shop() || is_front_page()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
/*Removes add to cart buttons from related products section */
global $woocommerce_loop;
@harishankerr
harishankerr / functions.php
Created February 17, 2020 06:42
Update order status to `processing` for the cheque payments method
add_action('woocommerce_order_status_changed', 'wc_auto_complete_by_payment_method');
function wc_auto_complete_by_payment_method($order_id)
{
if ( ! $order_id ) {
return;
}
global $product;
@harishankerr
harishankerr / woocommerce-stripe-apple-pay.php
Last active September 3, 2022 14:38
WooCommerce Stripe: Move Apple Pay Button to the bottom of the checkout page
/*
* Adds then moves Apple Pay button on the checkout page.
*/
add_filter( 'wc_stripe_show_payment_request_on_checkout', '__return_true' );
remove_action( 'woocommerce_checkout_before_customer_details', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_html' ), 1 );
remove_action( 'woocommerce_checkout_before_customer_details', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_separator_html' ), 2 );
add_action( 'woocommerce_after_checkout_form', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_html' ), 2 );
@harishankerr
harishankerr / allow-only-products-from-one-category.php
Created December 19, 2018 10:56
Snippet to allow only products from a particular category to be added to the cart at a time.
add_filter( 'woocommerce_add_to_cart_validation', 'wc_limit_one_per_order', 10, 2 );
function wc_limit_one_per_order( $passed_validation, $product_id ) {
$terms = wp_get_post_terms( $product_id, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'clothes', $categories ) ) { // 'clothes' is the defined product category. Change this value to the slug of your choice.
return $passed_validation;
}
/* if ( 55 !== $product_id ) {
@harishankerr
harishankerr / mandatory-state-field.php
Created December 18, 2018 03:57
Make State Field Mandatory For WooCommerce
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_state_fields' );
// Our hooked in function - $state_fields is passed via the filter!
function custom_override_default_state_fields( $state_fields ) {
$state_fields['state']['required'] = true;
return $state_fields;
}
/**
* Customize user data for new users registered via WC Social Login
* Example: set a custom role other than "customer" on registration
*/
/**
* Add filters to adjust user data for each active Social Login provider
*/
function sv_wc_social_login_new_user_data_add_filters() {
if ( ! function_exists( 'wc_social_login' ) ) {
@harishankerr
harishankerr / add-handing-fee.php
Last active June 7, 2017 01:25
Add Handling Fee To Flat Rate Shipping Method
function wc_ninja_change_flat_rates_cost( $rates, $package ) {
// Make sure flat rate is available
if ( isset( $rates['flat_rate'] ) ) {
// Adds a $3 handling fee to the existing flat rate shipping method.
$rates['flat_rate']->cost += 3;
}
return $rates;
}
@harishankerr
harishankerr / remove-checkout-fields.php
Last active May 2, 2017 03:25
Remove Checkout Fields From WooCommerce Checkout
<?php // Use the PHP Tag only if needed
/*
Snippet to remove Company And Phone Fields From The Checkout Page In WooCommerce
---------------------------------------------------------------------------------
Please paste this code snippet at the bottom of the functions.php file in your theme, or use a Code Snippets Plugin (https://wordpress.org/plugins/code-snippets/)plugin to add this snippet.
*/