Skip to content

Instantly share code, notes, and snippets.

@Garconis
Created January 7, 2020 16:28
Show Gist options
  • Save Garconis/92574ee0becb8edcefac579d5a54ca8a to your computer and use it in GitHub Desktop.
Save Garconis/92574ee0becb8edcefac579d5a54ca8a to your computer and use it in GitHub Desktop.
Faddegons
/**
* add a WISIHLIST LINK in the WooCommerce MY ACOUNT sidebar tab area
**/
add_filter ( 'woocommerce_account_menu_items', 'woo_my_wishlist' );
function woo_my_wishlist( $menu_links ){
// we will hook "my_wishlist" later
$new = array( 'my_wishlist' => 'My Wishlist' );
// array_slice() is good when you want to add an element between the other ones
$menu_links = array_slice( $menu_links, 0, 1, true )
+ $new
+ array_slice( $menu_links, 1, NULL, true );
return $menu_links;
}
// tell the new WooCommerce account endpoint where to link to
add_filter( 'woocommerce_get_endpoint_url', 'woo_my_wishlist_hook_endpoint', 10, 4 );
function woo_my_wishlist_hook_endpoint( $url, $endpoint, $value, $permalink ){
if( $endpoint === 'my_wishlist' ) {
// ok, here is the place for your custom URL, it could be external
$url = site_url('/my-wishlist/');
}
return $url;
}
/**
* Adds WooCommerce catalog SORTING OPTIONS using postmeta, such as custom fields
* Tutorial: http://www.skyverge.com/blog/sort-woocommerce-products-custom-fields/
* for the $sort_args['orderby'] = , you should use 'meta_value_num' if sorting by an actual number instead of text value
**/
function skyverge_add_postmeta_ordering_args( $sort_args ) {
// checks if Order By is enabled and also checks the default option (which we also set within the Options)
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {
// Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
case 'plant_common_name':
$sort_args['orderby'] = 'meta_value';
// Sort by meta_value because we're using alphabetic sorting
$sort_args['order'] = 'asc';
$sort_args['meta_key'] = 'common_name';
// use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
break;
case 'plant_botanical_name':
$sort_args['orderby'] = 'meta_value';
// Sort by meta_value because we're using alphabetic sorting
$sort_args['order'] = 'asc';
$sort_args['meta_key'] = 'genus';
// use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
break;
case 'plant_stock':
$sort_args['orderby'] = 'meta_value';
// We use meta_value_num here if value is a number and we want to sort in numerical order
$sort_args['order'] = 'asc';
$sort_args['meta_key'] = '_stock_status';
break;
}
return $sort_args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'skyverge_add_postmeta_ordering_args' );
// Add these new sorting arguments to the sortby options on the FRONTEND
function skyverge_add_new_postmeta_orderby( $sortby ) {
// Adjust the text as desired
$sortby['plant_common_name'] = __( 'Sort by Common Name', 'woocommerce' );
$sortby['plant_botanical_name'] = __( 'Sort by Botanical Name', 'woocommerce' );
$sortby['plant_stock'] = __( 'Sort by In Stock', 'woocommerce' );
return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'skyverge_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'skyverge_add_new_postmeta_orderby' );
/**
* Removes "Sorting" options from the shop template
*
* Can be used to unset other keys / sorting options instead
* Ref: https://github.com/woothemes/woocommerce/blob/master/includes/wc-template-functions.php#L654
**/
function skyverge_remove_default_sorting_option( $catalog_orderby_options ) {
unset( $catalog_orderby_options['popularity'] );
unset( $catalog_orderby_options['rating'] );
unset( $catalog_orderby_options['date'] );
// unset( $catalog_orderby_options['price'] );
// unset( $catalog_orderby_options['price-desc'] );
return $catalog_orderby_options;
}
add_filter( 'woocommerce_catalog_orderby', 'skyverge_remove_default_sorting_option' );
/**
* REMOVE the product TITLE within the loop, then re ADD the product title based on criteria
**/
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'fs_products_title_change_in_loop', 10 );
function fs_products_title_change_in_loop() {
global $product;
// Get the custom fields values
$custom_field_genus = get_post_meta( $product->get_id(), 'genus', true );
$custom_field_species = get_post_meta( $product->get_id(), 'species', true );
$custom_field_common = get_post_meta( $product->get_id(), 'common_name', true );
$custom_field_variety = get_post_meta( $product->get_id(), 'variety', true );
$custom_field_series = get_post_meta( $product->get_id(), 'series_name', true );
$custom_field_class = get_post_meta( $product->get_id(), 'class_name', true );
// checks if Order By is enabled and also checks the default option (which we also set within the Options)
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
/* If orderby Botanical or Stock, change title of products. Note, the Botanical orderby only returns results of products that have a Genus field (since thats how the Orderby works) */
if ( ('plant_botanical_name' == $orderby_value) || ('plant_stock' == $orderby_value) ) {
echo '<h2 class="woocommerce-loop-product__title">'.$custom_field_genus.' '.$custom_field_species.' '.$custom_field_variety.' '.$custom_field_series.' '.$custom_field_class.'</h2>';
}
/* Otherwise respit out the regular one */
else {
echo '<h2 class="woocommerce-loop-product__title">' . get_the_title() . '</h2>';
}
}
/**
* add some custom text (fields) BELOW each product TITLE within the product loop
**/
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom fields values
$custom_field_genus = get_post_meta( $product->get_id(), 'genus', true );
$custom_field_species = get_post_meta( $product->get_id(), 'species', true );
$custom_field_common = get_post_meta( $product->get_id(), 'common_name', true );
$custom_field_variety = get_post_meta( $product->get_id(), 'variety', true );
$custom_field_series = get_post_meta( $product->get_id(), 'series_name', true );
$custom_field_class = get_post_meta( $product->get_id(), 'class_name', true );
$custom_field_stock = get_post_meta( $product->get_id(), '_stock', true );
$custom_field_stock_status = get_post_meta( $product->get_id(), '_stock_status', true );
// checks if Order By is enabled and also checks the default option (which we also set within the Options)
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
// Display the Common Name if it exists and the sorting is by Botanical
if( ($custom_field_common) && ('plant_botanical_name' == $orderby_value) ){
echo '<p class="fs-custom-product-field"><span class="fs-custom-product-field-common">'.$custom_field_common.'</span></p>';
}
// Otherwise display the Common Name if it exists and the sorting is by Stock
elseif( ($custom_field_common) && ('plant_stock' == $orderby_value) ){
echo '<p class="fs-custom-product-field"><span class="fs-custom-product-field-common">'.$custom_field_common.'</span></p>';
}
// Otherwise display Genus + Species... if they exist
elseif( $custom_field_genus || $custom_field_species ){
echo '<p class="fs-custom-product-field"><span class="fs-custom-product-field-genus">'.$custom_field_genus.'</span> <span class="fs-custom-product-field-species">'.$custom_field_species.'</span></p>';
}
/**
* TESTING for stock count of parent and stock status of entire item
*
// Display Stock Count
if( $custom_field_stock ){
echo '<p class="fs-custom-product-field"><span class="fs-custom-product-field-stock">Stock: '.$custom_field_stock.'</span></p>';
}
// Display Stock
if( $custom_field_stock_status ){
echo '<p class="fs-custom-product-field"><span class="fs-custom-product-field-stock-status">Stock: '.$custom_field_stock_status.'</span></p>';
}
// If the Botanical is the orderby, then display this stuff
if ( 'plant_botanical_name' == $orderby_value ) {
// Display handled later through a hook
echo '<p class="fs-custom-product-field"><span class="fs-custom-product-field-orderby">Botanical: '.$custom_field_genus.'</span></span></p>';
}
*/
}
/**
* Add LABELS to the product based on criteria within the loop
**/
add_action('woocommerce_before_shop_loop_item_title', 'fs_custom_loop_labels', 11);
function fs_custom_loop_labels(){
global $product;
// Get the custom fields values
$custom_field_stock_status = get_post_meta( $product->get_id(), '_stock_status', true );
$new_term_label = get_field('plant_new_label', 'option');
$featured_term_label = get_field('plant_featured_label', 'option');
echo '<div class="fs-product-loop-labels-wrapper">';
if ( $custom_field_stock_status == 'instock' ) {
echo '<div class="fs-product-loop-label fs-product-loop-label-instock"><span>In Stock</span></div>';
}
if ( has_term( 'new', 'product_cat', $product->post ) ) {
echo '<div class="fs-product-loop-label fs-product-loop-label-new"><span>'. $new_term_label .'</span></div>';
}
if ( has_term( 'featured', 'product_cat', $product->post ) ) {
echo '<div class="fs-product-loop-label fs-product-loop-label-featured"><span>'. $featured_term_label .'</span></div>';
}
echo '</div>';
}
/**
* Change Featured category name on the frontend
* if on the product page of Featured, then change the "Featured" text to be custom text instead...
* which we change to the custom text of the ACF "Featured Label" field
**/
function change_woocommerce_category_page_title( $page_title ) {
if ( ( is_product_category() ) && ( 'Featured' == $page_title ) ) {
$featured_term_label = get_field('plant_featured_label', 'option');
// $page_title = "Plants for Success";
$page_title = $featured_term_label;
}
return $page_title;
}
add_filter( 'woocommerce_page_title', 'change_woocommerce_category_page_title', 10, 1 );
// Define a new breakpoint for woocommerce-smallscreen.css - fix issue for shop page on ipad
add_filter('woocommerce_style_smallscreen_breakpoint','woo_custom_breakpoint');
function woo_custom_breakpoint($px) {
$px = '767px';
return $px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment