Skip to content

Instantly share code, notes, and snippets.

View wbcomdev's full-sized avatar

Wbcom Dev wbcomdev

View GitHub Profile
/**
* Add a custom field (in an order) to the emails.
*/
function wb_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key', true ),
);
return $fields;
}
/**
* Add a custom field (in an order) to the emails.
*/
function wb_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['hear_about_us'] = array(
'label' => __( 'Hear About Us' ),
'value' => get_post_meta( $order->id, 'hear_about_us', true ),
);
return $fields;
}
/**
* Allow customers to access wp-admin.
*/
add_filter( 'woocommerce_prevent_admin_access', '__return_false' );
add_filter( 'woocommerce_disable_admin_bar', '__return_false' );
/**
* Automatically add product to cart on visit.
*/
function wb_add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 38; // replace with your own product id.
$found = false;
// check if product already in cart.
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
/**
* Add another product depending on the cart total.
*/
function wb_add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 38; // replace with your product id.
$found = false;
$cart_total = 30; // replace with your cart total needed to add above item.
/**
* Show product weight on archive or shop pages.
*/
function wb_show_weights() {
global $product;
$weight = $product->get_weight();
if ( $product->has_weight() ) {
echo '<div class="product-meta"><span class="product-meta-label">Weight: </span>' . $weight . get_option( 'woocommerce_weight_unit' ) . '</div></br>';
/**
* Prevent PO box shipping.
*/
function wb_deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array( ' ', '.', ',' );
/**
* Notify admin when a new customer account is created.
*/
function wb_woocommerce_created_customer_admin_notification( $customer_id ) {
wp_send_new_user_notifications( $customer_id, 'admin' );
}
add_action( 'woocommerce_created_customer', 'wb_woocommerce_created_customer_admin_notification' );
/**
* Trim zeros in price decimals.
*/
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );