Skip to content

Instantly share code, notes, and snippets.

@growdev
Created March 16, 2022 15:43
Show Gist options
  • Save growdev/137d7460787d11f3c0d1a19c71781db6 to your computer and use it in GitHub Desktop.
Save growdev/137d7460787d11f3c0d1a19c71781db6 to your computer and use it in GitHub Desktop.
Add textbox to get subscription details
<?php
/**
* Form used for add to existing subscription from the single product page.
*
* @author Shop Plugins
* @category WooCommerce Subscriptions/Templates
* @version 2.0.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! $product->is_purchasable() ) {
return;
}
?>
<textarea style="height: 650px; width: 300px;"><?php
// All subscriptions for customer
// Note: this does not check if subscription is Active
// set in Javorszky\Toolbox\add_to_existing_sub_markup()
foreach ($subscriptions as $subscription ) {
// Get all line items in the current subscription.
$items = $subscription->get_items();
// Loop through all line items.
foreach ( $items as $item ) {
// Output the subscription ID and the line item name.
echo $subscription->get_id() . '-'. $item->get_name() . PHP_EOL;
}
}
?></textarea>
<form action="#" method="POST" class="jgtb-add-to-subscription">
<label for="jgtb_add_to_existing">CHILD THEME Add to existing subscription?<small>please note that the renewal schedule will be the original subscription's</small></label>
<select name="jgtb_add_to_existing_subscription" id="jgtb_add_to_existing">
<option value="null">
<?php echo esc_html_x( '-- Select an existing subscription --', 'default option in dropdown in add to existing subscription template', 'jg-toolbox' ); ?>
</option>
<?php
foreach ( $subscriptions as $subscription ) {
echo wp_kses( sprintf( '<option value="%s">%s</option>', $subscription->get_id(), Javorszky\Toolbox\Utilities\generate_option_text( $subscription ) ), array( 'option' => array( 'value' => array() ) ) );
}
unset( $subscription );
?>
</select>
<?php
foreach ( $subscriptions as $subscription ) {
$items_string = Javorszky\Toolbox\Utilities\generate_nonce_on_items( $subscription );
wp_nonce_field( 'add_to_subscription_' . $items_string, 'jgtbwpnonce_' . $subscription->get_id(), false );
}
?>
<input type="hidden" name="ats_quantity" value="0">
<input type="hidden" name="ats_product_id" value="<?php echo esc_attr( $product->get_id() ); ?>">
<input type="hidden" name="ats_variation_id" value="0">
<input type="hidden" name="ats_variation_attributes" value="">
<button type="submit" name="add-to-subscription" disabled="disabled" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt">
<?php echo esc_html_x( 'Add to existing subscription', 'Text on button for add to existing subscription functionality', 'jg-toolbox' ); ?>
</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment