Advertisement
dale3h

[WooCommerce] Receiptful + Coupon Campaigns

Nov 23rd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Automatically assign various coupons to Coupon Campaigns
  5.  *
  6.  * @author dale3h
  7.  * @notes
  8.  *   This was a quick manual hack to get Receiptful coupons assigned to specific coupon campaigns.
  9.  *   It would be better to make a call directly to the WC_Coupon_Campaigns object to register the taxonomy,
  10.  *   but it only instantiates the object when loading as admin.
  11.  */
  12. add_action( 'woocommerce_process_shop_coupon_meta', 'wc_coupon_campaigns_auto_assign' );
  13. add_action( 'woocommerce_api_create_coupon', 'wc_coupon_campaigns_auto_assign' );
  14.  
  15. function wc_coupon_campaigns_auto_assign( $post_id ) {
  16.     // Define WC Coupon Campaigns taxonomy info
  17.     $wccc_post_type = 'shop_coupon';
  18.     $wccc_tax       = 'coupon_campaign';
  19.  
  20.     // Define our automatic campaigns
  21.     $campaigns = array(
  22.         'Refer a Friend'        => array( 'referral-coupons' ),
  23.         'Refer a Friend Reward' => array( 'referral-rewards' ),
  24.         "<font>To get you underway, here's a coupon for 10% off your next purchase in the next two weeks.</font>" => array( 'reorder-reminders' ),
  25.     );
  26.  
  27.     // Get the post object
  28.     $post = get_post( $post_id );
  29.  
  30.     // Bail out if no post
  31.     if ( ! $post ) {
  32.         return false;
  33.     }
  34.  
  35.     // Register taxonomy
  36.     if ( ! taxonomy_exists( $wccc_tax ) ) {
  37.         $labels = array(
  38.             'name'              => __( 'Coupon Campaigns' , 'wc_coupon_campaigns' ),
  39.             'singular_name'     => __( 'Campaign', 'wc_coupon_campaigns' ),
  40.             'search_items'      => __( 'Search Campaigns' , 'wc_coupon_campaigns' ),
  41.             'all_items'         => __( 'All Campaigns' , 'wc_coupon_campaigns' ),
  42.             'parent_item'       => __( 'Parent Campaign' , 'wc_coupon_campaigns' ),
  43.             'parent_item_colon' => __( 'Parent Campaign:' , 'wc_coupon_campaigns' ),
  44.             'edit_item'         => __( 'Edit Campaign' , 'wc_coupon_campaigns' ),
  45.             'update_item'       => __( 'Update Campaign' , 'wc_coupon_campaigns' ),
  46.             'add_new_item'      => __( 'Add New Campaign' , 'wc_coupon_campaigns' ),
  47.             'new_item_name'     => __( 'New Term Campaign' , 'wc_coupon_campaigns' ),
  48.             'menu_name'         => __( 'Coupon Campaigns' , 'wc_coupon_campaigns' ),
  49.         );
  50.  
  51.         $args = array(
  52.             'public'            => true,
  53.             'show_ui'           => true,
  54.             'show_in_nav_menus' => true,
  55.             'hierarchical'      => true,
  56.             'rewrite'           => true,
  57.             'labels'            => $labels,
  58.         );
  59.  
  60.         register_taxonomy( $wccc_tax, $wccc_post_type, $args );
  61.     }
  62.  
  63.     // Get the campaigns that we need to assign the coupon to
  64.     $terms = @( $campaigns[ $post->post_excerpt ] ) ?: false;
  65.  
  66.     // Bail out if no terms
  67.     if ( ! $terms ) {
  68.         return false;
  69.     }
  70.  
  71.     // Add the coupon to the campaigns
  72.     wp_add_object_terms( $post->ID, $terms, $wccc_tax );
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement