Using Drupal 4.7 RC

I'm having an Fatal error

Fatal error: Cannot use object of type stdClass as array in C:\Program Files\xampp\htdocs\team80\modules\custom.module on line 93

Files: 
CommentFileSizeAuthor
#16 ecommerce-ec_store_charge_inc-57538.patch717 bytesSoren Jones
PASSED: [[SimpleTest]]: [MySQL] 14 pass(es).
[ View ]
#15 ecommerce-ec_store-57538.patch746 bytesSoren Jones
PASSED: [[SimpleTest]]: [MySQL] 14 pass(es).
[ View ]
#11 ecommerce-ec_store-57538.patch876 bytesSoren Jones
PASSED: [[SimpleTest]]: [MySQL] 14 pass(es).
[ View ]

Comments

neclimdul’s picture

Sounds like this is your module. Can you give us any more information as to why this might be related to the ecommerce modules?

Eglish’s picture

Category:bug» support

Changing to support since this is a custom module. ton_cut345, please make sure this has to do with e-commerce, not drupal.

sime’s picture

Status:Active» Closed (fixed)

Closing: no follow-up from OP

mkmk’s picture

Component:other» ec_store
Status:Closed (fixed)» Active

I am having the same problem, reported against the ec_store component. The error message is:

Fatal error: Cannot use object of type stdClass as array in /home/XXXXXX/public_html/modules/ecommerce/ec_store/ec_store.module on line 1185

I am using:

Drupal 6.14
PHP 5.2.9
(the latest ec_commerce module)

The code on line 1185 reads:

if ($item[$key] != $value) {

I suspect that the error is that $item[$key] cannot be dereferenced as it is and may need to be written as $item->$key. But I'm not a PHP developer. It also seems that the entire module is written this way (using [brackets]), so I'm not sure what the issue is. Is this a problem with the PHP version I'm using ?

mkmk’s picture

Category:support» bug
gordon’s picture

Please correct the version information as HEAD is not currently in use, and will not be used until we start doing the Drupal 7 release

benmoreassynt’s picture

Found in 6.x-4.0-rc18

The fatal error only started to appear after some changes to the ecommerce configuration. Trying to retrace steps to find out what caused the bug to appear in the cart. The fatal error happens when you click on the 'Place your order' button. As already noted, it appears to be a case of an object being used as an array.

The full PHP error is:

Fatal error: Cannot use object of type stdClass as array in /path/to/sites/all/modules/ecommerce/ec_store/ec_store.module on line 1182

UPDATED: The error arose after implementing tax calculations through the 'Additional Charges' setting in the e-Commerce config.

benmoreassynt’s picture

Version:master» 6.x-4.0-rc18

I've written a workaround - not sure if this is required as a patch for a genuine bug in ec_store.module, or whether it's just a handy hack. The error is very simple - because some other component is passing an object where an array is expected. A solution is simply to handle objects and arrays by adding the if (is_array(...)) and else if (is_object(...)). Not sure if that is a hack to work around someone else's error or a proper fix.

<?php
function ec_store_filter_misc($item, $init = 'process') {
 
$params =& ctools_static(__FUNCTION__ . '_params');

  if (
$init == 'init') {
   
$params = $item;
  }
 
  if (
is_array($params)) {
    foreach (
$params as $key => $value) {
   
        if (
is_object($item)) {
        if (
$item->$key !=$value) {
            return
FALSE;
        }
    }
    else if (
is_array($item)){
          if (
$item[$key] != $value) {
                return
FALSE;
              }
        }
   }
  }
  return
TRUE;
}
?>
benmoreassynt’s picture

Title:Fatal error: Cannot use object of type stdClass as array» Fatal error: Cannot use object of type stdClass as array - (Object passed where array expected)
Priority:Critical» Normal
Status:Active» Needs review

I'd appreciate comments on whether the code above is required as a patch or not. Cheers.

gordon’s picture

Status:Needs review» Needs work

a patch will be much better as it will get run through the automated testing. also it allows me to see exactly what has been changed.

Soren Jones’s picture

Status:Needs work» Needs review
StatusFileSize
new876 bytes
PASSED: [[SimpleTest]]: [MySQL] 14 pass(es).
[ View ]

Code above attached as a patch. I'm not working with "additional charges" now, so I haven't tested this myself.

Soren Jones’s picture

benmoreassynt’s picture

The duplicate patch failed testing. This one appears to have passed. Thanks for turning it into a patch.

gordon’s picture

Status:Needs review» Needs work
+++ ec_store/ec_store.module 11 Jul 2010 17:02:54 -0000
@@ -1179,12 +1179,19 @@ function ec_store_filter_misc($item, $in
+    ¶
+        if (is_object($item)) {
+        if ($item->$key !=$value) {
+            return FALSE;
+        }
     }
+    else if (is_array($item)){
+          if ($item[$key] != $value) {
+                return FALSE;
+              }
+        }
+   }

please use Drupal standards when formatting code.

Powered by Dreditor.

Soren Jones’s picture

Version:6.x-4.0-rc18» 6.x-4.x-dev
Status:Needs work» Needs review
StatusFileSize
new746 bytes
PASSED: [[SimpleTest]]: [MySQL] 14 pass(es).
[ View ]
Soren Jones’s picture

StatusFileSize
new717 bytes
PASSED: [[SimpleTest]]: [MySQL] 14 pass(es).
[ View ]

Question: if the issue is arising "after implementing tax calculations through the 'Additional Charges' setting in the e-Commerce config," is it possible that the root of the issue is in ec_store.charge.inc?

On line 69 in ec_store_misc_group_filter_process we have:

<?php
function ec_store_misc_group_filter_process($type, $settings, $object, $charges = array()) {
?>

But on line 91 in ec_store_misc_group_variable_process we have:

<?php
function ec_store_misc_group_variable_process($type, $settings, $object, $charges) {
?>

Could changing $charges in function ec_store_misc_group_variable_process($type, $settings, $object, $charges) to $charges = array() address the issue? Patch attached, just in case.

For reference, ec_store_filter_misc is also called in ec_store.alloc.inc.

gordon’s picture

Status:Needs review» Fixed

Thanks, Fixed and both patches applied to dev

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.