Skip to content

Instantly share code, notes, and snippets.

@SirDarcanos
SirDarcanos / functions.php
Last active October 30, 2023 10:00
Delete Expired Coupons Automatically
<?php
// Delete Expired Coupons that have an expiration date set and are not from AutomateWoo
add_action('delete_expired_coupons_hook', 'delete_expired_coupons_action');
function delete_expired_coupons_action() {
$query_args = [
'fields' => 'ids',
'post_type' => 'shop_coupon',
'post_status' => 'any',
'posts_per_page' => -1,
'orderby' => 'date',
public function is_new_post( $post ) {
$process_old_posts = get_option( 'wp_discord_post_process_old_posts' );
$post_date = strtotime( $post->post_date );
var_dump(
$post_date < current_time( 'timestamp' ),
'yes' === $process_old_posts,
'yes' !== get_post_meta( $post->ID, '_wp_discord_post_published', true )
);
/**
* Change the subscription thank you message after purchase
*
* @param int $order_id
* @return string
*/
function custom_subscription_thank_you( $order_id ){
if( WC_Subscriptions_Order::order_contains_subscription( $order_id ) ) {
$thank_you_message = sprintf( __( '%sThank you for purchasign our subscription! Visit %syour account%s page to know its status.%s', 'woocommerce-subscriptions' ), '<p>', '<a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">', '</a>','</p>' );
.myaccount_avatar {
border-right: 1px solid rgba(0, 0, 0, 0.1);
float: left;
padding-right: 10px;
margin-right: 10px;
width: 139px;
}
@SirDarcanos
SirDarcanos / functions.php
Last active December 22, 2023 16:41
Adding the User Avatar in My Account Page
<?php
/**
* @snippet Adding the User Avatar in My Account Page
* @author Nicola Mustone
* @author_url https://nicolamustone.blog/2014/12/26/add-the-customer-avatar-in-my-account-page-in-storefront/
* @tested-up-to WooCommerce 8.4.X
*/
function nm_myaccount_customer_avatar() {
$current_user = wp_get_current_user();
echo '<div class="myaccount_avatar">' . get_avatar( $current_user->user_email, 128, '', $current_user->display_name ) . '</div>';
@SirDarcanos
SirDarcanos / functions.php
Last active October 30, 2023 07:53
How to Customise the Address Format in WooCommerce
<?php
add_filter( 'woocommerce_localisation_address_formats', 'change_us_address_format' );
function change_us_address_format( $formats ) {
$formats['US'] = "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state} {postcode}\n{country}";
return $formats;
}
@SirDarcanos
SirDarcanos / functions.php
Last active October 30, 2023 10:04
Post-sale cross-sell with WooCommerce
<?php
add_action( 'woocommerce_order_details_after_order_table', 'post_sale_cross_sell' );
function post_sale_cross_sell( $order ) {
$cross_sells = array();
$in_order = array();
foreach ( $order->get_items() as $item ) {
$product_id = $order->get_item( $item )->get_product_id();
$product = wc_get_product( $product_id );
$cross_sells = array_merge( $product->get_cross_sell_ids(), $cross_sells );
@SirDarcanos
SirDarcanos / functions.php
Last active October 30, 2023 07:57
Add an Empty Cart Button in WooCommerce
<?php
add_action( 'woocommerce_cart_coupon', 'woocommerce_empty_cart_button' );
function woocommerce_empty_cart_button() {
echo '<a href="' . esc_url( add_query_arg( 'empty_cart', 'yes' ) ) . '" class="button" title="' . esc_attr( 'Empty Cart', 'woocommerce' ) . '">' . esc_html( 'Empty Cart', 'woocommerce' ) . '</a>';
}
@SirDarcanos
SirDarcanos / functions.php
Last active October 30, 2023 07:56
Add an Empty Cart Button in WooCommerce
<?php
add_action( 'wp_loaded', 'woocommerce_empty_cart_action', 20 );
function woocommerce_empty_cart_action() {
if ( isset( $_GET['empty_cart'] ) && 'yes' === esc_html( $_GET['empty_cart'] ) ) {
WC()->cart->empty_cart();
$referer = wp_get_referer() ? esc_url( remove_query_arg( 'empty_cart' ) ) : wc_get_cart_url();
wp_safe_redirect( $referer );
}
}
@SirDarcanos
SirDarcanos / functions.php
Last active October 30, 2023 10:09
WordPress Discord Post: Send only specific categories
<?php
add_filter( 'wp_discord_post_is_new_post', 'wp_discord_post_limit_by_category' );
function wp_discord_post_limit_by_category( $post ) {
return has_category( 'Your Category Name', $post );
}