Instantly share code, notes, and snippets.

View prevent-widget.php
<?php
/**
* This will hide widgets
* @param array $instance The current widget instance's settings.
* @param WP_Widget $this The current widget instance.
* @param array $args An array of default widget arguments.
*/
function wcmo_filter_widget_display_callback( $settings, $widget, $args ) {
$can_access = false; // You need to set your conditions here
if( $can_access || in_array( $widget->name, $whitelist ) ) {
View update-existing-cart-item-meta.php
<?php
function prefix_update_existing_cart_item_meta() {
$cart = WC()->cart->cart_contents;
foreach( $cart as $cart_item_id=>$cart_item ) {
$cart_item['new_meta_data'] = 'Your stuff goes here';
WC()->cart->cart_contents[$cart_item_id] = $cart_item;
}
WC()->cart->set_session();
}
View woocommerce-payment-gateway-fee.php
<?php
/**
* Add a fee when the user checks out with PayPal
*/
function wcfad_apply_payment_gateway_fee() {
$payment_method = WC()->session->get( 'chosen_payment_method' );
// Only apply the fee if the payment gateway is PayPal
// Note that you might need to check this slug, depending on the PayPal gateway you're using
if( $payment_method == 'ppec_paypal' ) {
$label = __( 'PayPal fee', 'wcfad' );
View filter-spaced-icons.php
<?php
function my_spaced_filter_amenities_icons( $icons ) {
$icons['spaced_cctv'] = array(
'id' => 'spaced_cctv',
'icon' => 'icon-cctv',
'label' => __( 'CCTV', 'spaced' )
);
return $icons;
}
add_filter( 'spaced_filter_amenities_icons', 'my_spaced_filter_amenities_icons' );
View get-product-from-subscription.php
/**
* How to get the product associated with a WooCommerce subscription
*/
function ct_checkout_subscription_created( $subscription, $order, $recurring_cart ) {
$id = $subscription->get_id(); // You can use this to set meta in the subscription
// Get the products in the order
$items = $order->get_items();
foreach( $items as $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
View woocommerce-is-purchasable.php
<?php
/**
* Prevent products being purchased from archive
* @return Boolean
*/
function pewc_filter_is_purchasable( $is_purchasable, $product ) {
if( is_archive() ) {
return false;
}
return $is_purchasable;
View billing-details-from-wc-order.php
<?php
$order = wc_get_order( $order_id );
echo $order->get_billing_email();
echo $order->get_billing_first_name();
echo $order->get_billing_last_name();
echo $order->get_billing_address_1();
echo $order->get_billing_address_2();
echo $order->get_billing_postcode();
echo $order->get_billing_state();
echo $order->get_billing_country();
View woocommerce-customer-id-from-order.php
<?php
$order = wc_get_order( $order_id );
$customer_id = $order->get_user_id();
View wc-order-query.php
<?php
$args = array(
'limit' => 9999,
'return' => 'ids',
'date_completed' => '2018-10-01...2018-10-10',
'status' => 'completed'
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
foreach( $orders as $order_id ) {
View popular-posts-shortcode.php