When the subproducts_surcharge_extra function is call, it should return:
attribute (+$123) or
attribute (-$123) or
attribute

but it only shows
attribute

the code was wrong in two points:
1- Doesnt get the attribute id (assumes as 0 (?))
2- it assumes positve price change

instead:

function subproducts_surcharge_extra($attribute, $override = FALSE) {
   return ((variable_get('subproducts_dynamic_pricing', 0) || $override) && ($attribute->surcharge != 0)) ? t('%name (+%surcharge)', array('%name' => $attribute->name, '%surcharge' => payment_format($attribute->surcharge))) : $attribute->name;
}

should be:

function subproducts_surcharge_extra($attribute, $override = FALSE) {
   if ((variable_get('subproducts_dynamic_pricing', $attribute->aid) || $override) && ($attribute->surcharge != 0)) {
      ($attribute->surcharge > 0) ? $signal='+' : $signal='' ;
      return t('%name (%signal%surcharge)', array('%name' => $attribute->name, '%signal' => $signal, '%surcharge' => payment_format($attribute->surcharge)));
   }
   else {
      return $attribute->name;
   };
}

regards

massa

Comments

darren oh’s picture

Component: other » subproducts
Status: Needs review » Active

No patch is attached.

brmassa’s picture

Status: Active » Reviewed & tested by the community
StatusFileSize
new1.31 KB
recidive’s picture

Status: Reviewed & tested by the community » Needs review

Someone else besides the author needs to test the code before it can be commited.

nedjo’s picture

Title: Subproduct: doesnt show the atribute AND surcharge correctly » Subproduct: doesn't show the attribute AND surcharge correctly
Status: Needs review » Needs work
StatusFileSize
new1.15 KB

Thanks brmassa for flagging this issue and for your other fine work on subproducts.

The issue here is that there is no way to set the variable 'subproducts_dynamic_pricing', which should be a set to 0 or 1. Originally the idea was to implement a routine where changes in attribute surcharges would trigger updates of existing subproduct prices. This wasn't implemented. Without this, the display of surcharges could be incorrect, as the attribute surcharges may have changed after the subproduct was created.

As a beginning, here's a patch introducing the setting. But before applying this it would be better to implement updating of the subproduct price at the same time if this variable is set to enabled. I don't have time to do so so am not assigning this to myself.

brmassa?

brmassa’s picture

nedjo

as was thinking about do this auto-update feature. as long the code isnt commented, it was hard to find the proper way... but i need this feature so... voila: when you update a attribute's price all subproducts' prices will also be updated.

just after

db_query("UPDATE {ec_attribute} SET vid = %d, name = '%s', weight = %d, surcharge = '%s' WHERE aid = %d", $edit['vid'], $edit['name'], $edit['weight'], $edit['surcharge'], $edit['aid']);

it should be

    // update all products prices that have this attribute
    if (variable_get('subproducts_dynamic_pricing', 1)) {
      $nodes = db_query("SELECT pa.nid FROM {ec_product_attribute} pa WHERE pa.aid = %d", $edit['aid'] );
      if ($nodes) {
        while ($product = db_fetch_object($nodes)){
          $pparent_price = db_fetch_object(db_query("SELECT p2.price FROM {ec_product} p2 WHERE p2.nid = (SELECT p3.pparent FROM {ec_product} p3 WHERE p3.nid = %d )", $product->nid ));
          db_query("UPDATE {ec_product} p SET p.price = %d + (SELECT SUM(a.surcharge) as 'total surcharge' FROM {ec_attribute} a INNER JOIN {ec_product_attribute} pa ON pa.aid = a.aid WHERE pa.nid = %d ) WHERE p.nid = %d", $pparent_price->price, $product->nid, $product->nid );
        }
      }
    }

1* it wont update prices after the 'enable dynamic pricing' be turned on. i dont know where the code should be inserted (after _settings all variables are saved and thats all...) if you knows where, add this (similar, but not the same code as above):

    // update all products prices that have a attribute
    if (variable_get('subproducts_dynamic_pricing', 1)) {
      $nodes = db_query("SELECT pa.nid FROM {ec_product_attribute} pa" );
      if ($nodes) {
        while ($product = db_fetch_object($nodes)){
          $pparent_price = db_fetch_object(db_query("SELECT p2.price FROM {ec_product} p2 WHERE p2.nid = (SELECT p3.pparent FROM {ec_product} p3 WHERE p3.nid = %d )", $product->nid ));
          db_query("UPDATE {ec_product} p SET p.price = %d + (SELECT SUM(a.surcharge) as 'total surcharge' FROM {ec_attribute} a INNER JOIN {ec_product_attribute} pa ON pa.aid = a.aid WHERE pa.nid = %d ) WHERE p.nid = %d", $pparent_price->price, $product->nid, $product->nid );
        }
      }
    }

2* as i once said, i cant create a patch coz my modules have many other mods. i suggest to you do so.

regards,

Massa

brmassa’s picture

Status: Needs work » Closed (fixed)