Skip to content

Instantly share code, notes, and snippets.

@davemac
Last active May 16, 2017 10:56
Show Gist options
  • Save davemac/7c153c0a015820cdcb84271f248fb876 to your computer and use it in GitHub Desktop.
Save davemac/7c153c0a015820cdcb84271f248fb876 to your computer and use it in GitHub Desktop.
WooCommerce check if current product is being used in a Mix and Match parent product
global $wpdb;
$dmc_mnm_child_id = $product->get_id();
$querystr = "
SELECT *
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key = '_mnm_data'
AND $wpdb->postmeta.meta_value LIKE '%{$dmc_mnm_child_id}%'
";
$pageposts = $wpdb->get_results( $querystr, OBJECT );
// yes, it is
if( $pageposts != NULL ) {
// get mix n match parent product id
$dmc_mnm_parent_id = $pageposts[0]->post_id;
// get mix n match parent product permalink from the id
$dmc_mnm_parent_id = get_permalink( $dmc_mnm_parent_id );
// $dmc_mnm_parent_title = get_title( $dmc_mnm_parent_id );
// echo $dmc_mnm_parent_title;
// check referrering URL is from the mix n match parent product
if ( wp_get_referer() )
{
$dmc_referrer = wp_get_referer();
if ( strpos( $dmc_referrer, 'product' ) !== false)
// it is, so use the browser back button to maintain state
{ ?>
<a href="<?php echo esc_url( $dmc_mnm_parent_id ); ?>" class="single_buy_in_case_button button alt" onclick="history.go(-1);"><?php echo _e( 'Back to Your Mixed Case' ); ?></a>
<?php
} else {
// if not, use the default link back to the mix n match parent product ?>
<a href="<?php echo esc_url( $dmc_mnm_parent_id ); ?>" class="single_buy_in_case_button button alt"><?php echo _e( 'Buy in a Mixed Case' ); ?></a>
<?php
}; // end check referrering URL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment