Skip to content

Instantly share code, notes, and snippets.

@NickGreen
NickGreen / auto-complete.php
Created November 15, 2017 21:32
Autocomplete orders for a specific product in WooCommerce
<?php
/**
* Auto Complete all WooCommerce orders of a specific product.
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
@NickGreen
NickGreen / hide_no_stock_products.php
Last active December 22, 2017 22:54
Hide products that have no stock, but still allow backorders.
<?php
/*
* Use case: Product has 'allow backorders' enabled to allow purchase
* of products with stock fewer than 1, but they want them to be purchasable
* if direct linked to or added to the cart in other ways.
*/
function pp_always_hide_zero_stock( $is_visible, $id ) {
$product = wc_get_prodcut( $id );
if ( $product->stock < 1 ) {
@NickGreen
NickGreen / custom-renewal-period.php
Last active November 22, 2021 02:50
Add a new renewal period to WooCommerce Subscriptions to allow for custom renewal periods for products. Specifically a new "every 7th" selection that will set a subscription to renew every 7th renewal period..
<?php
/**
* Plugin Name: WooCommerce Subscription Custom Renewal Period
* Description: Add a new renewal period to WooCommerce Subscriptions to allow for custom renewal periods for products.
* Author: Nick Green
* Version: 1.0
* License: GPL v3
*/
function wcs_custom_renewal_period( $subscription_periods ) {
$subscription_periods[7] = sprintf( __( 'every %s', 'woocommerce-subscriptions' ), WC_Subscriptions::append_numeral_suffix( 7 ) );
@NickGreen
NickGreen / hide-variation.php
Last active April 5, 2018 19:22
hide variation with ID 99
function hide_variation( $is_visible, $id ) {
if ( $id == 99 ) {
$is_visible = false;
}
return $is_visible;
}
add_filter( 'woocommerce_variation_is_visible', 'hide_variation', 10, 2 );
@NickGreen
NickGreen / hide-payment-methods-tab.php
Last active May 16, 2018 19:52
Hide the "Payment Methods" tab from the WooCommerce My Account page
<?php
add_filter( 'woocommerce_account_menu_items', 'remove_payment_methods_tab_my_account', 999 );
function remove_payment_methods_tab_my_account( $items ) {
unset($items['payment-methods']);
return $items;
}
@NickGreen
NickGreen / prevent_suspension_per_product.php
Last active November 22, 2021 02:37
Disallow suspending a subscription based on product ID
function prevent_suspension_per_product( $user_can_suspend, $subscription ) {
if ( ! $user_can_suspend ) {
return $user_can_suspend;
}
$product_ids = array(438,128,2,345); // Change to whatever product ids you want to prevent suspension for
foreach ( $product_ids as $product_id ) {
if ( $subscription->has_product( $product_id ) ) {
$user_can_suspend = false;
@NickGreen
NickGreen / added_to_cart.php
Created July 19, 2018 15:30
Added to cart
add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
if ( is_shop() || is_product_category() || is_product_tag() ): // Only for archives pages
?>
<script type="text/javascript">
// Ready state
(function($){
$( document.body ).on( 'added_to_cart', function(){
console.log('EVENT: added_to_cart');
add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
?>
<script type="text/javascript">
// Ready state
(function($){
$( document.body ).on( 'added_to_cart', function(){
console.log('EVENT: added_to_cart');
});
@NickGreen
NickGreen / always_display_change_address_button.php
Last active July 20, 2018 20:17
Display 'Change Address' button even if there are no shipping methods declared
<?php
function always_show_edit_address_subscription_action( $actions, $subscription ) {
if ( $subscription->has_status( array( 'active', 'on-hold' ) ) ) {
$actions['change_address'] = array(
'url' => add_query_arg( array( 'subscription' => $subscription->get_id() ), wc_get_endpoint_url( 'edit-address', 'shipping' ) ),
'name' => __( 'Change Address', 'woocommerce-subscriptions' ),
);
}
@NickGreen
NickGreen / turn_off_switching_per_product.php
Created July 23, 2018 22:48
turn_off_switching_per_product
<?php
add_filter('woocommerce_subscriptions_can_item_be_switched','turn_off_switching_per_product', 10, 3);
function turn_off_switching_per_product(){
$turn_off_switching_for_these_products = array(10,24,438); // change this for whatever products you want to make not switchable
$product_id = wcs_get_canonical_product_id( $item );
if (in_array($product_id, $turn_off_switching_for_these_products)) {
$item_can_be_switch = false;
}
}