• Hi, I’m the author of WooCommerce Free Gift Coupons and a customer came to me asking about compatibility with your plugin. My plugin saves some custom meta, but I believe there are 2 barriers to generating a Free Gift style coupon.

    1. My admin coupon data is an array of arrays. My $_POST data looks like:

    array (
    	10 => array (
    		'quantity' => '1'
    	)
    );
    

    This throws a notice in step 2:

    
    [19-Apr-2021 17:10:52 UTC] PHP Notice:  Array to string conversion in public\wp-includes\formatting.php on line 1098
    [19-Apr-2021 17:10:52 UTC] PHP Stack trace:
    [19-Apr-2021 17:10:52 UTC] PHP   1. {main}() public\wp-admin\admin.php:0
    [19-Apr-2021 17:10:52 UTC] PHP   2. do_action() public\wp-admin\admin.php:259
    [19-Apr-2021 17:10:52 UTC] PHP   3. WP_Hook->do_action() public\wp-includes\plugin.php:484
    [19-Apr-2021 17:10:52 UTC] PHP   4. WP_Hook->apply_filters() public\wp-includes\class-wp-hook.php:316
    [19-Apr-2021 17:10:52 UTC] PHP   5. WCCG_Admin->coupon_generator_callback() public\wp-includes\class-wp-hook.php:292
    [19-Apr-2021 17:10:52 UTC] PHP   6. WCCG_Generator->output_step() public\wp-content\plugins\coupon-generator-for-woocommerce\includes\admin\class-wccg-admin.php:114
    [19-Apr-2021 17:10:52 UTC] PHP   7. WCCG_Generator->generator_options_handler() public\wp-content\plugins\coupon-generator-for-woocommerce\includes\admin\class-wccg-generator.php:105
    [19-Apr-2021 17:10:52 UTC] PHP   8. require_once() public\wp-content\plugins\coupon-generator-for-woocommerce\includes\admin\class-wccg-generator.php:148
    [19-Apr-2021 17:10:52 UTC] PHP   9. esc_attr() public\wp-content\plugins\coupon-generator-for-woocommerce\includes\admin\views\html-coupon-generator-step-2.php:68
    [19-Apr-2021 17:10:52 UTC] PHP  10. wp_check_invalid_utf8() public\wp-includes\formatting.php:4504

    which I believe is coming from attempting to escape the arrays:

    
    if ( is_array( $val ) ) :
    					foreach ( $val as $inner_val ) :
    						?><input type="hidden" name="<?php echo esc_attr( $key ); ?>[]" value="<?php echo esc_attr( $inner_val ); ?>" /><?php
    					endforeach;
    				else :

    Barrier 2

    Though the meta undergoes some more processing when saved. It appears you’re using $wpdb to insert new coupons instead of using WooCommerce’s CRUD. Even when I dabble in using the Woo CRUD I’m running into some issues with the data not being in $_POST like my plugin currently expects. That may require a tweak on my end… but that’s to be determined if we’re interested in pursuing any compatibility.

    cheers!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeroen Sormani

    (@sormano)

    Hi Helga,

    1) This should probably get a re-factor at some point to use json encoding of the data instead of adding it as separate fields for all posted data.

    2) This is purposely done for performance (added to it I don’t think CRUD was there when I first created the plugin, but performance reasons still apply, also the reason I’m not using ‘add_meta’ type functions). Using CRUD to generate thousands of coupons would cause it to slow down to a crawl.

    Downside being of course losing out on some potential out-of-the-box compatibility, but hooks are available add additional data.

    For the first a easy solution before any re-factor would be to add a additional loop to check for multi-dimensional arrays. If this is something needed by the customer on the short term before the refactor I’d recommend (for their developer, I wouldn’t expect you to do this) to make this change directly. A customization for compatibility to insert the custom data would have to be made anyways.

    Cheers,
    Jeroen

    Thread Starter HelgaTheViking

    (@helgatheviking)

    Hi Jeroen,

    Thanks for replying! I had not considered encoding all the fields into 1.. that makes sense. I’d been toying with just json encoding every field individually to get around that PHP notice.

    Interesting notes about performance… but yes, the pure SQL insert makes auto-compatibility much more difficult, though I never quite figured out how to make the CRUD part work. Is there an example for adding custom meta? I thought I saw the filter for it, but couldn’t quite grasp where the data was coming from.. and of course… in my case, the data was not actually coming through.

    I’ve kind of told the customer this goes beyond regular coverage of support, but it’s caught me interest. They asked me how to bulk generate coupons and I could not tell them… so it got me curious. I am very cautious about what plugins I declare official compatibility with, but really can’t think of any other alternatives for this feature.

    Cheers,
    -kathy

    Thread Starter HelgaTheViking

    (@helgatheviking)

    Took a stab at converting the inputs into a single JSON textarea, but i think i am having issues escaping JSON multiple times.

    One thing that was unrelated to my initial comment, but might be related to this, and in either case would probably be a major improvement… not reloading the admin to go from step 1->2->3->process.

    Is this on github anywhere?

    Thread Starter HelgaTheViking

    (@helgatheviking)

    Well I was procrastinating pretty hard and I wrote some tweaks to not use steps/reload the pages.. which does make it easier to handle the data since you don’t have to stash it or json encode it or anything.

    It’s also ready for integrating with plugins like mine that add custom meta. Check it out here:

    https://github.com/helgatheviking/coupon-generator-for-woocommerce/

    Plugin Author Jeroen Sormani

    (@sormano)

    Hi Helga,

    The ‘woocommerce_coupon_generator_coupon_meta_data’ filter can be used to insert custom meta data with the coupons. The posted data should also be available there.

    It is available on GH here; https://github.com/JeroenSormani/woocommerce-coupon-generator
    What changes did you make to make it work with custom meta? (the filter above I believe should still be the way to insert it in the DB)

    Let me know!
    Jeroen

    Thread Starter HelgaTheViking

    (@helgatheviking)

    I think that filter is not sufficient by itself, but I also didn’t check the $_POST data. With the notice, my data wasn’t getting relayed correctly anyway.

    Instead, I passed the $args as a 2nd parameter in my fork here: https://github.com/helgatheviking/woocommerce-coupon-generator/blob/single-step/includes/admin/wccg-core-functions.php#L90

    Though the biggest change is removing the separate “step” page loads. This eliminates the need to reload the admin (Which is always slow as molasses for me) and keeps the coupon form data on the initial page where it can be handled without needing to be parsed into hidden attributes on another page.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Compatibility with WooCommerce Free Gift Coupons’ is closed to new replies.