-3

The issue is Output on related products it shows on every related product box same total counted number in OpenCart. So, it see just main product_id. So, must indicate related product_id somehow. I tried many many ways but it don`t work for me. Can someone help me to fix this?

There is function on modules:

public function getUnitsSold($product_id) {
                    $query = $this->db->query("SELECT SUM(op.quantity) AS total FROM `" . DB_PREFIX . "order_product` op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id = '5' AND op.product_id = '" . (int)$product_id . "'"); 
                if ($query->row) {
                    return $query->row['total'];
                } else {
                    return FALSE;
                }
            } 

And this is simply template output.

         <?php if ($tproducts) { ?>
           <?php foreach ($tproducts as $product) { ?>
        <?php if ($product['units_sold']) { ?>
        <?php echo $text_units_sold; ?> <?php echo $product['units_sold']; ?> 
        <?php } ?>
        <?php } ?>
        <?php } ?>

In controller where is related product array is a possibility to indicate maybe this function query and then make right output. something like that units_sold?

$data['tproducts'][] = array(

                'product_id'  => $result['product_id'],

                'units_sold'  => $this->db->query("SELECT SUM(op.quantity) AS total FROM `" . DB_PREFIX . "order_product` op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id = '5' AND op.product_id = '" . (int)$product_id . "'"),

            );

or maybe

$data['tproducts'][] = array(

                'product_id'  => $result['product_id'],

                'units_sold'  => $this->model_catalog_product->getUnitsSold($this->request->get['product_id']),

            );
HDP
  • 4,005
  • 2
  • 36
  • 58
Ice Q
  • 13
  • 5

1 Answers1

0

You'll want to store the amount of sold items on the item itself. Increment the number as if it is a reverse stock. So, I'd just ALTER TABLE oc_product ADD amount_sold INT(8) NULL DEFAULT NULL;

Then, just display that simple column value. The storage of the orders must be modified and becomes a little bit slower (in the order of .001 - .0001s).

This way the store will keep performing well in the future, even when your order item table has become huge.

Let the storage handler be the working horse. Take a look at ModelCheckoutOrder::addOrderHistory(), when they subtract the quantity, you must add your quantity :)

"UPDATE " . DB_PREFIX . "product SET /*.... opencart default shit */, amount_sold = (amount_sold + " . (int)$order_product['quantity'] . ")

twicejr
  • 1,319
  • 3
  • 13
  • 21
  • Soo it should look like that ? $this->db->query("UPDATE " . DB_PREFIX . "product SET quantity = (quantity - amount_sold = (amount_sold + " . (int)$order_product['quantity'] . ") WHERE product_id = '" . (int)$order_product['product_id'] . "' AND subtract = '1'"); – Ice Q Aug 08 '16 at 14:04
  • If the storage works ok now, you just need to use the column 'amount_sold'. Step 1: Modifiy ControllerProductProduct::index() to include 'amount_sold' in $data. (It will be available from $this->model_catalog-_product->getProduct() near line 159, because it selects * from the product table already). So just add in if($product_info){$data['amount_sold'] = $product_info['amount_sold'] Step 2: Modify template to point to 'amount_sold'. Done :) – twicejr Aug 09 '16 at 09:49