Yes, sorry for being so unclear.
What I was trying to archieve was a way to add a “Video URL” to a colum in the Database Table (the Video or file will be uploaded to the server)
I’ve been doing this while looking at the addon “price_per_unit”, cause it seemed to do something similar (adding more data to the products table) because documentation was not so clear about what to do in my case.
-
I registered the
load_products_extra_data
hook, as it seemed to be what I wanted, just get an extra field. -
Coded the function
fn_add_videos_load_products_extra_data
to make it load the field (This is where I felt like I could take another turn, cause I dont think this does anything)
function fn_add_videos_load_products_extra_data(array &$extra_fields, array $products, array $product_ids, array $params)
{
if (!in_array('video_path', $params['extend'])) {
return;
}
$extra_fields['?:products']['fields'][] = 'video_path';
}
- Coded the TPL
update_product_availability.pre
. I made it to add another field to the Product Create form, at the moment its just a simple form, that uploads a file to the server.
{component name="configurable_page.section" entity="products" tab="detailed" section="add_videos"}
<hr>
{include file="common/subheader.tpl" title="Video vertical" target="#add_videos"}
<div id="add_videos" class="collapse in">
<h5>Video actual: {if $video_name}{$video_name}{else}Ninguno{/if}
</h5>
<div class="controls">
<form enctype="multipart/form-data" method="post">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
</form>
</div>
</div>
{/component} {* detailed :: add_videos *}
Then here it comes what I dont know what is wrong/what to do.
The controller. products.post.php
When the dispatch mode its “update”, this works fine, uploads the file and everything.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if($mode == "update" && !empty($_REQUEST['product_id'])){
if((isset($_FILES["file"])) && ($_FILES["file"]["size"] > 0))
{
$fileName = $_FILES["file"]["name"];//the files name takes from the HTML form
$fileTmpLoc = $_FILES["file"]["tmp_name"];//file in the PHP tmp folder
$fileErrorMsg = $_FILES["file"]["error"];//0 for false and 1 for true
$target_path = "./videos/" . basename( $_FILES["file"]["name"]);
$moveResult = move_uploaded_file($fileTmpLoc, $target_path);
$data = array(
"video_path" => $target_path
);
db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $data, $_REQUEST["product_id"]);
}
}
}
The problem would be when I try to make it work when the dispatch mode is “add” (creating a new product)
That is mostly the same code, but change the lines $mode == "update" && !empty($_REQUEST['product_id']
to $mode == "add" && !empty($_POST['product_id']
As I understand, when I add the “.post” to the file name, means that it takes place after the product has been inserted into the database, so updating by “product_id” should work.
Another thing I tried is trying to append video_path
to the request and Replacing into with the data of the request, still nothing.
I hope I was clearer this time, sorry.
Thanks in advance.
EDIT: Edited the whole post as a suggestion of making it clearer.