• Resolved HelgaTheViking

    (@helgatheviking)


    Hi there,

    I am the author of WooCommerce Name Your Price and I had a customer reach out after experiencing difficulty while using both our plugins. My plugin allows customers to enter a custom price for a product. With your plugin active this price is not passed to the cart.

    This is a follow up on:
    https://www.ads-software.com/support/topic/not-working-with-woocommerce-extension-name-your-price/

    Based on my inspection the issue is here in your code:

    
     // Empty cart when product is added to cart, so we can't have multiple products in cart
        add_action( 'woocommerce_add_cart_item_data', function() {
            wc_empty_cart();
        } );
    

    woocommerce_add_cart_item_data is a critical filter for many plugins that add custom or advanced products to the cart. Here you do not send the existing cart item value value back to WooCommerce. This is clearing out the custom values set by Name Your Price and if I had to guess these other issues are related:

    https://www.ads-software.com/support/topic/would-woocommerce-checkout-add-ons-work-with-this-plugin/
    https://www.ads-software.com/support/topic/errors-not-compatible-with-product-bundles-and-subscriptions/

    If you update lines 120-124 above to the following:

    
    // Empty cart when product is added to cart, so we can't have multiple products in cart
        add_filter( 'woocommerce_add_cart_item_data', function( $cart_item_data ) {
            wc_empty_cart();
            return $cart_item_data;
        } );

    `

    I can confirm this patches compatibility with Name Your Price (and probably with other plugins as well).

    A couple other things I noted:

    1. You are using anonymous callbacks for most all of your hooks/filters. This makes it difficult for developers to extend or interact with your plugin.
    2. update_option( 'woocommerce_cart_redirect_after_add', 'yes' ); means that this option is permanent updated even after plugin deletion since I don’t see this option deleted in the uninstall.php . You should be able to filter pre_option_woocommerce_cart_redirect_after_add instead as a non-permanent option.
    3. Your activation/deactivation hooks don’t do anything.. are they necessary?

    I did not find this plugin on github or I would have sent a PR. But here are some proposed changes:
    https://pastebin.com/TqFC7wsZ

    Though my most pressing issue is converting woocommerce_add_cart_item_data into an add_filter() and <em>returning</em> the $cart_item_data array.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author code4life

    (@code4life)

    Hi HelgaTheViking, how are you? I hope so!

    I proceeded to release version 1.2.5 which shows some of the improvements you proposed, which guarantee the operation with your plugin and avoid the permanent storage of the option.
    Thanks for the tips and for contributing to this free plugin!

    Best regards and stay safe

    Thread Starter HelgaTheViking

    (@helgatheviking)

    Hi, thanks so much for releasing the update! Our mutual user should be pleased. ??

    Take care!

    Plugin Author code4life

    (@code4life)

    You’re welcome HelgaTheViking, thank you to you for your collaboration!
    Best regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Destructive woocommerce_add_cart_item_data filter breaks Name Your Price’ is closed to new replies.