Skip to content

Instantly share code, notes, and snippets.

@tamara-m
Forked from plugin-republic/products.php
Created July 20, 2021 16:37
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 tamara-m/f624be9108659f704200a613dd880816 to your computer and use it in GitHub Desktop.
Save tamara-m/f624be9108659f704200a613dd880816 to your computer and use it in GitHub Desktop.
<?php
/**
* A products field template
* Ensure that the 'Products Quantities' field is set to 'One Only'
* @since 2.2.0
* @package WooCommerce Product Add Ons Ultimate
*/
// Exit if accessed directly
if( ! defined( 'ABSPATH' ) ) {
exit;
}
echo pewc_field_label( $item, $id );
if( isset( $item['child_products'] ) ) {
$layout = $item['products_layout'];
$index = 0;
// Set the allow_none parameter according to whether the field is required or not
// allow_none means that child products can be deleted from the cart without deleting the parent product
$allow_none = empty( $item['required'] ) ? true : false;
// This is the custom layout
$number_columns = ( isset( $item['number_columns'] ) ) ? $item['number_columns'] : 3;
$checkboxes_wrapper_classes = array(
'pewc-checkboxes-images-wrapper',
'child-product-wrapper'
);
$checkboxes_wrapper_classes[] = 'pewc-columns-' . intval( $number_columns );
if( ! empty( $item['hide_labels'] ) ) {
$checkboxes_wrapper_classes[] = 'pewc-hide-labels';
}
if( ! empty( $item['products_quantities'] ) ) {
$products_quantities = ! empty( $item['products_quantities'] ) ? $item['products_quantities'] : '';
$checkboxes_wrapper_classes[] = 'products-quantities-' . $item['products_quantities'];
} ?>
<table class="<?php echo join( ' ', $checkboxes_wrapper_classes ); ?>" data-products-quantities="<?php echo esc_attr( $item['products_quantities'] ); ?>">
<?php foreach( $item['child_products'] as $child_product_id ) {
$child_product = wc_get_product( $child_product_id );
$price = pewc_maybe_include_tax( $child_product, $child_product->get_price() );
$price = wc_price( $price );
// Check stock availability
$disabled = '';
if( ! $child_product->is_purchasable() || ! $child_product->is_in_stock() ) {
$disabled = 'disabled';
}
// Check available stock if stock is managed
$available_stock = '';
if( $child_product->managing_stock() ) {
$available_stock = $child_product->get_stock_quantity();
}
$name = get_the_title( $child_product_id );
$field_name = $id . '_child_product';
$checkbox_id = $id . '_' . $child_product_id;
$wrapper_classes = array(
'pewc-checkbox-image-wrapper'
);
if( $disabled ) {
$wrapper_classes[] = 'pewc-checkbox-disabled';
}
$checked = ( $value == $id ) ? 'checked="checked"' : '';
$quantity_field = '';
if( $products_quantities == 'independent' ) {
// Add a quantity field for each child checkbox
// The name format is {$id}_child_quantity_{$child_product_id}
// Where $id is the field ID and $child_product_id is the child product ID
$quantity_field = sprintf(
'<input type="number" min="0" step="1" max="%s" class="pewc-form-field pewc-child-quantity-field" name="%s" value="0" %s>',
$available_stock,
esc_attr( $id ) . '_child_quantity_' . esc_attr( $child_product_id ),
$disabled
);
}
$option_cost = pewc_maybe_include_tax( $child_product, $child_product->get_price() );
// Start the row
$table_row = '<tr>';
// Add a thumbnail
$image_url = ( get_post_thumbnail_id( $child_product_id ) ) ? wp_get_attachment_url( get_post_thumbnail_id( $child_product_id ) ) : wc_placeholder_img_src();
// Setting a width and height here
$image = '<img style="max-width: 50px; height: auto;" src="' . esc_url( $image_url ) . '">';
$table_row .= sprintf(
'<td>%s</td>',
$image
);
// Add a checkbox in the first column
$table_row .= sprintf(
'<td><input data-option-cost="%s" type="checkbox" name="%s[]" id="%s" class="pewc-checkbox-form-field" value="%s" %s %s></td>',
esc_attr( $option_cost ),
esc_attr( $field_name ),
esc_attr( $checkbox_id ),
esc_attr( $child_product_id ),
esc_attr( $checked ),
esc_attr( $disabled )
);
// Add the child product name
$table_row .= sprintf(
'<td class="%s"><label for="%s">%s</label></td>',
join( ' ', $wrapper_classes ),
esc_attr( $checkbox_id ),
$name
);
// Example column to get child product meta, e.g. SKU
$sku = $child_product->get_sku();
$table_row .= sprintf(
'<td>%s</td>',
$sku
);
// Price column
$table_row .= sprintf(
'<td>%s</td>',
$price
);
$table_row .= '</row>';
echo $table_row;
} ?>
</table><!-- .pewc-radio-images-wrapper -->
<input type="hidden" name="<?php echo esc_attr( $id ); ?>_quantities" value="<?php echo esc_attr( $item['products_quantities'] ); ?>">
<?php $allow_none = ! empty( $item['allow_none'] ) ? 1 : 0; ?>
<input type="hidden" name="<?php echo esc_attr( $id ); ?>_allow_none" value="<?php echo esc_attr( $allow_none ); ?>">
<?php }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment