• Resolved therealgilles

    (@chamois_blanc)


    Hello,

    I am using the auto-generated coupon feature of WC Smart Coupons. The restrictions (user roles…) applied by this plugin are not inherited by the auto-generated coupons. Which is a problem.

    Any advice?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter therealgilles

    (@chamois_blanc)

    Are you not supposed to use the wc_sc_new_coupon_generated action to handle duplicate coupon inheritance?

    Thread Starter therealgilles

    (@chamois_blanc)

    So I am guessing the plugin maybe does not the WC Smart Coupons plugin.

    If you wanted to add support, this is what the missing code would look like:

    add_action('wc_sc_new_coupon_generated', 'add_wjecf_meta_on_new_coupon_generated');
    function add_wjecf_meta_on_new_coupon_generated( $coupon_info ) {
      $new_coupon_id = $coupon_info['new_coupon_id'];
      $ref_coupon    = $coupon_info['ref_coupon'];
      $ref_coupon_id = $ref_coupon->get_id();
    
      $fields = get_all_coupon_meta_fields( $ref_coupon );
      foreach ( $fields as $field => $field_value ) {
        $value = get_post_meta( $ref_coupon_id, $field, true ); // always single
        if ( $value != null && $value != '' ) {
          update_post_meta( $new_coupon_id, $field, $value );
        }
      }
    }
    Plugin Author Soft79

    (@josk79)

    Hi,

    Thanks for this. I would like to test this locally, but don’t have the Smart Coupons plugin. Can you please send a copy to admin at soft79.nl if the license permits?

    Plugin Author Soft79

    (@josk79)

    Hi, do you still need help with this?

    Thread Starter therealgilles

    (@chamois_blanc)

    I don’t think I am allowed to send copy of a plugin. You will need to ask the Woocommerce team for access.

    I had to create my own get_all_coupon_meta_fields function as I could not find a way to call yours (I believe the function is not public). Also after upgrading to the pro plugin, I found that I needed to exclude pro-free-products from the plugin list. Here is my code:

    function my_wjecf_get_all_coupon_meta_fields($coupon) {
      // Collect the meta_fields of all the WJECF plugins
      $fields = array(
         '_wjecf_min_matching_product_qty'      => 'int',
         '_wjecf_max_matching_product_qty'      => 'int',
         '_wjecf_min_matching_product_subtotal' => 'decimal',
         '_wjecf_max_matching_product_subtotal' => 'decimal',
         '_wjecf_products_and'                  => 'yesno',
         '_wjecf_categories_and'                => 'yesno',
         '_wjecf_shipping_methods'              => 'clean',
         '_wjecf_payment_methods'               => 'clean',
         '_wjecf_customer_ids'                  => 'int,',
         '_wjecf_customer_roles'                => 'clean',
         '_wjecf_excluded_customer_roles'       => 'clean',
      );
      foreach (WJECF()->get_plugins() as $name => $plugin) {
        if ($plugin->plugin_is_enabled() && ($name != 'pro-free-products')) {
          // error_log('plugin name = '.$name);
          $fields = array_merge($fields, $plugin->admin_coupon_meta_fields($coupon));
        }
      }
      return $fields;
    }

    Let me know if there is a better way to do this.

    Plugin Author Soft79

    (@josk79)

    Please check the license, If it’s GPL you can copy it.

    Otherwise I’ll look into this somewhere next year.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Auto-generated coupons do not inherit this plugin restrictions’ is closed to new replies.