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:13
MySQL query remove wc_sessions
DELETE FROM wp_options
WHERE option_name LIKE '_wc_session_%' OR option_name LIKE '_wc_session_expires_%';
@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 / robots.txt
Created August 14, 2015 23:15
Robots.txt prevent access to WooCommerce add-to-cart links
User-agent: *
Disallow: /*add-to-cart=*
@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;