Destructive woocommerce_add_cart_item_data filter breaks Name Your Price
-
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 theuninstall.php
. You should be able to filterpre_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/TqFC7wsZThough my most pressing issue is converting
woocommerce_add_cart_item_data
into anadd_filter()
and <em>returning</em> the$cart_item_data
array.
- The topic ‘Destructive woocommerce_add_cart_item_data filter breaks Name Your Price’ is closed to new replies.