Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save plugin-republic/37ad8a1fe0fb6eeef4edd372f47b7186 to your computer and use it in GitHub Desktop.

Select an option

Save plugin-republic/37ad8a1fe0fb6eeef4edd372f47b7186 to your computer and use it in GitHub Desktop.
<?php
<?php
/**
* Snippet: Choose the pricing display per product in WooCommerce Product Add-Ons Ultimate.
*
* List product IDs in the arrays below to set how prices are shown on each product page:
*
* - 'totals' : the standard totals fields (Product total, Options total, Grand total)
* - 'summary' : the summary panel only (no totals fields underneath)
* - 'none' : no pricing summary at all (no summary panel, no totals fields)
*
* Products that aren't in any array keep the global settings in
* WooCommerce > Settings > Product Add-Ons.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Edit these arrays with your product IDs.
*/
function prsnip_pewc_pricing_display_products() {
return array(
'totals' => array( 12, 34 ), // Standard totals fields
'summary' => array( 56, 78 ), // Summary panel
'none' => array( 90 ), // No pricing summary
);
}
/**
* Get the display mode for a product: 'totals', 'summary', 'none', or false if not listed.
*/
function prsnip_pewc_get_pricing_display( $product_id ) {
$product_id = absint( $product_id );
if ( ! $product_id ) {
return false;
}
foreach ( prsnip_pewc_pricing_display_products() as $mode => $ids ) {
if ( in_array( $product_id, array_map( 'absint', $ids ), true ) ) {
return $mode;
}
}
return false;
}
/**
* The add-on fields use the global post ID, so do the same here.
*/
function prsnip_pewc_current_product_id() {
global $post;
return ( $post && isset( $post->ID ) ) ? $post->ID : get_the_ID();
}
/**
* Turn the summary panel on or off per product.
*/
add_filter( 'pewc_enable_summary_panel', 'prsnip_pewc_summary_panel_per_product' );
function prsnip_pewc_summary_panel_per_product( $enable ) {
$mode = prsnip_pewc_get_pricing_display( prsnip_pewc_current_product_id() );
if ( 'summary' === $mode ) {
return 'yes';
}
if ( 'totals' === $mode || 'none' === $mode ) {
return false;
}
return $enable;
}
/**
* Show or hide the totals fields per product.
*/
add_filter( 'pewc_product_show_totals', 'prsnip_pewc_totals_per_product', 10, 2 );
function prsnip_pewc_totals_per_product( $show_totals, $post_id ) {
$mode = prsnip_pewc_get_pricing_display( $post_id );
if ( 'totals' === $mode ) {
return 'all';
}
if ( 'summary' === $mode || 'none' === $mode ) {
return 'none';
}
return $show_totals;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment