Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created May 26, 2019 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/f35363cd1b6f7f99bc2d3648e9fa646d to your computer and use it in GitHub Desktop.
Save plugin-republic/f35363cd1b6f7f99bc2d3648e9fa646d to your computer and use it in GitHub Desktop.
<?php
/**
* A checkbox field template
* @see https://pluginrepublic.com/documentation/overriding-templates/
* @since 2.0.0
* @package WooCommerce Product Add-Ons Ultimate
*/
// Exit if accessed directly
if( ! defined( 'ABSPATH' ) ) {
exit;
}
// Labels are used with the inputs
echo pewc_field_label( $item, $id );
if( isset( $item['field_options'] ) ) {
$index = 0; ?>
<table class="pewc-checkbox-group-wrapper">
<?php foreach( $item['field_options'] as $key=>$option_value ) {
$classes = array( 'pewc-checkbox-form-field' );
$name = esc_html( $option_value['value'] );
$option_percentage = '';
// Set price differently if percentage is enabled
if( pewc_is_pro() && isset( $item['field_percentage'] ) && ! empty( $option_value['price'] ) ) {
// Set the option price as a percentage of the product price
$product_price = $product->get_price();
$option_price = ( floatval( $option_value['price'] ) / 100 ) * $product_price;
$option_price = pewc_maybe_include_tax( $product, $option_price );
$option_percentage = floatval( $option_value['price'] );
$classes[] = 'pewc-option-has-percentage';
} else {
$option_price = ! empty( $option_value['price'] ) ? pewc_maybe_include_tax( $product, $option_value['price'] ) : 0;
}
if( ! empty( $option_price ) ) {
$name .= ' - <span class="pewc-option-cost-label">' . pewc_get_semi_formatted_raw_price( $option_price ) . '</span>';
}
$radio_id = $id . '_' . strtolower( str_replace( ' ', '_', $option_value['value'] ) );
$checked = ( is_array( $value ) && in_array( $option_value['value'], $value ) ) ? 'checked="checked"' : '';
$row = '<tr>';
// Checkbox first
$row .= sprintf(
'<td><input data-option-cost="%s" data-option-percentage="%s" type="checkbox" name="%s[]" id="%s" class="%s" value="%s" %s></td>',
esc_attr( $option_price ),
esc_attr( $option_percentage ),
esc_attr( $id ),
esc_attr( $radio_id ),
join( ' ', $classes ),
esc_attr( $option_value['value'] ),
esc_attr( $checked )
);
$row .= sprintf(
'<td><label class="pewc-checkbox-form-label" for="%s">%s</label></td>',
esc_attr( $radio_id ),
$name
);
// echo apply_filters( 'pewc_filter_checkbox_group_field', $radio, $radio_id, $option_price, $id, $name, $option_value, $item );
}
echo $row; ?>
</table>
<?php }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment