Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Automatically assign various coupons to Coupon Campaigns
- *
- * @author dale3h
- * @notes
- * This was a quick manual hack to get Receiptful coupons assigned to specific coupon campaigns.
- * It would be better to make a call directly to the WC_Coupon_Campaigns object to register the taxonomy,
- * but it only instantiates the object when loading as admin.
- */
- add_action( 'woocommerce_process_shop_coupon_meta', 'wc_coupon_campaigns_auto_assign' );
- add_action( 'woocommerce_api_create_coupon', 'wc_coupon_campaigns_auto_assign' );
- function wc_coupon_campaigns_auto_assign( $post_id ) {
- // Define WC Coupon Campaigns taxonomy info
- $wccc_post_type = 'shop_coupon';
- $wccc_tax = 'coupon_campaign';
- // Define our automatic campaigns
- $campaigns = array(
- 'Refer a Friend' => array( 'referral-coupons' ),
- 'Refer a Friend Reward' => array( 'referral-rewards' ),
- "<font>To get you underway, here's a coupon for 10% off your next purchase in the next two weeks.</font>" => array( 'reorder-reminders' ),
- );
- // Get the post object
- $post = get_post( $post_id );
- // Bail out if no post
- if ( ! $post ) {
- return false;
- }
- // Register taxonomy
- if ( ! taxonomy_exists( $wccc_tax ) ) {
- $labels = array(
- 'name' => __( 'Coupon Campaigns' , 'wc_coupon_campaigns' ),
- 'singular_name' => __( 'Campaign', 'wc_coupon_campaigns' ),
- 'search_items' => __( 'Search Campaigns' , 'wc_coupon_campaigns' ),
- 'all_items' => __( 'All Campaigns' , 'wc_coupon_campaigns' ),
- 'parent_item' => __( 'Parent Campaign' , 'wc_coupon_campaigns' ),
- 'parent_item_colon' => __( 'Parent Campaign:' , 'wc_coupon_campaigns' ),
- 'edit_item' => __( 'Edit Campaign' , 'wc_coupon_campaigns' ),
- 'update_item' => __( 'Update Campaign' , 'wc_coupon_campaigns' ),
- 'add_new_item' => __( 'Add New Campaign' , 'wc_coupon_campaigns' ),
- 'new_item_name' => __( 'New Term Campaign' , 'wc_coupon_campaigns' ),
- 'menu_name' => __( 'Coupon Campaigns' , 'wc_coupon_campaigns' ),
- );
- $args = array(
- 'public' => true,
- 'show_ui' => true,
- 'show_in_nav_menus' => true,
- 'hierarchical' => true,
- 'rewrite' => true,
- 'labels' => $labels,
- );
- register_taxonomy( $wccc_tax, $wccc_post_type, $args );
- }
- // Get the campaigns that we need to assign the coupon to
- $terms = @( $campaigns[ $post->post_excerpt ] ) ?: false;
- // Bail out if no terms
- if ( ! $terms ) {
- return false;
- }
- // Add the coupon to the campaigns
- wp_add_object_terms( $post->ID, $terms, $wccc_tax );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement