Skip to content

Instantly share code, notes, and snippets.

@xadapter
xadapter / functions.php
Last active July 12, 2018 17:20
WooCommerce - Custom redirect based on order details just after order placed
add_action('template_redirect', 'wc_custom_redirect_after_purchase');
function wc_custom_redirect_after_purchase() {
global $wp;
if (is_checkout() && !empty($wp->query_vars['order-received'])) {
$order = new WC_Order($wp->query_vars['order-received']);
$quantity = 0;
@xadapter
xadapter / functions.php
Last active August 9, 2016 07:20
Change WooCommerce Cart / Checkout Options based on User Role and Country
add_action('woocommerce_available_payment_gateways', 'xa_change_optins_based_on_user_role');
function xa_change_optins_based_on_user_role(){
global $woocommerce;
// Check with the capability which is unique to the particular user role.
if( current_user_can( 'edit_posts' ) {
//
if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['ccavenue'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
unset( $available_gateways['paypal'] );
@xadapter
xadapter / functions.php
Last active July 4, 2017 11:21
WooCommerce - Add new Custom Fields to Emails
add_filter('woocommerce_email_order_meta_keys', 'xa_email_custom_order_meta_keys');
function xa_email_custom_order_meta_keys( $field_keys ) {
// Please make sure the Custom Field 'tracking_code' is added to the Checkout Field.
$field_keys[] = 'tracking_code';
return $field_keys;
}
@xadapter
xadapter / functions.php
Created August 11, 2016 07:19
WooCommerce - Add a new Custom Checkout Field
add_action( 'woocommerce_after_order_notes', 'xa_new_custom_checkout_field' );
function xa_new_custom_checkout_field( $checkout ) {
echo '<div id="xa_new_custom_checkout_field"><h2>' . __('SSN') . '</h2>';
woocommerce_form_field( 'new_field_ssn', array(
'type' => 'text',
'class' => array('ssn-field-class form-row-wide'),
'label' => __('SSN Number'),
'placeholder' => __('Enter SSN'),
@xadapter
xadapter / functions.php
Created August 11, 2016 07:21
WooCommerce - Change a Required Checkout Field to Optional
add_filter( 'woocommerce_billing_fields', 'xa_filter_billing_phone', 10, 1 );
function xa_filter_billing_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}
@xadapter
xadapter / functions.php
Created August 11, 2016 07:23
WooCommece - Override Existing Checkout Fields
add_filter( 'woocommerce_checkout_fields' , 'xa_override_existing_checkout_fields' );
function xa_override_existing_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Let us know your color preference.';
$fields['order']['order_comments']['label'] = 'Gift Wrap Preference';
return $fields;
}
@xadapter
xadapter / functions.php
Last active October 24, 2017 15:10
Order Export Import Plugin - Export Additional Order Meta Data from WooCommerce https://www.xadapter.com/product/order-import-export-plugin-for-woocommerce/
add_filter('hf_csv_order_post_additional_columns', 'woocommerce_csv_order_post_add_additional_columns', 10, 1);
function woocommerce_csv_order_post_add_additional_columns($additional_meta) {
$additional_meta = array(
'pdf_invoice_num' => '_invoice_number_display',
'Payer PayPal address' => 'Payer PayPal address',
'PayPal Transaction Fee' => 'PayPal Transaction Fee'
);
return $additional_meta;
@xadapter
xadapter / functions.php
Last active October 24, 2017 15:09
WooCommerce Print Invoices, Packing List & Labels - Add additional info like Tracking Number, Ship Date https://www.xadapter.com/product/print-invoices-packing-list-labels-for-woocommerce/
add_filter( 'eh_pklist_label_add_additional_information', 'add_eh_pklist_label_add_tracking_number', 10 , 1 );
function add_eh_pklist_label_add_tracking_number($fromaddress)
{
$tracking_number = '78965413301236547893210'; // Change this to desired one.
$ship_date = '27-08-2016'; // Change it to desired one.
$fromaddress['tracking_number'] = $tracking_number;
$fromaddress['ship_date'] = $ship_date;
return $fromaddress;
}
@xadapter
xadapter / functions.php
Last active October 24, 2017 15:08
Print Invoices, Packing List & Labels Plugin for WooCommerce - Add ship date and tracking number to shipping labels. https://www.xadapter.com/product/print-invoices-packing-list-labels-for-woocommerce/
add_filter( 'wf_pklist_label_add_additional_information', 'wf_pklist_add_tracking_number', 10 , 1 );
function wf_pklist_add_tracking_number($order_additional_information)
{
$tracking_number = '78965413301236547893210'; // Change this to desired one.
$ship_date = '07-08-2016'; // Change it to desired one.
$order_additional_information['tracking_number'] = $tracking_number;;
$order_additional_information['ship_date'] = $ship_date;;
return $order_additional_information;
}
@xadapter
xadapter / functions.php
Last active October 24, 2017 15:10
WooCommerce Order Export Import - Support exporting of user role by whom the order is placed. https://www.xadapter.com/product/order-import-export-plugin-for-woocommerce/
add_filter('hf_csv_order_post_role_column', 'hf_csv_order_add_role_column', 10, 2);
function hf_csv_order_add_role_column($user_roles=array(), $user_id=NULL) {
if ($user_id!==NULL || $user_id===0) {
if($user_id===0){
$user_roles = array('role' => 'Guest');
}else{
$user_info = get_userdata($user_id);
$roles = implode(', ', $user_info->roles) . " ";
$user_roles = array('role' => $roles);