Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Last active June 24, 2020 22:53
Show Gist options
  • Save tradesouthwest/2ba3496558df5529c86793971a8b71fe to your computer and use it in GitHub Desktop.
Save tradesouthwest/2ba3496558df5529c86793971a8b71fe to your computer and use it in GitHub Desktop.
Only display YITH Request A Quote button on Woo pages. Parent child relations. Woocommerce
<?php
/**
* Only display YITH Request A Quote button on Woo pages.
* @uses Shortcode from YITH
* Maybe translate manually. && $post->post_parent > 0
* @see https://developer.wordpress.org/reference/functions/is_page/
*/
function betheme_child_request_aquote_echo( )
{
if ( is_home() || is_front_page() || is_page( array(
'about',
'resources',
'services',
'industries',
'contact',
'component-recycling-program' )
) ) {
return;
}
if ( betheme_child_check_for_display_button() == true ) :
// Get quotes count in (cart)
$quote_cnt = '';
$quote_cnt = do_shortcode('[yith_ywraq_number_items class="qcnt" item_name="Item" item_plural_name="Items" show_url="no"]');
// Clean and echo
ob_start();
echo '<a class="action_button yith-items-button" href="' . home_url( '/' ) . 'request-quote/" title="quotes">
<span id="yithQuoteItem"><i class="fa fa-cart"></i> <span id="quote_quantity"> ' . $quote_cnt . ' </span>
<span class="lnkspc"> &nbsp;' . __( "in Quote" ) . '</span></span></a>';
$qbutton = ob_get_clean();
echo $qbutton;
else:
return false;
endif;
}
add_action( 'betheme_child_request_aquote_hook', 'betheme_child_request_aquote_echo', 10, 1 );
// Quick Boolean check for if on right page.
function betheme_child_check_for_display_button()
{
global $post;
//$product = wc_get_product( $post->ID );
// Check if on the right page(s).
if ( is_woocommerce() || is_page( 'catalog' ) || '3360' == $post->post_parent
|| is_page( 'request-quote' ) || is_page( 'products' ) || '2852' == $post->post_parent
|| get_posts_children( array( '2852', '3360' ) ) ) :
return true;
else:
return false;
endif;
}
/**
* Find if post has a parent in particular category
*/
function get_posts_children($parent_id)
{
$children = array();
// grab the posts children
$posts = get_posts( array( 'numberposts' => -1,
'post_status' => 'publish',
//'post_type' => 'any',
'post_parent' => $parent_id,
'suppress_filters' => false
) );
// now grab the grand children
foreach( $posts as $child ){
// recursion!! hurrah
$gchildren = get_posts_children($child->ID);
// merge the grand children into the children array
if( !empty($gchildren) ) {
$children = array_merge($children, $gchildren);
}
}
// merge in the direct descendants we found earlier
$children = array_merge($children,$posts);
return $children;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment