Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions includes/Admin/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public static function add_campaign() {
'post_content' => $cause,
'post_status' => $status,
'meta_input' => array(
'wcdm_goal_amount' => $goal_amount,
'_end_date' => $end_date,
'wcdm_goal_amount' => $goal_amount,
'_end_date' => $end_date,
'wcdm_goal_behavior' => ( ! empty( $_POST['goal_behavior'] ) ? sanitize_key( wp_unslash( $_POST['goal_behavior'] ) ) : get_option( 'wcdm_goal_behavior', 'continue' ) ),
),
);

Expand Down Expand Up @@ -96,8 +97,9 @@ public static function edit_campaign() {
'post_content' => wp_kses_post( $cause ),
'post_status' => $status,
'meta_input' => array(
'wcdm_goal_amount' => $goal_amount,
'_end_date' => $end_date,
'wcdm_goal_amount' => $goal_amount,
'_end_date' => $end_date,
'wcdm_goal_behavior' => ( ! empty( $_POST['goal_behavior'] ) ? sanitize_key( wp_unslash( $_POST['goal_behavior'] ) ) : get_option( 'wcdm_goal_behavior', 'continue' ) ),
),
);

Expand Down
12 changes: 12 additions & 0 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ public function get_settings( $tab ) {
'default' => 'no',
'type' => 'checkbox',
),
array(
'title' => __( 'Donation behaviour after goal completion', 'wc-donation-manager' ),
'desc' => __( 'Define what should happen when a campaign reaches its goal.', 'wc-donation-manager' ),
'id' => 'wcdm_goal_behavior',
'type' => 'select',
'default' => 'continue',
'options' => array(
'continue' => __( 'Continue accepting donations', 'wc-donation-manager' ),
'close' => __( 'Close campaign automatically', 'wc-donation-manager' ),
'soft_close' => __( 'Show as completed but still allow donations', 'wc-donation-manager' ),
),
),
array(
'type' => 'sectionend',
'id' => 'advanced_options',
Expand Down
17 changes: 17 additions & 0 deletions includes/Admin/views/campaigns/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@
</p>
</td>
</tr>

<tr valign="top">
<th scope="row">
<label for="goal_behavior"><?php esc_html_e( 'Donation behavior after goal', 'wc-donation-manager' ); ?></label>
</th>
<td>
<select id="goal_behavior" name="goal_behavior" class="regular-text">
<?php $default_behavior = get_option( 'wcdm_goal_behavior', 'continue' ); ?>
<option value="continue" <?php echo 'continue' === $default_behavior ? 'selected' : ''; ?>><?php esc_html_e( 'Continue accepting donations', 'wc-donation-manager' ); ?></option>
<option value="close" <?php echo 'close' === $default_behavior ? 'selected' : ''; ?>><?php esc_html_e( 'Close campaign automatically', 'wc-donation-manager' ); ?></option>
<option value="soft_close" <?php echo 'soft_close' === $default_behavior ? 'selected' : ''; ?>><?php esc_html_e( 'Show as completed but still allow donations', 'wc-donation-manager' ); ?></option>
</select>
<p class="description">
<?php esc_html_e( 'Select the behavior when this campaign reaches its goal.', 'wc-donation-manager' ); ?>
</p>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="status"><?php esc_html_e( 'Status', 'wc-donation-manager' ); ?></label>
Expand Down
15 changes: 15 additions & 0 deletions includes/Admin/views/campaigns/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@
</p>
</div>
</div>

<div class="pev-form-field">
<label for="goal_behavior">
<?php esc_html_e( 'Donation behavior after goal', 'wc-donation-manager' ); ?>
</label>
<div class="field-group">
<?php $behavior = get_post_meta( $campaign->ID, 'wcdm_goal_behavior', true ); ?>
<select id="goal_behavior" name="goal_behavior" style="width: 300px">
<option value="continue" <?php echo 'continue' === $behavior ? 'selected' : ''; ?>><?php esc_html_e( 'Continue accepting donations', 'wc-donation-manager' ); ?></option>
<option value="close" <?php echo 'close' === $behavior ? 'selected' : ''; ?>><?php esc_html_e( 'Close campaign automatically', 'wc-donation-manager' ); ?></option>
<option value="soft_close" <?php echo 'soft_close' === $behavior ? 'selected' : ''; ?>><?php esc_html_e( 'Show as completed but still allow donations', 'wc-donation-manager' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'Select the behavior when this campaign reaches its goal.', 'wc-donation-manager' ); ?></p>
</div>
</div>
</div>
</div>
</div>
Expand Down
25 changes: 25 additions & 0 deletions includes/Donation/class-donation-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ public function is_sold_individually() {
return true;
}

/**
* Override purchasable behavior to respect campaign goal behavior.
*
* @since 1.0.0
* @return bool
*/
public function is_purchasable() {
$base = parent::is_purchasable();
if ( ! $base ) {
return false;
}

$campaign_id = get_post_meta( $this->get_id(), 'wcdm_campaign_id', true );
if ( $campaign_id ) {
$behavior = wcdm_get_goal_behavior( $campaign_id );
$raised = (float) get_post_meta( $campaign_id, '_raised_amount', true );
$goal = (float) get_post_meta( $campaign_id, 'wcdm_goal_amount', true );
if ( 'close' === $behavior && $goal > 0 && $raised >= $goal ) {
return false;
}
}

return true;
}

/**
* Get the add to cart button text.
*
Expand Down
5 changes: 5 additions & 0 deletions includes/Frontend/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public static function order_status_completed( $order_id, $order ) {
$raised_amount += (float) $item['subtotal'];
update_post_meta( $item['product_id'], 'wcdm_orders_id', $orders_id );
update_post_meta( $campaign_id, '_raised_amount', $raised_amount );
$behavior = wcdm_get_goal_behavior( $campaign_id );
$goal = (float) get_post_meta( $campaign_id, 'wcdm_goal_amount', true );
if ( 'close' === $behavior && $goal > 0 && $raised_amount >= $goal ) {
update_post_meta( $campaign_id, 'wcdm_goal_completed', 'yes' );
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions includes/Frontend/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ public static function add_to_cart_template(): void {
return;
}

$raised = (float) get_post_meta( $campaign_id, '_raised_amount', true );
$goal = (float) get_post_meta( $campaign_id, 'wcdm_goal_amount', true );
$behavior = wcdm_get_goal_behavior( $campaign_id );
if ( $goal > 0 && $raised >= $goal ) {
if ( 'close' === $behavior ) {
printf( '%1$s%2$s%3$s', '<p class="expired">', esc_html__( 'This campaign has reached its goal and is now closed.', 'wc-donation-manager' ), '</p>' );
return;
}
if ( 'soft_close' === $behavior ) {
printf( '%1$s%2$s%3$s', '<p class="completed">', esc_html__( 'This campaign has reached its goal but is still accepting donations.', 'wc-donation-manager' ), '</p>' );
}
}

do_action( 'woocommerce_simple_add_to_cart' );
}

Expand Down
14 changes: 14 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ function wcdm_get_campaign_products( $campaign_id ) {
return $query->posts;
}

/**
* Get goal behavior for a campaign.
*
* @param int $campaign_id Campaign ID.
* @return string
*/
function wcdm_get_goal_behavior( $campaign_id ) {
$behavior = get_post_meta( $campaign_id, 'wcdm_goal_behavior', true );
if ( ! $behavior ) {
$behavior = get_option( 'wcdm_goal_behavior', 'continue' );
}
return $behavior;
}

/**
* Get the post title.
*
Expand Down
Loading