Last active
November 2, 2020 21:00
-
-
Save trvswgnr/89ea477219acf5826a2219afbe7c783c to your computer and use it in GitHub Desktop.
Must-use Local Plugin WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Must-use plugin | |
* | |
* @package taw | |
*/ | |
add_action( | |
'admin_head', | |
function() { | |
// force show the product type dropdown. | |
?> | |
<style>.product_data .type_box.hidden { display: block; }</style> | |
<?php | |
} | |
); | |
/** | |
* Add "Empty Cart" button to cart page | |
*/ | |
function mu_woocommerce_empty_cart_button() { | |
echo '<a href="' . esc_url( add_query_arg( 'empty_cart', 'yes' ) ) . '" class="button btn btn-primary ml-2" title="' . esc_attr__( 'Empty Cart', 'woocommerce' ) . '">' . esc_html__( 'Empty Cart', 'woocommerce' ) . '</a>'; | |
} | |
add_action( 'woocommerce_cart_actions', 'mu_woocommerce_empty_cart_button' ); | |
/** | |
* Empty WC cart | |
*/ | |
function mu_woocommerce_empty_cart_action() { | |
if ( isset( $_GET['empty_cart'] ) ) { | |
WC()->cart->empty_cart(); | |
$referer = remove_query_arg( 'empty_cart' ); | |
wp_safe_redirect( $referer ); | |
} | |
} | |
add_action( 'wp_loaded', 'mu_woocommerce_empty_cart_action', 20 ); | |
/** | |
* Dump var wrapped in pre tags for pretty print in HTML | |
* | |
* @param mixed $var Any variable. | |
*/ | |
function pre_dump( $var ) { | |
echo '<pre>'; | |
var_dump( $var ); | |
echo '</pre>'; | |
} | |
/** | |
* Replace local images with remote. | |
* | |
* @param array $image Image. | |
* @return array $image Replaced image. | |
*/ | |
function mu_use_remote_images( $image ) { | |
if ( ! empty( $image[0] ) && defined( 'REMOTE_IMAGE_URL' ) ) { | |
// only replace the url if the image doesn't exist locally. | |
$relative_path = str_replace( site_url() . '/', '', $image[0] ); | |
if ( ! file_exists( ABSPATH . $relative_path ) ) { | |
$image[0] = str_replace( site_url(), rtrim( REMOTE_IMAGE_URL, '/' ), $image[0] ); | |
} | |
} | |
return $image; | |
} | |
if ( defined( 'REMOTE_IMAGE_URL' ) ) { | |
add_filter( 'wp_get_attachment_image_src', 'mu_use_remote_images' ); | |
// disable srcsets since they're not being replaced with remote. | |
add_filter( 'wp_calculate_image_srcset', '__return_false' ); | |
} | |
/** | |
* Add a button to the nav menu that adds the first simple product found to the cart. | |
* | |
* @usage add_filter( 'nav_menu_item_args', 'mu_add_first_product_to_cart' ); | |
* | |
* @see wp_nav_menu() For default args. | |
* @param object $args Nav menu args. | |
* @return object $args Updated args. | |
*/ | |
function mu_add_first_product_to_cart( $args ) { | |
if ( isset( $_GET['mu-add-to-cart'] ) ) { | |
wp_safe_redirect( wc_get_checkout_url() ); | |
} | |
$cart_is_empty = 0 >= WC()->cart->get_cart_contents_count(); | |
$query_args = array( | |
'post_type' => 'product', | |
'posts_per_page' => 1, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'product_type', | |
'field' => 'slug', | |
'terms' => 'simple', | |
), | |
), | |
); | |
$loop = new WP_Query( $query_args ); | |
$link = ''; | |
while ( $loop->have_posts() ) : | |
$loop->the_post(); | |
$params = $cart_is_empty ? site_url( '?mu-add-to-cart=true&add-to-cart=' . get_the_ID() ) : '?empty_cart=yes'; | |
$label = $cart_is_empty ? 'Checkout' : 'Empty'; | |
$link .= '<li class="menu-item nav-item"><a class="nav-link" href="' . $params . '">' . $label . '</li></a>'; | |
endwhile; | |
wp_reset_postdata(); | |
$args->items_wrap = '<ul id="%1$s" class="%2$s">' . $link . '%3$s</ul>'; | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment