Skip to content

Instantly share code, notes, and snippets.

Avatar

Nicola Mustone SiR-DanieL

View GitHub Profile
View functions.php
add_filter( 'woocommerce_add_to_cart_validation', 'wc_limit_one_per_order', 10, 2 );
function wc_limit_one_per_order( $passed_validation, $product_id ) {
if ( 31 !== $product_id ) {
return $passed_validation;
}
if ( WC()->cart->get_cart_contents_count() >= 1 ) {
wc_add_notice( __( 'This product cannot be purchased with other products. Please, empty your cart first and then add it again.', 'woocommerce' ), 'error' );
return false;
}
View functions.php
/**
* Register term fields
*/
add_action( 'init', 'register_attributes_url_meta' );
function register_attributes_url_meta() {
$attributes = wc_get_attribute_taxonomies();
foreach ( $attributes as $tax ) {
$name = wc_attribute_taxonomy_name( $tax->attribute_name );
@SiR-DanieL
SiR-DanieL / functions.php
Last active November 16, 2022 18:14
functions.php
View functions.php
/**
* Schedule the daily event if necessary.
*/
function schedule_delete_expired_coupons() {
if ( ! wp_next_scheduled( 'delete_expired_coupons' ) ) {
wp_schedule_event( time(), 'daily', 'delete_expired_coupons' );
}
}
add_action( 'init', 'schedule_delete_expired_coupons' );
View functions.php
/**
* Register term fields
*/
add_action( 'init', 'register_vendor_custom_fields' );
function register_vendor_custom_fields() {
add_action( WC_PRODUCT_VENDORS_TAXONOMY . '_add_form_fields', 'add_vendor_custom_fields' );
add_action( WC_PRODUCT_VENDORS_TAXONOMY . '_edit_form_fields', 'edit_vendor_custom_fields', 10 );
add_action( 'edited_' . WC_PRODUCT_VENDORS_TAXONOMY, 'save_vendor_custom_fields' );
add_action( 'created_' . WC_PRODUCT_VENDORS_TAXONOMY, 'save_vendor_custom_fields' );
}
@SiR-DanieL
SiR-DanieL / functions.php
Created November 29, 2017 06:06
functions.php
View functions.php
add_filter( 'body_class', 'add_preorder_class' );
add_filter( 'post_class', 'add_preorder_class' );
function add_preorder_class( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
if ( $product && 'yes' === get_post_meta( $product->get_id(), '_wc_pre_orders_enabled', true ) ) {
$classes[] = 'pre-order';
}
@SiR-DanieL
SiR-DanieL / functions.php
Created March 4, 2022 06:05
Remove Russia from WooCommerce checkout fields.
View functions.php
add_filter( 'woocommerce_countries_allowed_countries', 'wc_ban_russia' );
function wc_ban_russia( $countries ) {
if ( isset( $countries['RU'] ) ) {
unset( $countries['RU'] );
}
return $countries;
}
@SiR-DanieL
SiR-DanieL / functions.php
Created March 4, 2022 04:25
Remove the "From:" in bookable product prices
View functions.php
add_filter( 'woocommerce_get_price_html', 'wc_edit_bookable_remove_from_price', 10, 2 );
function wc_edit_bookable_remove_from_price( $html, $product ) {
if ( ! $product->is_type( 'booking' ) ) {
return $html;
}
return str_replace( 'From: ', '', $html );
}
View short-description-on-shop-page.php
/**
* Plugin Name: Show Short Description on the Shop Page
* Plugin URI: https://nicolamustone.blog/2016/12/01/show-products-short-description-shop-page/#comment-2119
* Description: Shows the product's short description on the shop page with WooCommerce.
* Version: 1.0
* Author: Nicola Mustone
* Author URI: https://nicolamustone.blog
*/
add_action( 'woocommerce_after_shop_loop_item', 'woo_show_excerpt_shop_page', 5 );
@SiR-DanieL
SiR-DanieL / jquery.wc-custom.js
Last active April 27, 2021 05:05
woocommerce ajax load billing address
View jquery.wc-custom.js
jQuery(document).ready(function ( $ ) {
$( 'button.load_customer_billing' ).off( 'click' );
$( 'button.load_customer_billing' ).on( 'click', function() {
if ( window.confirm( woocommerce_admin_meta_boxes.load_billing ) ) {
// Get user ID to load data for
var user_id = $( '#customer_user' ).val();
if ( ! user_id ) {
window.alert( woocommerce_admin_meta_boxes.no_customer_selected );
View functions.php
/**
* Add the Confirm Email Address field to the Billing chekcout form
*/
add_filter( 'woocommerce_checkout_fields' , 'wc_custom_add_email_confirm_field_checkout' );
function wc_custom_add_email_confirm_field_checkout( $fields ) {
$fields['billing']['billing_email_confirm'] = array(
'label' => __( 'Confirm Email Address', 'domain' ),
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 120,