Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[*] Project: gives the possibility to use combinations for virtual products #90

Closed
wants to merge 10 commits into from
Expand Up @@ -31,7 +31,7 @@
var msg_new_combination = '{l s='New combination'}';
</script>

{if isset($product->id) && !$product->is_virtual}
{if isset($product->id)}
<input type="hidden" name="submitted_tabs[]" value="Combinations" />
<script type="text/javascript">
var attrs = new Array();
Expand Down

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions classes/ProductDownload.php
Expand Up @@ -29,6 +29,9 @@ class ProductDownloadCore extends ObjectModel
{
/** @var integer Product id which download belongs */
public $id_product;

/** @var integer Product attribute id which download belongs */
public $id_product_attribute;

/** @var string DisplayFilename the name which appear */
public $display_filename;
Expand All @@ -55,6 +58,7 @@ class ProductDownloadCore extends ObjectModel
public $is_shareable = 0;

protected static $_productIds = array();
protected static $_productIdAttributes = array();

/**
* @see ObjectModel::$definition
Expand All @@ -64,6 +68,7 @@ class ProductDownloadCore extends ObjectModel
'primary' => 'id_product_download',
'fields' => array(
'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'display_filename' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
'filename' => array('type' => self::TYPE_STRING, 'validate' => 'isSha1', 'size' => 255),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
Expand Down Expand Up @@ -315,4 +320,27 @@ public static function isFeatureActive()
{
return Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE');
}

/**
* Return the id_product_download from an id_product
*
* @param int $id_product Product the id
* @return integer Product the id for this virtual product
*/
public static function getIdFromCombination($id_product, $id_product_attribute)
{
if (!ProductDownload::isFeatureActive())
return false;
if (array_key_exists($id_product_attribute, self::$_productIdAttributes))
return self::$_productIdAttributes[$id_product_attribute];
self::$_productIdAttributes[$id_product_attribute] = (int)Db::getInstance()->getValue('
SELECT `id_product_download`
FROM `'._DB_PREFIX_.'product_download`
WHERE `id_product` = '.(int)$id_product.'
AND `id_product_attribute` = '.(int)$id_product_attribute.'
AND `active` = 1
ORDER BY `id_product_download` DESC');

return self::$_productIdAttributes[$id_product_attribute];
}
}
30 changes: 22 additions & 8 deletions classes/order/OrderDetail.php
Expand Up @@ -262,7 +262,7 @@ public static function getDownloadFromHash($hash)
if ($hash == '') return false;
$sql = 'SELECT *
FROM `'._DB_PREFIX_.'order_detail` od
LEFT JOIN `'._DB_PREFIX_.'product_download` pd ON (od.`product_id`=pd.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_download` pd ON (od.`product_id`=pd.`id_product` AND od.`product_attribute_id`=pd.`id_product_attribute`)
WHERE od.`download_hash` = \''.pSQL(strval($hash)).'\'
AND pd.`active` = 1';
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
Expand Down Expand Up @@ -385,13 +385,27 @@ protected function setVirtualProductInformation($product)
$this->download_deadline = '0000-00-00 00:00:00';
$this->download_hash = null;

if ($id_product_download = ProductDownload::getIdFromIdProduct((int)($product['id_product'])))
{
$productDownload = new ProductDownload((int)($id_product_download));
$this->download_deadline = $productDownload->getDeadLine();
$this->download_hash = $productDownload->getHash();

unset($productDownload);
if(!(int)($product['id_product_attribute']))
{
if ($id_product_download = ProductDownload::getIdFromIdProduct((int)($product['id_product'])))
{
$productDownload = new ProductDownload((int)($id_product_download));
$this->download_deadline = $productDownload->getDeadLine();
$this->download_hash = $productDownload->getHash();

unset($productDownload);
}
}
else
{
if ($id_product_download = ProductDownload::getIdFromCombination((int)($product['id_product']), (int)($product['id_product_attribute'])))
{
$productDownload = new ProductDownload((int)($id_product_download));
$this->download_deadline = $productDownload->getDeadLine();
$this->download_hash = $productDownload->getHash();

unset($productDownload);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion classes/order/OrderHistory.php
Expand Up @@ -352,8 +352,13 @@ public function addWithemail($autodate = true, $template_vars = false, Context $
$assign = array();
foreach ($virtual_products as $key => $virtual_product)
{
$id_product_download = ProductDownload::getIdFromIdProduct($virtual_product['product_id']);
if((int)$virtual_product['product_attribute_id'] > 0)
$id_product_download = ProductDownload::getIdFromCombination($virtual_product['product_id'], $virtual_product['product_attribute_id']);
else
$id_product_download = ProductDownload::getIdFromIdProduct($virtual_product['product_id']);

$product_download = new ProductDownload($id_product_download);

// If this virtual item has an associated file, we'll provide the link to download the file in the email
if ($product_download->display_filename != '')
{
Expand Down