Jump to content

Count Function In Php


Bartjuh1994

Recommended Posts

Hi All,

 

This code gives me the quantity output of the ordered products (per line):

(int)($product['product_quantity'])

 

Like:

Product 1 - 5x

Product 2 - 10x

 

So, i want a function, that outputs the total of the order, like:

Total products: 15x

 

How can i store the code in an array and give the total order output?

 

Thanks in advance!

Bart

Link to comment
https://forums.phpfreaks.com/topic/270668-count-function-in-php/
Share on other sites

Thanks for your reply,

 

However, i'm not sure about it.. Now i have:

 

$result = count ($product);, that gives me "0" as output.. the same for:

$result = count($product['product_quantity']);

 

I dont know if i'm do it right, i'm a php newb.

 

I hope you can help me,

Thanks

Its a big file, so i put the most relevant code below:

 

public function install()
{
 return parent::install() && $this->registerHook('newOrder') && $this->registerHook('updateQuantity') &&
 $this->registerHook('productOutOfStock') && $this->registerHook('customerAccount') &&
 $this->registerHook('updateProduct') && $this->registerHook('deleteProduct') &&
 $this->registerHook('deleteProductAttribute') && $this->registerHook('updateProductAttribute') &&
 $this->registerHook('myAccountBlock') && Configuration::updateValue('MA_MERCHANT_ORDER', 1) &&
 Configuration::updateValue('MA_MERCHANT_OOS', 1) && Configuration::updateValue('MA_CUSTOMER_QTY', 1) &&
 Configuration::updateValue('MA_MERCHANT_MAILS', Configuration::get('PS_SHOP_EMAIL')) &&
 Configuration::updateValue('MA_LAST_QTIES', Configuration::get('PS_LAST_QTIES')) &&
 Db::getInstance()->Execute('
 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'mailalert_customer_oos` (
   `id_customer` int(10) unsigned NOT NULL,
   `customer_email` varchar(128) NOT NULL,
   `id_product` int(10) unsigned NOT NULL,
   `id_product_attribute` int(10) unsigned NOT NULL,
   `id_lang` int(10) unsigned NOT NULL,
   `date_add` datetime NOT NULL,
   PRIMARY KEY (`id_customer`, `customer_email`, `id_product`, `id_product_attribute`, `id_lang`),
   KEY `customer_email` (`customer_email`),
   KEY `id_product` (`id_product`,`id_product_attribute`)
 ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
}

 

public function hookNewOrder($params)
{
 if (!$this->_merchant_order || empty($this->_merchant_mails))
  return;


 // Getting differents vars
 $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  $currency = $params['currency'];
 $configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL', 'PS_MAIL_METHOD', 'PS_MAIL_SERVER', 'PS_MAIL_USER', 'PS_MAIL_PASSWD', 'PS_SHOP_NAME'));
 $order = $params['order'];
 $customer = $params['customer'];
 $delivery = new Address((int)($order->id_address_delivery));
 $invoice = new Address((int)($order->id_address_invoice));
 $order_date_text = Tools::displayDate($order->date_add, (int)($id_lang));
 $carrier = new Carrier((int)($order->id_carrier));
 $message = $order->getFirstMessage();
 if (!$message || empty($message))
  $message = $this->l('No message');
 $itemsTable = '';
 $products = $params['order']->getProducts();
 $customizedDatas = Product::getAllCustomizedDatas((int)$params['cart']->id);
 Product::addCustomizationPrice($products, $customizedDatas);
 foreach ($products as $key => $product)
 {
  $unit_price = $product['product_price_wt'];
  $price = $product['total_price'];

  $customizationText = '';
  if (isset($customizedDatas[$product['product_id']][$product['product_attribute_id']]))
  {


   foreach ($customizedDatas[$product['product_id']][$product['product_attribute_id']] as $customization)
   {
 if (isset($customization['datas'][_CUSTOMIZE_TEXTFIELD_]))
  foreach ($customization['datas'][_CUSTOMIZE_TEXTFIELD_] as $text)
   $customizationText .= $text['name'].':'.' '.$text['value'].'<br />';

 if (isset($customization['datas'][_CUSTOMIZE_FILE_]))
  $customizationText .= count($customization['datas'][_CUSTOMIZE_FILE_]) .' '. Tools::displayError('image(s)').'<br />';

 $customizationText .= '---<br />';
   }

   $customizationText = rtrim($customizationText, '---<br />');
  }

  $itemsTable .=
   '<tr>
   <td width="10px">'.(int)($product['product_quantity']).' x</td>
   <td width="100px">'.$product['product_name'].(isset($product['attributes_small']) ? ' '.$product['attributes_small'] : '').(!empty($customizationText) ? '<br />'.$customizationText : '').'</td>
   <td width="100px">'.Tools::displayPrice(($unit_price * $product['product_quantity']), $currency, false).'</td>
   </tr> <tr><td width="15%"colspan="2">'.$product['product_reference'].'</td>
   <tr> <td>&nbsp</td> </tr>
   </tr>';
 }




 foreach ($params['order']->getDiscounts() as $discount)
 {
  $itemsTable .=
  '<tr style="background-color:#EBECEE;">
 <td>'.$this->l('Voucher code:').' '.$discount['name'].'</td>
 <td>-'.Tools::displayPrice($discount['value'], $currency, false).'</td>
  </tr>';
 }
 if ($delivery->id_state)
  $delivery_state = new State((int)($delivery->id_state));
 if ($invoice->id_state)
  $invoice_state = new State((int)($invoice->id_state));


 // Filling-in vars for email
 $template = 'new_order';
 $subject = $this->l('New order', false, (int)$id_lang).' - '.sprintf('%06d', $order->id);
 $templateVars = array(
  '{firstname}' => $customer->firstname,
  '{lastname}' => $customer->lastname,
  '{email}' => $customer->email,
  '{delivery_block_txt}' => $this->_getFormatedAddress($delivery, "\n"),
  '{invoice_block_txt}' => $this->_getFormatedAddress($invoice, "\n"),
  '{delivery_block_html}' => $this->_getFormatedAddress($delivery, "<br />", 
  array(
   'firstname' => '<span style="color:#DB3484; font-weight:bold;">%s</span>', 
   'lastname' => '<span style="color:#DB3484; font-weight:bold;">%s</span>')),
  '{invoice_block_html}' => $this->_getFormatedAddress($invoice, "<br />", 
  array(
   'firstname' => '<span style="color:#DB3484; font-weight:bold;">%s</span>',
   'lastname' => '<span style="color:#DB3484; font-weight:bold;">%s</span>')),
  '{delivery_company}' => $delivery->company,
  '{delivery_firstname}' => $delivery->firstname,
  '{delivery_lastname}' => $delivery->lastname,
  '{delivery_address1}' => $delivery->address1,
  '{delivery_address2}' => $delivery->address2,
  '{delivery_city}' => $delivery->city,
  '{delivery_postal_code}' => $delivery->postcode,
  '{delivery_country}' => $delivery->country,
  '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
  '{delivery_phone}' => $delivery->phone,
  '{delivery_other}' => $delivery->other,
  '{invoice_company}' => $invoice->company,
  '{invoice_firstname}' => $invoice->firstname,
  '{invoice_lastname}' => $invoice->lastname,
  '{invoice_address2}' => $invoice->address2,
  '{invoice_address1}' => $invoice->address1,
  '{invoice_city}' => $invoice->city,
  '{invoice_postal_code}' => $invoice->postcode,
  '{invoice_country}' => $invoice->country,
  '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
  '{invoice_phone}' => $invoice->phone,
  '{invoice_other}' => $invoice->other,
  '{order_name}' => sprintf("%06d", $order->id),
  '{shop_name}' => Configuration::get('PS_SHOP_NAME'),
  '{date}' => $order_date_text,
  '{count}' => $result,
  '{carrier}' => (($carrier->name == '0') ? Configuration::get('PS_SHOP_NAME') : $carrier->name),
  '{payment}' => Tools::substr($order->payment, 0, 32),
  '{items}' => $itemsTable,
  '{total_paid}' => Tools::displayPrice($order->total_paid, $currency),
  '{total_products}' => Tools::displayPrice($order->getTotalProductsWithTaxes(), $currency),
  '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
  '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
  '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
  '{currency}' => $currency->sign,
  '{message}' => $message
 );
 $iso = Language::getIsoById((int)$id_lang);
 if (file_exists(dirname(__FILE__).'/mails/'.$iso.'/'.$template.'.txt') && file_exists(dirname(__FILE__).'/mails/'.$iso.'/'.$template.'.html'))
  Mail::Send((int)$id_lang, $template, $subject, $templateVars, explode(self::__MA_MAIL_DELIMITOR__, $this->_merchant_mails), null, $configuration['PS_SHOP_EMAIL'], $configuration['PS_SHOP_NAME'], null, null, dirname(__FILE__).'/mails/');
}

 

I hope this is enough. Thanks!

Bart

I'm guessing here, but this should probably be where you want the code to go :

$counter = 0;
foreach ($products as $key => $product)   {
  $unit_price = $product['product_price_wt'];
  $price = $product['total_price'];
  $counter = $counter+$product['product_quantity'];
...

and then just echo out $counter at the end of the loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.