Skip to content

Instantly share code, notes, and snippets.

View paaljoachim's full-sized avatar

Paal Joachim Romdahl paaljoachim

View GitHub Profile
@paaljoachim
paaljoachim / functions.php
Created October 3, 2019 21:28
WooCommerce cart: Change shipping rate based on contents count and another example based on total purchased.
// Change shipping based on quantity purchased.
// https://easywebdesigntutorials.com/adjust-shipping-price-when-quantity-changes/
// https://businessbloomer.com/woocommerce-setup-tiered-shipping-rates-order-amount/
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 );
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
$threshold1 = 2;
$threshold2 = 3;
if ( WC()->cart->get_cart_contents_count() < $threshold1 ) {
unset( $rates['flat_rate:6'], $rates['flat_rate:8'] );
} elseif ( WC()->cart->get_cart_contents_count() < $threshold2 ){
@paaljoachim
paaljoachim / functions.php
Last active September 12, 2019 12:34
WooCommerce Checkout: Add multiple custom fields to billing area - customer case
// Initial inspiration: https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/
// My Custom Fields
// Can be added to billing, shipping or order area. For the account page use the word account.
add_filter( 'woocommerce_checkout_fields', 'custom_fields_checkout' );
function custom_fields_checkout( $fields ) {
$fields['billing']['billing_idrettslag'] = array(
'label' => 'Hvilken idrettslag/ forening/ klasse?',
'type' => 'text',
'required' => true,
@paaljoachim
paaljoachim / functions.php
Last active May 29, 2022 00:49
Tutorial: WooCommerce Checkout: Add multiple custom fields to billing area. This code adds priority placement of field. Shows the custom fields in the backend Order Details screen and in e-mails to the admin and customer. I have also adjusted the checkbox field to give a text result instead of a value of 1. Checkbox is also pre-selected.
/* --------- Adds custom fields using filters. ------
The below custom fields shows various types one can use.
It is then displayed in the backend Order Details page and in admin and customer e-mails.
Checkboxes by default result only show the number 1 when clicked. I have added code so that when the a checkbox is clicked it will
show text such as On and Yes. Checkbox is also pre-selected.
Tutorial: https://easywebdesigntutorials.com/woocommerce-modifying-the-checkout-page/
*/
// Initial inspiration: https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/
// My Custom Fields
@paaljoachim
paaljoachim / functions.php
Last active September 2, 2019 19:27
WooCommerce Checkout: Add multiple custom fields to billing area - PART 2
// https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/
// Year
add_filter( 'woocommerce_checkout_fields', 'bbloomer_billing_year_checkout' );
function bbloomer_billing_year_checkout( $fields ) {
$fields['billing']['billing_year'] = array(
'label' => 'I am over 18',
'type' => 'checkbox',
'required' => true,
'class' => array( 'form-row-wide' ),
@paaljoachim
paaljoachim / functions.php
Last active September 2, 2019 12:33
WooCommerce Checkout: Add multiple custom fields to billing area
// https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/
// Year
add_filter( 'woocommerce_checkout_fields', 'bbloomer_billing_year_checkout' );
function bbloomer_billing_year_checkout( $fields ) {
$fields['billing']['billing_year'] = array(
'label' => 'I am over 18',
'type' => 'checkbox',
'required' => true,
'class' => array( 'form-row-wide' ),
@paaljoachim
paaljoachim / functions.php
Last active March 7, 2023 18:50
WooCommerce: Adding multiple custom fields to the order notes area in the checkout page
/* 1. Adds a custom field. NB. I am using some Norwegian words in the below text.
* 2. Then adds a validate error message if person does not fill out the field.
* 3. Then adds the custom field to the order page.
https://businessbloomer.com/woocommerce-add-custom-checkout-field-php/
https://businessbloomer.com/woocommerce-add-shipping-phone-checkout/
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-6
*/
add_action( 'woocommerce_before_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
@paaljoachim
paaljoachim / functions.php
Last active September 27, 2022 18:00
WooCommerce dropdown product quantity. It also correctly adds the correct minimum quantity when pressing the Add to Cart in the Shop page.
/** Initial code from: https://gist.github.com/kloon/6495019
As the code did not fully work I received help from Helga the Viking
with this gist: https://gist.github.com/helgatheviking/ff8792fbb12f5c5367c816b8a46c70ad
* Change the quantity input to select.
* @param array $args Args for the input.
* @param WC_Product|null $product Product.
* @param boolean $echo Whether to return or echo|string.
* @return string
*/
@paaljoachim
paaljoachim / cpt.php
Last active September 10, 2018 15:53
Using the CPT - Custom Post Type UI plugin to create Custom Post Types
function cptui_register_my_cpts() {
/**
* Post Type: Nettmagasin.
*/
$labels = array(
"name" => __( "Nettmagasin", "tm-beans" ),
"singular_name" => __( "Nettmagasin", "tm-beans" ),
"all_items" => __( "Alle Nettmag tekster", "tm-beans" ),
@paaljoachim
paaljoachim / woocommerce-snippet.php
Last active January 10, 2018 08:16
WooCommerce checkout field adjustments. Removes company, country, address, city, state and postcode. I have commented out the removing of phone and e-mail. I have also removed the additional information box.
/* -------- WooCommerce - checkout fields ---------- */
/* From Kathy. */
function kia_modify_default_address_fields( $fields ){
if( isset( $fields['company'] ) ) unset( $fields['company'] );
if( isset( $fields['country'] ) ) unset( $fields['country'] );
if( isset( $fields['address_1'] ) ) unset( $fields['address_1'] );
if( isset( $fields['address_2'] ) ) unset( $fields['address_2'] );
if( isset( $fields['city'] ) ) unset( $fields['city'] );
if( isset( $fields['state'] ) ) unset( $fields['state'] );
@paaljoachim
paaljoachim / autoconfirm-order-woocommerce.php
Last active January 8, 2020 14:31
WooCommerce auto confirm order code snippet from Kathy. Any order marked for "processing" will be changed to complete without the need to manually click the complete button through the order screen in WooCommerce.
add_filter( 'woocommerce_payment_complete_order_status', 'kia_order_payment_complete_order_status', 10, 2 );
function kia_order_payment_complete_order_status( $order_status, $order_id ) {
$order = wc_get_order( $order_id );
if ( 'processing' == $order_status &&
( 'pending' == $order->status ) ) {
$order_status = 'completed';
}