Skip to content

Instantly share code, notes, and snippets.

@ScottDeLuzio
ScottDeLuzio / functions.php
Created August 14, 2015 23:02
Hook into checkout form
add_action('woocommerce_after_order_notes', 'conditional_checkout_field');
@ScottDeLuzio
ScottDeLuzio / functions.php
Last active August 29, 2015 14:27
Check if product is in cart
<?php
function conditional_checkout_field( $checkout ) {
echo '<div id="conditional_checkout_field">';
$check_in_cart = conditional_product_in_cart(xxxx); //replace xxxx with the product id
// Check if the product is in the cart and show the custom fields if it is
if ($check_in_cart === true ) {
echo '<h2>'.__('My Custom Checkout Field').'</h2>';
@ScottDeLuzio
ScottDeLuzio / mysql_query
Created August 14, 2015 23:14
MySQL query remove WooCommerce transients
DELETE FROM wp_options
WHERE option_name LIKE '_transient_wc_%' OR option_name LIKE '_transient_timeout_wc_%';
@ScottDeLuzio
ScottDeLuzio / functions.php
Created August 14, 2015 23:18
Add custom field to checkout page
/**
* Add the field to the checkout page
*/
add_action( 'woocommerce_after_order_notes', 'some_custom_checkout_field' );
function some_custom_checkout_field( $checkout ) {
echo '<div id="some_custom_checkout_field"><h2>' . __('My Field Header') . '</h2>';
woocommerce_form_field( 'some_field_name', array(
@ScottDeLuzio
ScottDeLuzio / functions.php
Created August 14, 2015 23:19
Process custom field in checkout
<?php
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'some_custom_checkout_field_process');
function some_custom_checkout_field_process() {
// Check if the field is set, if not then show an error message.
if ( ! $_POST['some_field_name'] )
wc_add_notice( __( 'Please enter something here.' ), 'error' );
@ScottDeLuzio
ScottDeLuzio / functions.php
Created August 14, 2015 23:20
Update order meta with custom field value
<?php
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'some_custom_checkout_field_update_order_meta' );
function some_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['some_field_name'] ) ) {
update_post_meta( $order_id, 'Some Field', sanitize_text_field( $_POST['some_field_name'] ) );
}
@ScottDeLuzio
ScottDeLuzio / functions.php
Created August 14, 2015 23:21
Add custom field to order emails
<?php
/**
* Add the field to order emails
**/
add_filter( 'woocommerce_email_order_meta_keys', 'some_order_meta_keys' );
function some_order_meta_keys() {
if (get_post_meta( get_the_ID(), 'Some Field')) {
echo 'This is the custom information you entered in Some Field: ' . get_post_meta( get_the_ID(), 'Some Field', true) . '<br />';
}
@ScottDeLuzio
ScottDeLuzio / functions.php
Created August 14, 2015 23:24
WooCommerce checkout field hook and change default fields
<?php
// WooCommerce Checkout Fields Hook
add_filter( 'woocommerce_checkout_fields' , 'custom_wc_checkout_fields' );
// Change order comments placeholder and label, and set billing phone number to not required.
function custom_wc_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Enter your placeholder text here.';
$fields['order']['order_comments']['label'] = 'Enter your label here.';
$fields['billing']['billing_phone']['required'] = false;
return $fields;
remove_filter( 'woocommerce_default_address_fields', 'get_default_address_fields', 10, 1 );
add_filter( 'woocommerce_default_address_fields', 'updated_get_default_address_fields', 10, 1 );
function updated_get_default_address_fields() {
global $woocommerce;
$fields = array(
'first_name' => array(
'label' => __( 'First Name', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first' ),
),
@ScottDeLuzio
ScottDeLuzio / UserForm
Created February 18, 2016 20:23
Software Licensing for Excel Add-In Populate UserForm
Private Sub userform_initialize()
With ThisWorkbook.Worksheets("Sheet1").Range("A1")
license.Value = .Cells(1, 1).Value
Label2.Caption = "Your license key is " & .Cells(3, 1).Value & " and has " & .Cells(4, 1).Value & " activations remaining."
End With
End Sub