Skip to content

Instantly share code, notes, and snippets.

@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 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: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 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>';
.myaccount_avatar {
border-right: 1px solid rgba(0, 0, 0, 0.1);
float: left;
padding-right: 10px;
margin-right: 10px;
width: 139px;
}
/**
* 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>' );
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 )
);
@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',
@SirDarcanos
SirDarcanos / functions.php
Created February 12, 2018 06:04
functions.php
add_filter( 'woocommerce_get_script_data', 'my_strength_meter_custom_strings', 10, 2 );
function my_strength_meter_custom_strings( $data, $handle ) {
if ( 'wc-password-strength-meter' === $handle ) {
$data_new = array(
'i18n_password_error' => esc_attr__( 'Come on, enter a stronger password.', 'theme-domain' ),
'i18n_password_hint' => esc_attr__( 'The password should be at least seven characters long.', 'theme-domain' )
);
$data = array_merge( $data, $data_new );
}
@SirDarcanos
SirDarcanos / untitled
Created February 7, 2018 05:42
functions.php
add_action( 'init', 'custom_init', 999 );
function custom_init() {
$order = wc_get_order( 123 );
$order->set_payment_method_title( 'Credit Card' );
}