0

I want to create custom form on my 'Theme Options'

Basically, I already build this :

/* == THEME OPTIONS == */

// This tells WordPress to call the function named "setup_theme_admin_menus"
// when it's time to create the menu pages.
add_action("admin_menu", "setup_theme_admin_menus");

function setup_theme_admin_menus() {
    add_submenu_page('themes.php', 
        'Generate Coupons', 'Generate Coupons', 'manage_options', 
        'generate-coupons-elements', 'theme_generate_coupons_settings'); 
}

function theme_generate_coupons_settings() {
?>
    <div class="wrap">
        <?php screen_icon('themes'); ?> <h2>Generate Coupons</h2>

        <form method="POST" action="">
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">
                        <label for="coupon">
                            Number of coupons:
                        </label> 
                    </th>
                    <td>
                        <input type="text" name="coupon" size="25" />
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">
                        <label for="discount">
                            Amount of discount:
                        </label> 
                    </th>
                    <td>
                        <input type="text" name="discount" size="25" />
                    </td>
                </tr>
            </table>
            <p>
                <input type="submit" value="Generate" class="button-primary"/>
            </p>
        </form>
    </div>
<?php
}

/* == END OF THEME OPTIONS == */

That code to show fields form on my dashboard.

And I saw this on WooCommerce documentation :

$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product

$coupon = array(
    'post_title' => $coupon_code,
    'post_content' => '',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
);

$new_coupon_id = wp_insert_post( $coupon );

// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

My question is, how to make my first code run the second code and catch the variables that sent from that code? I understand the second code only make 1 coupon, but later I can loop it based on the 'coupons' number that sent by the form.

I hope you can help me on this one.

Thank you.

1 Answer 1

0

After did many trials and errors, finally I found the answer.

Just in case anyone wondering how this working, I put the full code right here :

/* == THEME OPTIONS == */

add_action("admin_menu", "setup_theme_admin_menus");

function setup_theme_admin_menus() {
    add_submenu_page('themes.php', 
        'Generate Coupons', 'Generate Coupons', 'manage_options', 
        'generate-coupons-elements', 'theme_generate_coupons_settings'); 
}

function theme_generate_coupons_settings() {
?>
  <div class="wrap">
    <?php screen_icon('themes'); ?> <h2>Generate Coupons</h2>

    <form method="POST" action="">
      <table class="form-table">
        <tr valign="top">
          <th scope="row">
            <label for="coupon">
              Number of coupons:
            </label> 
          </th>
          <td>
            <input type="text" name="coupon" size="25" />
          </td>
        </tr>
        <tr valign="top">
          <th scope="row">
            <label for="discount">
              Amount of discount:
            </label> 
          </th>
          <td>
            <input type="text" name="discount" size="25" />
          </td>
        </tr>
      </table>
      <p>
        <input type="hidden" name="update_settings" value="Y" />
        <input type="submit" value="Generate" class="button-primary"/>
      </p>
    </form>
  </div>
<?php
  if (isset($_POST["update_settings"])) {
    $coupon = esc_attr($_POST["coupon"]); 
    $discount = esc_attr($_POST["discount"]); 
    $coupon_code = 'UNIQUECODES'; // Code
    $amount = $discount; // Amount
    $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product

    $coupon = array(
      'post_title' => $coupon_code,
      'post_content' => '',
      'post_status' => 'publish',
      'post_author' => 1,
      'post_type'   => 'shop_coupon'
    );

    $new_coupon_id = wp_insert_post( $coupon );

    // Add meta
    update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
    update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
    update_post_meta( $new_coupon_id, 'individual_use', 'no' );
    update_post_meta( $new_coupon_id, 'product_ids', '' );
    update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
    update_post_meta( $new_coupon_id, 'usage_limit', '1' );
    update_post_meta( $new_coupon_id, 'usage_limit_per_user', '1' );
    update_post_meta( $new_coupon_id, 'expiry_date', '' );
    update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
    update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
    ?>
      <div id="message" class="updated">Coupon generated!</div>
    <?php
  }
}

/* == END OF THEME OPTIONS == */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.