Skip to content

Instantly share code, notes, and snippets.

@jentheo
Created September 3, 2018 22:25
Show Gist options
  • Save jentheo/ac495e685f3c067753aed225b94a4ce0 to your computer and use it in GitHub Desktop.
Save jentheo/ac495e685f3c067753aed225b94a4ce0 to your computer and use it in GitHub Desktop.
<?php
// Function to add credits to cart (in main class)
public function listen_for_credit_purchases() {
// if ( ! wp_verify_nonce( '' ) ) {
// die( '-1' );
// }
$product_quantities = $_POST['product_quantity'];
//check if this array has value
if ( ! empty( $product_quantities ) ) {
foreach ( $product_quantities as $name => $value ) {
if ( $value > 0 ) {
$product_id = $_POST['product_id'];
WC()->cart->add_to_cart( $product_id, $value );
}
}
}
}
// Purchase credits table
?>
<div class="tribe-community-events-content community-charging-purchase-credits">
<form
id="community-events-purchase-credits"
action="<?php echo esc_url( wc_get_cart_url() ) ?>"
class="<?php //echo esc_attr( implode( ' ', $cart_classes ) ); ?>"
method="post"
enctype='multipart/form-data'
novalidate
>
<?php //if there are no credits on the account, don't show this table. Instead, show a message to go to the purchase page ?>
<h2 class="tribe-community-events-list-title"><?php echo esc_html__( 'My Credits', 'tribe-ext-charge-for-submitting-featuring-events' ); ?></h2>
<div class="tribe-responsive-table-container">
<table id="tribe-community-credits-list" class="tribe-community-events-list my-events display responsive stripe">
<thead>
<tr>
<th>
<?php echo esc_html__( 'Quantity', 'tribe-ext-charge-for-submitting-featuring-events' ); ?>
</th>
<th>
<?php echo esc_html__( 'Credit', 'tribe-ext-charge-for-submitting-featuring-events' ); ?>
</th>
<th>
<?php echo esc_html__( 'Description', 'tribe-ext-charge-for-submitting-featuring-events' ); ?>
</th>
<th>
<?php echo esc_html__( 'Cost', 'tribe-ext-charge-for-submitting-featuring-events' ); ?>
</th>
</tr>
</thead>
<tbody>
<!-- add nonce -->
<?php foreach ( $available_credits as $credit => $id ) { ?>
<?php $product = wc_get_product( $id ); ?>
<tr>
<td>
<input type="number" class="input-text qty text" name="product_quantity[<?php echo esc_attr( $id ); ?>]">
<input type="hidden" name="product_id[]" value="<?php echo esc_attr( $id ); ?>">
</td>
<td>
<?php echo get_the_title( $id );?>
</td>
<td>
<?php //$myvar = $product->post->post_excerpt;?>
<?php echo get_the_excerpt( $id ); ?>
</td>
<td>
<?php echo $product->get_price();?>
</td>
</tr>
<?php } ?>
<tr>
<td>
<button
type="submit"
name="credits_add_to_cart"
value="1"
class="tribe-button tribe-button-primary add-new"
>
<?php esc_html__( 'Buy now', 'tribe-ext-charge-for-submitting-featuring-events' ); ?>
</button>
</td>
</tr>
</tbody>
</table>
</div>
<div class="tribe-community tribe-community-no-credits">
<i><p>No credits currently available message</p></i>
</div>
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment