---
title: Custom Discount Integration
description: "This guide covers how to integrate Smart Cycle Discounts with custom code, other plugins, and external systems. Learn how to programmatically interact with campaigns and discounts. --- ## Checking if Plugin is Active ### PHP Check ``` // Check if…"
url: "https://webstepper.io/docs/smart-cycle-discounts/developer-documentation/custom-discount-integration/"
updated: "2026-02-17"
category: Developer Documentation
---

# Custom Discount Integration

This guide covers how to integrate Smart Cycle Discounts with custom code, other plugins, and external systems. Learn how to programmatically interact with campaigns and discounts.

---

## Checking if Plugin is Active

### PHP Check

```
// Check if Smart Cycle Discounts is active
if ( class_exists( 'Smart_Cycle_Discounts' ) ) {
    // Plugin is active, safe to use SCD functions
}

// Or check for main class
if ( function_exists( 'SCD' ) ) {
    $scd = wsscd();
}
```

### Wait for Plugin Load

```
add_action( 'plugins_loaded', function() {
    if ( ! class_exists( 'Smart_Cycle_Discounts' ) ) {
        return;
    }

    // Your integration code here
}, 20 );
```

---

## Getting Campaign Information

### Get All Active Campaigns

```
$campaign_manager = wsscd()->get_service( 'campaign_manager' );
$active_campaigns = $campaign_manager->get_active_campaigns();

foreach ( $active_campaigns as $campaign ) {
    echo $campaign->get_name();
    echo $campaign->get_discount_value();
}
```

### Get Campaign by ID

```
$campaign = $campaign_manager->get_campaign( 123 );

if ( $campaign ) {
    $name = $campaign->get_name();
    $status = $campaign->get_status();
    $discount_type = $campaign->get_discount_type();
    $discount_value = $campaign->get_discount_value();
}
```

### Check Campaign Status

```
$campaign = $campaign_manager->get_campaign( 123 );

if ( $campaign->is_active() ) {
    // Campaign is currently active
}

if ( $campaign->is_scheduled() ) {
    // Campaign is scheduled for future
}
```

---

## Getting Product Discounts

### Get Discounted Price for Product

```
$discount_engine = wsscd()->get_service( 'discount_engine' );

$product_id = 45;
$original_price = 100.00;

$discounted_price = $discount_engine->get_discounted_price( $product_id, $original_price );

if ( $discounted_price !== $original_price ) {
    echo "Product is on sale: " . wc_price( $discounted_price );
}
```

### Get Campaign Applying to Product

```
$campaign = $discount_engine->get_campaign_for_product( $product_id );

if ( $campaign ) {
    echo "Discount from: " . $campaign->get_name();
}
```

### Check if Product Has Any Discount

```
$has_discount = $discount_engine->product_has_discount( $product_id );

if ( $has_discount ) {
    // Product is in an active campaign
}
```

### Get All Discounted Products

```
$campaign_id = 123;
$product_ids = $campaign_manager->get_campaign_products( $campaign_id );

// Returns array of product IDs
```

---

## Creating Campaigns Programmatically

### Create a Simple Campaign

```
$campaign_data = array(
    'name'            => 'API Created Campaign',
    'status'          => 'draft',
    'priority'        => 3,
    'discount_type'   => 'percentage',
    'discount_value'  => 25,
    'start_date'      => '2025-07-01 00:00:00',
    'end_date'        => '2025-07-31 23:59:59',
    'product_selection' => array(
        'type' => 'all',
    ),
);

$campaign_id = $campaign_manager->create_campaign( $campaign_data );

if ( $campaign_id ) {
    // Campaign created successfully
    $campaign_manager->activate_campaign( $campaign_id );
}
```

### Create Campaign with Conditions

```
$campaign_data = array(
    'name'            => 'Category Sale',
    'status'          => 'scheduled',
    'priority'        => 3,
    'discount_type'   => 'percentage',
    'discount_value'  => 20,
    'start_date'      => '2025-07-01 00:00:00',
    'end_date'        => '2025-07-31 23:59:59',
    'product_selection' => array(
        'type' => 'conditions',
        'conditions' => array(
            array(
                'field'    => 'category',
                'operator' => 'equals',
                'value'    => 'summer-collection',
            ),
        ),
    ),
);

$campaign_id = $campaign_manager->create_campaign( $campaign_data );
```

---

## Modifying Campaigns

### Update Campaign Settings

