In the module uc_discounts, I am unable to add a discount.

When I try to add a discount at admin/store/uc_discounts/add my dev site just hangs. I have Backdrop v1.22.2 and the latest release of this module (1.x-2.0.1).

Update: I was using Chrome, but when using Firefox I get a little more information and this is logged in the site reports:

LOCATION    xxx/admin/store/uc_discounts/add
REFERRER    xxx/admin/store/uc_discounts
MESSAGE    Notice: Array to string conversion in date_limit_format() (line 1171 of xxxxx/web/core/includes/date.inc).
SEVERITY    notice

and

LOCATION    xxx/admin/store/uc_discounts/add
REFERRER    xxx/admin/store/uc_discounts/list
MESSAGE    Warning: strtr() expects parameter 1 to be string, array given in strtr() (line 1186 of xxxxx/web/core/includes/date.inc).

Hopefully that gives a clue as to where I might look?

Accepted answer

@nattyweb, I think you can string together some things to make this work. Within the module, you could use the "Qualification by Role" discount, creating a role (e.g., "OECD DC") that receives the discount. Then you would need to give that role to qualifying users.

For that, you might be able to accomplish it with Rules (you'll need to enable the Rules UI module), creating rules that add or remove the role from a user if their country is in your specified list.

If that doesn't work, it could be done with a fairly simple module that uses update hooks to do the check and add/remove of role when a user account is created or modified.

 

 

Most helpful answers

Good to know. I didn't suggest uc_price_per_role initially because one needs to set the discounted price on each product, which could be tedious for lots of products. Glad it works for you! I don't currently use uc_discounts (so don't run across the bugs in regular usage), but do use uc_price_per_role regularly, so it gets more real-world testing. (Nevertheless, if you find issues with it, do post them in the queue, and I'll investigate ASAP.)

Thanks for digging into this @Graham Leach! Yes, there are a number of differences between Backdrop and Drupal 7 (including the '/core' subdirectory for most core files) since Backdrop was forked in between Drupal 7 and Drupal 8. But for the most part it's extremely compatible in terms of the API, etc. The Change Records page is useful to skim in this regard.

As far as this issue, it sounds like @bugfolder has tracked it down:

This signals to me that you are trying to do "something D7" in a D8 or D9 environment as the architecture of D7 does not have a /core subdirectory as far as I can remember.

Actually, the problem was that a property in the date_popup element that changed in Backdrop hadn't been updated in the module. It's been fixed in the latest release.

Comments

bugfolder's picture

There's a new bugfix release (which, among other things, fixed the bug of the wrong release number on the previous bugfix release). Does version 1.x-2.0.2 still exhibit this problem?

I'm afraid so, yes.

I uninstalled the module, cleared cache, replaced the module files, reinstalled and the exact same problem exists.

The error suggests something related to date but I'm afraid I'm not clever enough to find out anything further.

I assume this is repeatable and I'm not the only person seeing this?

My need for a discount is very simple - to apply a percentage discount on an item if the customer's country (defined in their user account addressfield) is one of a selected list (OECD developing countries).

Looking at the functionality of the Ubercart Discount module, I was not at all clear that I could use it for this purpose. So I reviewed the D7 modules and found Ubercart Simple Discount which enables discounts to be applied by Rules.

I followed instructions to convert a D7 module to BackdropCMS and - with changes required only in the .info file - this module miraculously works exactly as I wanted.

No problems so far, so perhaps this is one of those examples where a module can easily be ported to BackdropCMS.

If anyone knows of any problems I might encounter following this, do please let me know.

In the meantime, I uninstalled the module uc_discounts as I couldn't get it to work at all in my dev site. I hope that module is investigated and fixed by someone smarter than me (I'm not an experienced developer, just a site builder).

Hello There, Doesn't look like anyone dug into the nature of this error, so let me help out. I have been working with PHP since 2000, and with Drupal since 2010, though I would not describe myself as any kind of expert. The error you received is a PARAMETER TYPE MISMATCH error:  

Warning: strtr() expects parameter 1 to be string, array given in strtr() (line 1186 of xxxxx/web/core/includes/date.inc).

 

