Hi Guys,
I am going for a slightly different approach and have hit a snag.
Let me lay out some background. I decided that instead of creating another table I would add the fields I needed to the existing Zen Cart tables. So I added a "products_code" field to "zen_products" table. This field holds a unique code for each product. It is a GUID type field because it is used later on to generate the registration codes.
I then added a "products_reg_key" field to the "zen_orders_products" table. This will be dynamically populated when the customer buys some games from my site.
At the point of successful sale, the registration key generator takes the customer's email address and the unique products_code and using some algorithms generates a unique products_reg_key. This products_reg_key is then emailed to the customer as part of the usual email notification system.
Ok the snag is that at the point of inserting the products_reg_key into the zen_orders_products table it seems to reuse the first one it generated. Most the changes take place in order.php as follows...
In function query()...
PHP Code:
$orders_products_query = "select orders_products_id, products_id, products_name,
products_model, products_price, products_tax,
products_quantity, final_price,
onetime_charges,
products_priced_by_attribute, product_is_free, products_discount_type,
products_discount_type_from, [B]products_reg_key[/B]
from " . TABLE_ORDERS_PRODUCTS . "
where orders_id = '" . (int)$order_id . "'";
$orders_products = $db->Execute($orders_products_query);
and
PHP Code:
$this->products[$index] = array('qty' => $new_qty,
'id' => $orders_products->fields['products_id'],
'name' => $orders_products->fields['products_name'],
'model' => $orders_products->fields['products_model'],
'tax' => $orders_products->fields['products_tax'],
'price' => $orders_products->fields['products_price'],
'final_price' => $orders_products->fields['final_price'],
'onetime_charges' => $orders_products->fields['onetime_charges'],
'products_priced_by_attribute' => $orders_products->fields['products_priced_by_attribute'],
'product_is_free' => $orders_products->fields['product_is_free'],
'products_discount_type' => $orders_products->fields['products_discount_type'],
'products_discount_type_from' => $orders_products->fields['products_discount_type_from'],
'products_reg_key' => $orders_products->fields['products_reg_key'] );
In function cart() is where I think it is not generating a unique code even though it is using the $i loop variable. But I can't see why it is not working. Here is the code...
PHP Code:
$this->products[$index] = array('qty' => $products[$i]['quantity'],
'name' => $products[$i]['name'],
'model' => $products[$i]['model'],
'tax' => zen_get_tax_rate($products[$i]['tax_class_id'], $tax_address->fields['entry_country_id'], $tax_address->fields['entry_zone_id']),
'tax_description' => zen_get_tax_description($products[$i]['tax_class_id'], $tax_address->fields['entry_country_id'], $tax_address->fields['entry_zone_id']),
'price' => $products[$i]['price'],
'final_price' => $products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']),
'onetime_charges' => $_SESSION['cart']->attributes_price_onetime_charges($products[$i]['id'], $products[$i]['quantity']),
'weight' => $products[$i]['weight'],
'products_priced_by_attribute' => $products[$i]['products_priced_by_attribute'],
'product_is_free' => $products[$i]['product_is_free'],
'products_discount_type' => $products[$i]['products_discount_type'],
'products_discount_type_from' => $products[$i]['products_discount_type_from'],
'id' => $products[$i]['id'],
'products_code' => $products[$i]['products_code'],
'products_reg_key' => generatecode( $this->customer['email_address'], $this->products[$i]['products_code'] ),
'rowClass' => $rowClass);
Then in function create_add_products() is where it then saves the reg_keys to the zen_orders_products table...
PHP Code:
$sql_data_array = array('orders_id' => $zf_insert_id,
'products_id' => zen_get_prid($this->products[$i]['id']),
'products_model' => $this->products[$i]['model'],
'products_name' => $this->products[$i]['name'],
'products_price' => $this->products[$i]['price'],
'final_price' => $this->products[$i]['final_price'],
'onetime_charges' => $this->products[$i]['onetime_charges'],
'products_tax' => $this->products[$i]['tax'],
'products_quantity' => $this->products[$i]['qty'],
'products_priced_by_attribute' => $this->products[$i]['products_priced_by_attribute'],
'product_is_free' => $this->products[$i]['product_is_free'],
'products_discount_type' => $this->products[$i]['products_discount_type'],
'products_discount_type_from' => $this->products[$i]['products_discount_type_from'],
'products_prid' => $this->products[$i]['id'],
'products_reg_key' => $this->products[$i]['products_reg_key'] );
zen_db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array);
And lastly, this is how I send the information to the customer...
PHP Code:
// build output for email notification
$this->products_ordered .= $this->products[$i]['qty'] . ' x ' . $this->products[$i]['name'] . ($this->products[$i]['model'] != '' ? ' (' . $this->products[$i]['model'] . ')' : '') . ' = ' .
$currencies->display_price($this->products[$i]['final_price'], $this->products[$i]['tax'], $this->products[$i]['qty']) .
($this->products[$i]['onetime_charges'] !=0 ? "\n" . TEXT_ONETIME_CHARGES_EMAIL . $currencies->display_price($this->products[$i]['onetime_charges'], $this->products[$i]['tax'], 1) : '') .
$this->products_ordered_attributes . "\n" .
' REG CODE : ' . $this->products[$i]['products_reg_key'] . "\n";
The last change that is required if
As I said, it all basically works if you only have 1 product in the basket that is bought. But it's when it the customer buys more than one is when the problem manifests itself.
Can any Zen-Cart gurus point out where I have gone wrong with the PHP code above. If you search through the code for "reg_code" you will see all the areas where I added registration specific PHP code.
Any suggestions welcome.
Bookmarks