Coupon applies to multiple line items with MarketPress plugin

Using MarketPress, if I create a coupon code with a specific dollar amount (say $40), that dollar amount applies to every line item in the shopping cart.

For instance, if I have two different t-shirts that cost $45 each, and apply a $40 coupon, the customer will be billed a total of $10 instead of $50.

It appears the coupon is applying to each line item, not to the subtotal.

Is there a fix for this?

Thanks!

  • Jack Kitterhing
    • Code Norris

    Hi there @Todd,

    Hope you're well today and thanks for your question.

    We did make some fixes to the coupon functionality, in a MarketPress update a short while ago, could you upgrade please and see if it works correctly then for you please? :slight_smile:

    We're here to help! :slight_smile:

    Thanks!

    Kind Regards
    Jack.

  • Todd
    • WPMU DEV Initiate

    Any help on this? As a paying Wpmudev customer, is there another avenue I should be pursuing for support?

    I have a website that is ready to go into production today, but this one bug has held us up. My customer's happiness is steadily declining.

    I would be willing to fix this problem myself temporarily, but need direction on what needs to be adjusted. From what I can tell, the update_cart function runs when a coupon is applied, but it also appears the coupon_value_product function is being called on each line item to adjust the price.

    Thanks!

  • Jack Kitterhing
    • Code Norris

    Hi there @Todd,

    Hope you're well today and thanks for your patience, sorry about the delay here.

    Is this a different site? I'm showing you still have 2.9.4.4 on store.domain.com

    Though I have done some tests on my own site and there seems to a flaw in the logic, on my site running the latest the exact same happens.

    I've notified one of our developers @Hoang Ngo to take a look and get this fixed up.

    Thank you!

    Kind Regards
    Jack.

  • Todd
    • WPMU DEV Initiate

    Thank you for the update!

    I've verified I am indeed running Version 2.9.4.8

    When can I expect a patch? I've begun hacking and slashing the MarketPress plugin myself; however I'd rather put an official patch in production instead.

    Thanks!

  • Hoang Ngo
    • Code Slayer

    Hi @Todd,

    I hope you are well today and I'm sorry about this issue.

    Here is a quick fix, it will have cart apply, but the way it work will a little different. It will divide the amount and subtract the result to every items from the cart.

    This fix for now only apply for paypal express checkout, please tell me your active gateway so I can apply the fix to.

    Please backup the old plugin, and overwrite the files with this attachment.

    If you have any issues please don't hesitate to let us know so we can assist.
    Best regards,
    Hoang

  • Todd
    • WPMU DEV Initiate

    Guys,

    This did NOT solve the problem! See attached. I completely removed the old plugin, downloaded the new plugin from the website and overwrote the files from the above attachments.

    The attachments above did not include a new template-functions.php, which I know is used when creating the Review Cart and Confirm checkout pages.

    Perhaps you didn't send the updated copies of those files?

    Thanks!

  • Hoang Ngo
    • Code Slayer

    Hi @Todd,

    I hope you are well today and I'm sorry for the issue.

    Hmm, I will need your ftp to take a look, can you please send in:

    - Mark to my attention - ATTN: Hoang Ngo
    - Link back to this thread
    - Include admin/network access
    - Include FTP
    - Include any relevant URLS for your site

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    Best regards,
    Hoang

  • Hoang Ngo
    • Code Slayer

    Hi @Todd,

    Thanks for your information and I'm really sorry about this inconvenience.

    Could you enable support staff login in WPMU DEV dashboard > support > support access > grant access, please. And please make sure the coupon is "Cart" type, not product type.

    Thanks!!!

    Best regards,
    Hoang

  • Hoang Ngo
    • Code Slayer

    Hi @Todd,

    I hope you are well today and I'm sorry about the issue.

    I have updated the tax calculation function to make it compatibility with cart coupon type. It should right now, please give it a try and let us update.

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang

  • Todd
    • WPMU DEV Initiate

    Hoang,

    Thanks for your work on this.

    Unfortunately, the "Confirm" page does not show the correct totals for each line item. It's using the per-item coupon code instead of the per-cart code.

    Also, can you please confirm the actual amount sent to the payment processor is correct. That will be more difficult for me to check.

    Thanks,
    Todd

  • Hoang Ngo
    • Code Slayer

    Hi @Todd,

    I hope you are well today and I'm sorry about the issue.

    I have some update on your sites, now all the pages look correct now.

    Can I quickly change your payment setting to sandbox mode and do a final test to make sure this working properly ?, If yes, please tell me when the suitable time and your timezone :slight_smile:.

    I also attach the newest fix for the marketpress cart coupon.

    Thanks !!!,

    Best regards,
    Hoang

  • Todd
    • WPMU DEV Initiate

    Hoang,

    The customer wants to begin using the site today. I'm only able to leave it in test mode for another hour (until 11 AM EDT).

    The site will be live (and credit cards will be charged) after 11 AM EDT today.

    Thanks,
    Todd

  • Todd
    • WPMU DEV Initiate

    Hello,

    I have found another bug related to this fix for the coupon codes.

    The coupon code is still applied incorrectly if a customer selects two of the same items, but different options. See attached, 40HWT is a $40 off coupon and it is being applied twice.

    Please let me know what can be done about this.

    Thanks!

  • Jack Kitterhing
    • Code Norris

    Hi there @Todd,

    Hope you're well today and sorry to see this still isn't working correctly, Hoang isn't currently around at the moment.

    But I'm going to see if another developer @Saurabh Shukla could take a look, and of course as soon as Hoang is online he'll take a look. :slight_smile:

    Thanks!

    Kind Regards
    Jack.

  • Hoang Ngo
    • Code Slayer

    Hi @Todd,

    I hope you are well today, and I'm really sorry about the issue :slight_frown:.

    In the file /marketpress/marketpress.php, please find the function "coupon_value_product", remove that, and replace with this alternative.

    function coupon_value_product( $code, $price, $product_id ) {
    		//check does the code for cart
    		$cp_data = $this->get_coupon_data( $code );
    		$items_count = mp_items_count_in_cart();
    		if ( $cp_data['applies_to']['type'] == 'cart' && $items_count > 0 ) {
    			$discount      = $cp_data['discount'];
    			$discount_type = $cp_data['discount_type'];
    			$items_count = 0;
    			global $mp, $blog_id;
    			$blog_id = ( is_multisite() ) ? $blog_id : 1;
    			$total = 0;
    
    			$global_cart = $mp->get_cart_contents( true );
    			if ( ! $mp->global_cart )
    				$selected_cart[$blog_id] = $global_cart[$blog_id];
    			else
    				$selected_cart = $global_cart;
    
    			if ( is_array( $selected_cart ) && count( $selected_cart ) ) {
    				$count = 0;
    				foreach ( $selected_cart as $cart ) {
    					if ( is_array( $cart ) && count( $cart ) ) {
    						foreach ( $cart as $variations ) {
    							if ( is_array( $variations ) && count( $variations ) ) {
    								foreach ( $variations as $item ) {
    									$items_count+= 1;
    									$total+=($item['price']*$item['quantity']);
    								}
    							}
    						}
    					}
    				}
    			}
    
    			//check the item count
    			$discount_per_item = sprintf('%01.2f', ( $discount / $items_count ) );
    			//check does this is fix or percent
    			if ( $discount_type == 'amt' ) {
    				$price = $price - $discount_per_item;
    			} else {
    				$price = $price - ( ( $price * $discount_per_item ) / 100 );
    			}
    			if ( $price < 0 ) {
    				$price = 0;
    			}
    
    			return $price;
    		}
    
    		if ( $this->coupon_applicable( $code, $product_id ) ) {
    			$discount = $this->coupon_value( $code, $price );
    
    			return ( $discount === false ) ? $price : $discount['new_total'];
    		}
    
    		return $price;
    	}

    Please make sure you have a backup before applying the new code.

    If you have any issues please don't hesitate to let us know so we can assist
    Best regards,
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.