1)  The strtr() function expects that the first parameter supplied be a STRING, which is a simple data type often likened to a 1-dimensional array of CHARacters.  You can liken this to a ROW in a spreadsheet. 2)  The parameter supplied was actually an ARRAY, which is a complex data type that you can liken to the entire spreadsheet To solve your issue, you need to reduce the complexity of the passed parameter by exploring the OBJECT being passed (which is an ARRAY)  using either injected code or debugging tools to figure out what ROW in the ARRAY is the string of CHAR that will meet the expectations of the parameter declaration for that function. Parameter declarations exist to prevent programs from acting crazy.  They are actually an effort to help make programs safer.  Unfortunately, when you change the underlying architecture of a technology (Drupal 6 -> Drupal 7 -> Drupal 8 -> Drupal 9 anyone?) the code, correct though it was 10 years ago even, may not work today.  Such is the hubris of those who steer our tools. As for the exact nature of the error, here's an analogy: Let's propose a data structure like a mini spreadsheet that looks like this: aaa bbb ccc The strtr() function is expecting to receive one of the above 3-letter rows, not the entire 9-letter block, which is what it's getting. Now, I notice that the error being thrown in question is actually NOT in uc_discount.  It is in DRUPAL CORE and it is a consequence of code somewhere (maybe in uc_discount?) submitting an improperly formatted function call to date.inc. This makes me feel like your particular error may have something to do with your using a different PHP version than was originally used to develop Drupal Core or Ubercart Discounts.  There were a MAJOR changes between PHP 5, PHP 6 and PHP 7, for example, over which the Ubercart product was originally developed and upgraded.  This kind of "rug pulling" is not exclusive to Drupal leadership, though they are an egregious example of it. So...let's have a look see at the code in question, shall we?

line 1186 of xxxxx/web/core/includes/date.inc

First of all, it looks to me like the error message is totally nuts.  A) date.inc 196 lines long B) uc_simple_discount.module is only 533 lines long

This leads me to believe that the error actually emanates from a "function call stack" where function -> function -> function and the error message that "bubbled up" got garbled. OK, let's look for instances of strtr() in the code of those two modules: A) There are NO instances of strtr() in date.inc B) There are NO instances of strtr() in uc_simple_discout Your error message contains a reference to /core/includes/xxxxxx This signals to me that you are trying to do "something D7" in a D8 or D9 environment as the architecture of D7 does not have a /core subdirectory as far as I can remember. g. ----  

bugfolder's picture

This signals to me that you are trying to do "something D7" in a D8 or D9 environment as the architecture of D7 does not have a /core subdirectory as far as I can remember.

Actually, the problem was that a property in the date_popup element that changed in Backdrop hadn't been updated in the module. It's been fixed in the latest release.

Thanks for digging into this @Graham Leach! Yes, there are a number of differences between Backdrop and Drupal 7 (including the '/core' subdirectory for most core files) since Backdrop was forked in between Drupal 7 and Drupal 8. But for the most part it's extremely compatible in terms of the API, etc. The Change Records page is useful to skim in this regard.

As far as this issue, it sounds like @bugfolder has tracked it down:

Hello @laryn, Thanks! g. ----

 

bugfolder's picture

Glad to hear you found a solution. Just for completeness, the issue you found in uc_discounts module is now fixed in the latest release. Thanks for flagging it!

Thanks for advising. Now revisiting - see question below!

@bugfolder I'm now revisiting this. Can you suggest if I can achieve the discount I mentioned originally, that is:

My need for a discount is very simple - to apply a percentage discount on an item if the customer's country (defined in their user account addressfield) is one of a selected list (OECD developing countries).

Thanks!

bugfolder's picture

@nattyweb, I think you can string together some things to make this work. Within the module, you could use the "Qualification by Role" discount, creating a role (e.g., "OECD DC") that receives the discount. Then you would need to give that role to qualifying users.

For that, you might be able to accomplish it with Rules (you'll need to enable the Rules UI module), creating rules that add or remove the role from a user if their country is in your specified list.

If that doesn't work, it could be done with a fairly simple module that uses update hooks to do the check and add/remove of role when a user account is created or modified.

 

 

@bugfolder - thank you for the suggestion; I'll try out use of Rules to assign a role and then use that as a discount condition. Great idea!

@bugfolder your suggestion is working for me - a Rule allows me to assign a role to relevant users. However, when I then go to add a relevant product for that user to the cart, I see an error message.

Error: Call to undefined method DatabaseStatementBase::fetchArray() in uc_discounts_get_discounts_for_order()

I've flagged this at:

https://github.com/backdrop-contrib/uc_discounts/issues/15

 

*update*

This appears to be caused by setting Max uses per user to anything other than the default of 0. I had it set to 1. Reset to 0 and the error goes away.

@bugfolder - Interestingly, what I'm trying to do is covered by (your) UC Price per Role module. Wish I'd come across that sooner!

https://backdropcms.org/project/uc_price_per_role

I have very few 'products', which are in fact annual membership subscriptions, and the prices can be presented in full according to role. And the role has been defined by separate criteria using Rules, as you suggested.

No need for Discounts at all in my case, but I appreciated the help while the bugs were highlighted and fixed!

Great community support. I love BackdropCMS.

bugfolder's picture

Good to know. I didn't suggest uc_price_per_role initially because one needs to set the discounted price on each product, which could be tedious for lots of products. Glad it works for you! I don't currently use uc_discounts (so don't run across the bugs in regular usage), but do use uc_price_per_role regularly, so it gets more real-world testing. (Nevertheless, if you find issues with it, do post them in the queue, and I'll investigate ASAP.)