```
$campaign_id = 123;

$update_data = array(
    'discount_value' => 30,
    'end_date'       => '2025-08-15 23:59:59',
);

$campaign_manager->update_campaign( $campaign_id, $update_data );
```

### Activate/Pause Campaign

```
// Activate
$campaign_manager->activate_campaign( $campaign_id );

// Pause
$campaign_manager->pause_campaign( $campaign_id );

// Resume
$campaign_manager->resume_campaign( $campaign_id );
```

### Delete Campaign

```
$campaign_manager->delete_campaign( $campaign_id );
```

---

## Working with Analytics

### Get Campaign Analytics

```
$analytics = wsscd()->get_service( 'analytics_controller' );

$campaign_id = 123;
$data = $analytics->get_campaign_analytics( $campaign_id, array(
    'start_date' => '2025-06-01',
    'end_date'   => '2025-06-30',
) );

echo "Revenue: " . wc_price( $data['revenue'] );
echo "Conversions: " . $data['conversions'];
```

### Track Custom Event

```
$activity_tracker = wsscd()->get_service( 'activity_tracker' );

$activity_tracker->track( array(
    'event_type'  => 'custom_event',
    'campaign_id' => 123,
    'data'        => array( 'custom_field' => 'value' ),
) );
```

---

## Integration Examples

### Member-Only Discounts

```
add_filter( 'wsscd_is_product_eligible_for_discount', function( $eligible, $product, $context ) {
    if ( ! $eligible ) {
        return false;
    }

    $campaign_id = isset( $context['campaign_id'] ) ? (int) $context['campaign_id'] : 0;
    $members_only = $campaign_id ? get_post_meta( $campaign_id, '_members_only', true ) : false;

    if ( $members_only && ! is_user_logged_in() ) {
        return false;
    }

    return $eligible;
}, 10, 3 );
```

### Time-Based Dynamic Discounts

```
add_filter( 'wsscd_is_discount_rule_applicable', function( $applicable, $product_id, $rule, $context ) {
    if ( ! $applicable ) {
        return false;
    }

    // Example: block one rule during a specific hour window.
    $hour = (int) current_time( 'G' );
    if ( $hour >= 16 && $hour

Custom Stock-Based Exclusions

add_filter( 'wsscd_is_product_eligible_for_discount', function( $eligible, $product, $context ) {
    if ( ! $eligible ) {
        return false;
    }

    // Exclude low-stock products
    if ( $product instanceof WC_Product && $product->get_stock_quantity()

External System Integration

// Sync campaigns to external system when campaign status changes to active.
add_action( 'wsscd_campaign_status_changed', function( $campaign, $from, $to ) {
    if ( 'active' !== $to ) {
        return;
    }

    $api_endpoint = 'https://external-system.com/api/campaigns';

    wp_remote_post( $api_endpoint, array(
        'body' => json_encode( array(
            'external_id'    => $campaign->get_id(),
            'name'           => $campaign->get_name(),
            'discount_type'  => $campaign->get_discount_type(),
            'discount_value' => $campaign->get_discount_value(),
            'start_date'     => $campaign->get_start_date() ? $campaign->get_start_date()->format( 'c' ) : null,
            'end_date'       => $campaign->get_end_date() ? $campaign->get_end_date()->format( 'c' ) : null,
        ) ),
        'headers' => array(
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . EXTERNAL_API_KEY,
        ),
    ) );
}, 10, 3 );

Cache Considerations

Clear Cache After Changes

// Clear campaign cache
$cache_manager = wsscd()->get_service( 'cache_manager' );
$cache_manager->clear_campaign_cache( $campaign_id );

// Clear all plugin cache
$cache_manager->clear_all();

Bypass Cache for Custom Queries

$discount_engine = wsscd()->get_service( 'discount_engine' );

// Get price without using cache
$price = $discount_engine->get_discounted_price( $product_id, $original_price, array(
    'bypass_cache' => true,
) );

Error Handling

Try-Catch Pattern

try {
    $campaign_id = $campaign_manager->create_campaign( $data );
} catch ( WSSCD_Exception $e ) {
    error_log( 'SCD Error: ' . $e->getMessage() );
    // Handle error appropriately
}

Validation Before Operations

$validation = wsscd()->get_service( 'validation' );

$errors = $validation->validate_campaign_data( $campaign_data );

if ( ! empty( $errors ) ) {
    foreach ( $errors as $field => $message ) {
        echo "Error in {$field}: {$message}";
    }
} else {
    // Safe to create campaign
    $campaign_manager->create_campaign( $campaign_data );
}
```
