I am getting this following fatal error once I logged in as sub admin [ Custom user role created by super admin ] . If I logged in as super admin, then this error is not displaying but I am getting this error if i logged in as other user who has permission to access order administration.

Fatal error: __clone method called on non-object in
profiles/commerce_kickstart/modules/contrib/commerce_discount/commerce_discount.module on line 33

I hope the problem is with the following code in commerce_discount.module

Line 33:

$cloned_line_items[$wrapper_line_item->getIdentifier()] = clone($wrapper_line_item->value());

Please see it and let me know it. Thanks in advance.

Comments

gianlucarossi’s picture

Up? Have the same problem.

ralt’s picture

Hi,

I can't reproduce after trying to create a user with somehow admin access... can you describe a procedure to exactly reproduce?

Thanks,

ralt’s picture

Status: Active » Postponed (maintainer needs more info)
bechtold’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.45 KB

Hey,
I had exactly the same error when trying to access orders.
I can't really reproduce the error at the moment, but I believe ist must have been an anonymous order.
Because I was experimenting with those when the error appeared.

Seems to be a line-item that is empty. This fix I offer allowed me to get rid of the error and access the order page. After saving the order or the payment (I don't remember exactly) the error was gone.
The solution I found is maybe not the best, because I don't know what we miss out, or what this function should be doing.
So maybe the maintainers can say a word where this might come from.

Until they have spoken, I recommend to use the patch only if you are stuck on your orders page and don't find another solution.
Maybe it's a good idea to remove the patch again, as long as the error is not appearing.

Cheers
bechtold

andrewsizz’s picture

Status: Needs review » Needs work

Hey Oskar,
I think better will be:

if(!empty($line_item) && is_object($line_item)) {
  $cloned_line_items[$wrapper_line_item->getIdentifier()] = clone($line_item);

// Instead of

if(isset($line_item)) {
   $cloned_line_items[$wrapper_line_item->getIdentifier()] = clone($wrapper_line_item->value());

As $line_item variable always will be and in this case isset always will returns TRUE.

rsvelko’s picture

my several cents:
- I downloaded 7 dev of this module
- the clone keyword is used only in one place on line 33
- here is some code context:

/**
 * Implements hook_commerce_cart_order_refresh().
 *
 * Remove any existing commerce discount line item.
 */
function commerce_discount_commerce_cart_order_refresh($wrapper) {
  $line_items_to_delete = array();
  $cloned_line_items = array();
  // Remove all discount references from the order.
  // (If there are none, setting array() would modify the order, so test first.)
  if ($wrapper->__isset('commerce_discounts') && $wrapper->commerce_discounts->value()) {
    $wrapper->commerce_discounts->set(array());
  }

  // Remove discount components from the order total price.
  commerce_discount_remove_discount_components($wrapper->commerce_order_total);

  foreach ($wrapper->commerce_line_items as $delta => $wrapper_line_item) {
    if ($wrapper_line_item->getBundle() == 'product_discount') {
      // Delete the line item, and remove it from the order.
      $line_items_to_delete[] = $wrapper_line_item->line_item_id->value();
      $wrapper->commerce_line_items->offsetUnset($delta);
    }
    else {
      $cloned_line_items[$wrapper_line_item->getIdentifier()] = clone($wrapper_line_item->value());

So, here is my patch attached - which is an adaptation of all other patches and the previous comment suggestion to use ! empty and is_object.

I am still not sure if we should check for isset or empty ? Maybe add isset check in front of the rest ?

rsvelko’s picture

Status: Needs work » Needs review
korlandi’s picture

I have this same error when accessing the Manage Orders page. For me, it happened when I uploaded a new inventory list. For the past three months the 30+ inventory lists I uploaded did not trigger this error, but this time it did. I scrubbed the file for data issues, but could not identify any. The inventory file only contained changes to price and quantity and active status, nothing about discounts.

Fatal error: __clone method called on non-object in .../profiles/commerce_kickstart/modules/contrib/commerce_discount/commerce_discount.module on line 33

It goes away if I disable the discount module altogether, which isn't ideal. The Coupon module seems to still work, so our commerce site is getting by using just that, but I really would like the extremely helpful Discount Module functionality.

Following this post to see if/when a solution is found. Thanks!

jmary’s picture

Patch #6 seems to have solved the issue.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @rsvelko This looks good, just a couple of nitpicks on #6:
Which could be fixed on commit.

  1. +++ b/commerce_discount.module
    @@ -30,13 +30,17 @@ function commerce_discount_commerce_cart_order_refresh($wrapper) {
    +      if ( ! empty($line_item) && is_object($line_item) ) {
    

    Spaces around the ! aren't part of the coding standards.

  2. +++ b/commerce_discount.module
    @@ -30,13 +30,17 @@ function commerce_discount_commerce_cart_order_refresh($wrapper) {
    +        $cloned_line_items[$wrapper_line_item->getIdentifier()] = clone($line_item);
    

    clone is a language construct not a function, doesn't need the parenthesis.

    http://php.net/clone

deggertsen’s picture

What's the difference between this issue and #2107617: Cloning discount offers is broken?

joelpittet’s picture

@deggertsen one is cloning the discount entity in the UI, and this one is an error in trying to clone a line item on order refresh.

joelpittet’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new829 bytes

The empty() check should actually do anything because it's an object and not an array or scalar value.

Here's why:
http://3v4l.org/FR7S2

How about this?

joelpittet’s picture

Version: 7.x-1.0-alpha3 » 7.x-1.x-dev

@deggertsen or @rsvelko if you are cool with that rational? I'll commit this patch.

deggertsen’s picture

Sounds good to me!

joelpittet’s picture

Status: Needs review » Fixed

Cool committed, thanks @deggertsen

  • joelpittet committed 97a9d95 on 7.x-1.x
    Issue #2335953 by joelpittet, rsvelko, bechtold: Fatal error: __clone...

Status: Fixed » Closed (fixed)

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