Skip to content

Instantly share code, notes, and snippets.

@NickGreen
NickGreen / output_times.php
Last active January 7, 2019 20:00
Output various times
<?php
add_action('wp_footer', 'test_func');
function test_func() {
error_log("WORDPRESS OUTPUT_______");
error_log( "get_option('gmt_offset') returns: " . get_option('gmt_offset') );
error_log( "get_option('timezone_string') returns: " . get_option('timezone_string') );
$blogtime = current_time( 'mysql' );
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = preg_split( '([^0-9])', $blogtime );
error_log( "current_time( 'mysql' ) returns local site time: " . current_time( 'mysql' ) );
error_log( "current_time( 'mysql', 1 ) returns GMT: " . current_time( 'mysql', 1 ) );
@NickGreen
NickGreen / opc_add_to_cart.php
Last active February 25, 2019 18:11
Automatically add product to cart on that product's page
<?php
/**
* Automatically add product to cart on visit
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
$product_ids = array( 128,132,140 ); //replace with your own product ids separated by commas
foreach( $product_ids as $product_id) {
if ( is_product($product_id) ) {
$found = false;
@NickGreen
NickGreen / auth_net_activate_auth_only.php
Last active February 27, 2019 00:29
Set Auth.net to 'Authorize only' if there are no subscription products in the cart
<?php
// note: this would require the store settings to be set to "capture" to begin with
function wc_authorize_net_cim_credit_card_change_transaction_type_no_subs( $settings ) {
if ( method_exists( 'WC_Subscriptions_Cart' , 'cart_contains_subscription' ) ) {
if ( ! WC_Subscriptions_Cart::cart_contains_subscription() ) {
$settings['transaction_type'] = 'authorization';
}
}
@NickGreen
NickGreen / remove-wc-dashboard-widget.php
Created March 1, 2019 19:17
Remove WC Dashboard Widget
<?php
function remove_dashboard_widgets() {
// remove WooCommerce Dashboard Status
remove_meta_box( 'woocommerce_dashboard_status', 'dashboard', 'normal');
}
add_action('wp_user_dashboard_setup', 'remove_dashboard_widgets', 20);
add_action('wp_dashboard_setup', 'remove_dashboard_widgets', 20);
@NickGreen
NickGreen / turn_off_switching_per_user_role.php
Last active November 22, 2021 02:28
Turn off switching for specific user role
<?php
if (is_account_page()) {
$user = wp_get_current_user();
if ( in_array( 'EXAMPLE_ROLE', (array) $user->roles ) ) {
add_filter( 'woocommerce_subscriptions_can_item_be_switched', '__return_false', 100 );
}
}
@NickGreen
NickGreen / auto_remove_coupon_sub.php
Last active April 11, 2019 01:27
Remove coupons from cart if there's a subscription product in it.
<?php
function auto_remove_coupon_sub( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
if ( method_exists( 'WC_Subscriptions_Cart' , 'cart_contains_subscription' ) ) {
@NickGreen
NickGreen / subscriptions_activated_for_order_test.php
Last active November 22, 2021 02:26
subscriptions_activated_for_order early renewal test
<?php
add_action( 'subscriptions_activated_for_order', 'test_function' );
function test_function( $order_id ) {
$subscription = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => array( 'any' ) ));
error_log( 'In ' . __FUNCTION__ . '(), $subscription = ' . var_export( $subscription, true ) );
}
@NickGreen
NickGreen / auto_apply_coupon.php
Last active April 25, 2019 17:50
Auto-apply coupon to cart at least $25
<?php
// apply free shipping coupon automatically when the cart total is >=$25
add_action('woocommerce_before_cart_table', 'freeship_when_total_at_least_25');
function freeship_when_total_at_least_25() {
global $woocommerce;
$cart_total = WC()->cart->get_cart_contents_total();
if( $cart_total >= 25 ) {
$coupon_code = 'freeshipping'; // this requires the coupon of this name to already exist
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
@NickGreen
NickGreen / pre-select.php
Last active November 22, 2021 02:25
Pre-select drop-down item by value
<?php
function preselect_option() {
// URL should be in the format https://xxxxxxx/?option=VALUE_OF_OPTION
$option = htmlspecialchars($_GET["option"]);
echo '<script>';
echo 'function setSelectedIndex(s, valsearch) {';
echo ' // Loop through all the items in drop down list';
echo ' for (i = 0; i< s.options.length; i++) { ';
@NickGreen
NickGreen / deactivate_paypal_unless_subscription.php
Last active May 15, 2019 00:14
Deactivate PayPal standard for non-subscription purchases
<?php
function deactivate_paypal_unless_subscription( $available_gateways ) {
global $wp;
if ( class_exists( 'WC_Subscriptions_Cart' ) ) {
// version 1.5 and 2.0 compatibility
$cart_contains_renewal = function_exists( 'wcs_cart_contains_renewal' ) ? wcs_cart_contains_renewal() : WC_Subscriptions_Cart::cart_contains_subscription_renewal();