Skip to content

Instantly share code, notes, and snippets.

View daveloodts's full-sized avatar

Dave Loodts daveloodts

View GitHub Profile
@daveloodts
daveloodts / gist:2ff15712d9910dcfbe98e8fbccf471de
Last active October 7, 2024 07:39
WooCommerce coupons and shipping costs
<?php
add_action('woocommerce_cart_calculate_fees', 'apply_coupon_to_shipping', 10, 1);
function apply_coupon_to_shipping($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
// Get the applied coupon(s)
@daveloodts
daveloodts / gist:976115f1d35a9b29c012f18ef44368c5
Created October 2, 2024 12:30
Campaign overview in order table
<?php
// Add new columns to the order table
add_filter('manage_edit-shop_order_columns', 'add_custom_order_column', 20);
function add_custom_order_column($columns) {
$columns['utm_source'] = __('Campaign', 'woocommerce');
return $columns;
}
// Populate the new columns with UTM data
define( 'WP_AUTO_UPDATE_CORE', false );
define( 'DISALLOW_FILE_EDIT', true );
define( 'WP_POST_REVISIONS', 12 );
define('AUTOSAVE_INTERVAL', 180);
<?php
/**
* Email Order Items
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
@daveloodts
daveloodts / gist:a00f1130e2fc9453c9729811edb44f44
Created March 14, 2022 09:56
WooCommerce afhaal tekst in mails
function action_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_processing_order' && $order->has_shipping_method( 'local_pickup' ) ) {
echo __( '<strong>U ontvangt een mailtje wanneer uw bestelling klaarligt.</strong><br/><br+>', 'woocommerce' );
// For 'completed'
} elseif( $email->id == 'customer_completed_order' && $order->has_shipping_method( 'local_pickup' ) ) {
echo __( '<strong>Uw bestelling ligt klaar. <a href="https://www.vanlommelprinting.be/contact/">Bekijk onze openingsuren</a></strong><br/><br/>', 'woocommerce' );
}
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_customer_details', 10, 4 );
add_action( 'template_redirect', function() {
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
});
@daveloodts
daveloodts / gist:46a7e7bb67e3c01e2bcc23de728cfbff
Last active August 20, 2021 21:07
schema.org book for WooCommerce
<?php
// define the woocommerce_structured_data_product callback
function filter_woocommerce_structured_data_product( $markup, $product ) {
$formaat = get_field("boek_formaat");
$authors = get_field("boek_auteurs");
$date = get_the_date('Y m d');
$markup['additionalType'] = "book";
$markup['brand'] = "Ertsberg";
$markup['isbn'] = $product->get_sku();
@daveloodts
daveloodts / gist:c5993a216746c136e2b5e8120625037b
Created April 21, 2021 14:38
Volgorde van adresvelden in WooCommerce account pagina
// Account Edit Adresses: Remove and reorder addresses fields
add_filter( 'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
function custom_default_address_fields( $fields ) {
if( ! is_account_page() ) return $fields;
$sorted_fields = array('first_name','last_name','country','address_1','postcode','city');
$new_fields = array();
$priority = 0;
foreach($sorted_fields as $key_field){
$priority += 10;
if( $key_field == 'postcode' )
// Check if address field contains house number otherwise provide error message
add_action( 'woocommerce_after_checkout_validation', 'validate_checkout', 10, 2);
function validate_checkout( $data, $errors ){
if ( ! preg_match('/[0-9]/', $data[ 'billing_address_1' ] ) ){
$errors->add( 'address', 'Het adresveld bevat geen huisnummer of busnummer.' );
}
}
@daveloodts
daveloodts / gist:67473ca072f613fb4f1175b2463504dc
Created January 15, 2021 09:41
Verplichte checkbox toevoegen in WooCommerce + notificatie indien niet ingevuld
/**
* Add WooCommerce Checkbox checkout
*/
add_action( 'woocommerce_review_order_before_submit', 'woofers_add_checkout_checkbox', 10 );
function woofers_add_checkout_checkbox() {
woocommerce_form_field( 'checkout-checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),