Downloadable product : add file links to downloadable product programmatically - magento

I am trying to add link to downloadable product programmatically, below is the code on which i am working.
$linkfile = array();
$_highfilePath = $FolderPath.DS.$fname;
$linkfile[] = array(
'file' => $filePath,
'name' => $fname,
'size' => filesize($filePath),
'status' => 'new'
);
$linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
Mage_Downloadable_Model_Link::getBaseTmpPath(),
Mage_Downloadable_Model_Link::getBasePath(),
$linkfile
);
$linkModel = Mage::getModel('downloadable/link')->setData(array(
'product_id' => $product->getId(),
'sort_order' => 0,
'number_of_downloads' => 0, // Unlimited downloads
'is_shareable' => 2, // Not shareable
'link_url' => '',
'link_type' => 'file',
'link_file' => json_encode($linkfile),
'sample_url' => $SamplePathUrl,
'sample_file' => json_encode($linkfile),
'sample_type' => 'file',
'use_default_title' => true,
'default_price' => 0,
'price' => 0,
'store_id' => 0,
'website_id' => $product->getStore()->getWebsiteId(),
));
and this is the error i get An error occurred while saving the file(s)..
Please help

Impossible to diagnose with certainty without more information. Here's the code for the call that's failing:
/**
* Checking file for moving and move it
*
* #param string $baseTmpPath
* #param string $basePath
* #param array $file
* #return string
*/
public function moveFileFromTmp($baseTmpPath, $basePath, $file)
{
if (isset($file[0])) {
$fileName = $file[0]['file'];
if ($file[0]['status'] == 'new') {
try {
$fileName = $this->_moveFileFromTmp(
$baseTmpPath, $basePath, $file[0]['file']
);
} catch (Exception $e) {
Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).'));
}
}
return $fileName;
}
return '';
}
A good place to start would be checking filesystem permissions, making sure your datatypes match the types specified in the function comments, and making sure all files and directories exist.

Related

Yii2 max_size validation issue

This is my action:
public function actionCustom() {
$model = new Custom();
$model->load(\Yii::$app->request->post());
if ($model->validate()) {
// emptying the model's data
$model = new Custom();
var_dump('good');
} else {
var_dump('bad');
}
var_dump($_FILES);
return $this->render('custom', [
'model' => $model
]);
}
And this is my model:
class Custom extends Model
{
public $file;
public function rules()
{
return [
// ['file', 'file', 'extensions' => ['png', 'jpg', 'jpeg', 'gif', 'txt'], 'maxSize' => 1024 * 100]
['file', 'file', 'maxSize' => 1024 * 100],
];
}
}
When I try to upload a file which size exceeds the maxSize rule I set, the client-side validation displays an error and I can't submit the form by clicking on the button and this is all fine, but I can force submitting by typing something like this in the console like how a hacker would do:
document.forms[0].submit()
And I get this output:
C:\wamp64\www3\controllers\SiteController.php:138:string 'good' (length=4)
C:\wamp64\www3\controllers\SiteController.php:143:
array (size=1)
'Custom' =>
array (size=5)
'name' =>
array (size=1)
'file' => string 'tste.txt' (length=8)
'type' =>
array (size=1)
'file' => string 'text/plain' (length=10)
'tmp_name' =>
array (size=1)
'file' => string 'C:\wamp64\tmp\phpDE60.tmp' (length=25)
'error' =>
array (size=1)
'file' => int 0
'size' =>
array (size=1)
'file' => int 818064
string 'good' means that the file has passed the validation, but how?! The size of the file I sent was 818064 and it is bigger than 102400 (1024 * 100) file size limit which I set.
What am I doing wrong?
Have you tried to use yii\web\UploadedFile::getInstance() method mentioned in the official docs example?
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$model->imageFile = UploadedFile::getInstance($model, 'imageFile');
if ($model->upload()) {
// file is uploaded successfully
return;
}
}
http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html#wiring-up

Returning user input after AJAX call in Drupal 7 hook_form_FORM_ID_alter

I am using the hook_form_FORM_ID_alter in Drupal 7 to create a custom form so that the user can enter and edit data which is then attached to a custom node type.
For a new node the default number of input boxes in a group is 10, this can then be added to in groups of 5. When the node is reloaded for editing the saved data is used to create the form with whatever number of inputs have been saved previously and also the ability to add more fields in the same manner as required.
I have managed to get both the initial version of the form and the editing version to work using the following code however when the 'add five' button is pressed and the AJAX called (in both cases), any values which have been entered without saving are removed.
<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function entries_form_form_entries_node_form_alter(&$form, &$form_state, $form_id) {
$node = $form['#node'];
// fieldset
$form["section"] = array(
'#type' => 'fieldset',
'#title'=> 'Section 1',
);
$form["section"]["termwrapper"] = array(
"#type" => 'container',
"#attributes" => array(
"id" => 'groupWrapper',
),
);
$form["section"]["termwrapper"]["terms"]["#tree"] = TRUE;
if(!isset($form_state['fired'])){
$form_state['terms'] = $node->entries_form['term'];
}
foreach ($form_state['terms'] as $key => $values) {
$form["section"]["termwrapper"]["terms"][$key] = array(
'#type' => 'textfield',
'#size' => 20,
'#attributes' => array(
'class' => array('left'),
),
'#value' => $values,
);
}
$form['section']['addFive_button'] = array(
'#type' => 'submit',
'#value' => t('+5'),
'#submit' => array('entries_form_add_five_submit'),
'#ajax' => array(
'callback' => 'entries_form_commands_add_callback',
'wrapper' => 'groupWrapper',
),
'#prefix' => "<div class='clear'></div>",
);
dpm($form_state);
}
function entries_form_commands_add_callback($form, $form_state) {
return $form["section"]["termwrapper"];
}
function entries_form_add_five_submit($form, &$form_state){
$form_state['rebuild'] = TRUE;
$form_state['fired'] = 1;
$values = $form_state['values'];
$form_state['terms'] = $values['terms'];
$numterms = count($values['terms']);
$addfivearray = array_fill($numterms,5,'');
$form_state['terms'] = array_merge($values['terms'],$addfivearray);
}
/**
* Implements hook_node_submit().
*/
function entries_form_node_submit($node, $form, &$form_state) {
$values = $form_state['values'];
$node->entries_form['term'] = $values['term'];
}
/**
* Implements hook_node_prepare().
*/
function entries_form_node_prepare($node) {
if (empty($node->entries_form)){
$node->entries_form['term'] = array_fill(0, 10, '');
}
}
/**
* Implements hook_node_load().
*/
function entries_form_node_load($nodes, $types) {
if($types[0] == 'entries'){
$result = db_query('SELECT * FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)))->fetchAllAssoc('nid');
foreach ($nodes as &$node) {
$node->entries_form['term'] = json_decode($result[$node->nid]->term);
}
}
}
/**
* Implements hook_node_insert().
*/
function entries_form_node_insert($node) {
if (isset($node->entries_form)) {
db_insert('mytable')
->fields(array(
'nid' => $node->nid,
'term' => json_encode($node->entries_form['term']),
))
->execute();
}
}
How can I keep the values which have been typed in and retain the ajax functionality?
Any help or pointers much appreciated. This is my first dip into Drupal so I'm sure there is something which hopefully is quite obvious that I'm missing.
Ok, I think I finally have the answer.
Within the foreach that builds the form input boxes I had set '#value' => $values, when it seems that '#default_value' => $values, should have been set instead.
The updated section of the code that is now working for me is as follows
foreach ($form_state['terms'] as $key => $values) {
$form["section"]["termwrapper"]["terms"][$key] = array(
'#type' => 'textfield',
'#size' => 20,
'#attributes' => array(
'class' => array('left'),
),
'#default_value' => $values,
);
}
Seems to be as simple as that. Hope this helps someone else.

Attach Image to Product with Drupal Api

I am trying to programatically add products to my drupal commerce store. So far I have been able to add products that contain basic information ( such as sku, title, and price ). How would I be able to add field data, such as images and other field types, using drupal's api.
The code I used to add the products is derived from the commerce_examples/product_example module.
<?php
/**
* #file product_example.module
* Demonstrates pricing hooks, etc.
*/
/**
* Implements hook_menu().
*
* Simply presents a page that will explain what this module is for.
* hook_menu() has nothing to do with the checkout page functionality.
*/
function product_example_menu() {
$items['commerce_examples/product_example'] = array(
'title' => 'Product Example',
'page callback' => 'drupal_get_form',
'page arguments' => array('product_example_info_page'),
'access callback' => TRUE,
);
return $items;
}
/**
* This form provides a way to interact with some of the example functions
* provided here.
*/
function product_example_info_page($form, &$form_state) {
$form['explanation'] = array(
'#type' => 'item',
'#markup' => t('This example demonstrates product creation and manipulation.'),
);
$form['product_creation'] = array(
'#type' => 'fieldset',
'#title' => t('Please create a product for use with this example'),
);
$types = commerce_product_types();
$form['product_creation']['product_type'] = array(
'#type' => 'select',
'#title' => t('Product type for product to be created'),
'#options' => drupal_map_assoc(array_keys($types)),
);
$form['product_creation']['title'] = array(
'#title' => t('Product title'),
'#type' => 'textfield',
'#default_value' => t('A dummy product for use with product_example'),
);
$form['product_creation']['price'] = array(
'#title' => t('Product price'),
'#type' => 'textfield',
'#description' => t('A price in decimal format, without a currency symbol'),
'#default_value' => '100.00',
);
$form['product_creation']['product_creation_submit'] = array(
'#type' => 'submit',
'#value' => t('Create product'),
'#submit' => array('product_example_product_creation_submit')
);
return $form;
}
/**
* Submit handler for creating a product.
*/
function product_example_product_creation_submit($form, &$form_state) {
$extras = array(
'sku' => 'product_example_' . drupal_hash_base64(microtime()),
'status' => TRUE,
'uid' => $GLOBALS['user']->uid,
'title' => $form_state['values']['title'],
);
// Use the product example's creation function to create a product.
$product_id = product_example_create_product($form_state['values']['product_type'], $form_state['values']['price'], $extras);
drupal_set_message(t('Created sample product with title !title and sku !sku', array('!title' => l($extras['title'], 'admin/commerce/products/' . $product_id), '!sku' => $extras['sku'])));
}
/**
* Create a product programmatically.
*
* This is stolen shamelessly from commerce_bpc. Thanks for the help here!
*
* #param $product_type
* (string) The name of the product type for which products should be created.
* #param $price
* Decimal amount of price. If additional fields need to be populated they
* can be populated in exactly the same way as the commerce_price field.
* #param $extras
* An array for the values of 'extra fields' defined for the product type
* entity, or patterns for these. Recognized keys are:
* - status
* - uid
* - sku
* - title
* Note that the values do NOT come in the form of complex arrays (as they
* are not translatable, and can only have single values).
* #return
* The ID of the created product.
*/
function product_example_create_product($product_type, $price, $extras) {
$form_state = array();
$form_state['values'] = array();
$form = array();
$form['#parents'] = array();
// Generate a new product object
$new_product = commerce_product_new($product_type);
$new_product->status = $extras['status'];
$new_product->uid = $extras['uid'];
$new_product->sku = $extras['sku'];
$new_product->title = $extras['title'];
$new_product->created = $new_product->changed = time();
//commerce_price[und][0][amount]
$price = array(LANGUAGE_NONE => array(0 => array(
'amount' => $price * 100,
'currency_code' => commerce_default_currency(),
)));
$form_state['values']['commerce_price'] = $price;
// Notify field widgets to save their field data
field_attach_submit('commerce_product', $new_product, $form, $form_state);
commerce_product_save($new_product);
return $new_product->product_id;
}
Using the same format the examples uses to add price data:
//commerce_price[und][0][amount]
$price = array(LANGUAGE_NONE => array(0 => array(
'amount' => $price * 100,
'currency_code' => commerce_default_currency(),
)));
I was able to add other field data, however I am still having trouble adding data to the field_productimage field that all commerce products come with by default.
I can suggest you about other fields data such as extra info related to product. You can add these fields to the product type from the UI or if you are creating product type programmatically then also you can do the same.
Add fields in your form to collect the data e.g -
$form['product_creation']['values'] = array(
'#title' => t('Some data'),
'#type' => 'textfield',
'#description' => t('Dummy data'),
);
Get those data in your function product_example_create_product() by $form_state().
Add them to $new_product object like
$new_product->field_name['und'][0]['value'] = $extras['values'];
Save the product, just like you are doing.
For the image, you will have to follow the method of file attachment. Like -
$file_path = drupal_realpath('image/product_image.png');
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => file_get_mimetype($file_path),
'status' => 1,
);
// We save the file to the root of the files directory.
$file = file_copy($file, 'public://');
$new_product->product_image[LANGUAGE_NONE][0] = (array)$file;

Make an error message on Magento checkout page (Shipping Method stage)

I developed a Magento shipping method module and it works fine.
I can retrieve the list of available shipping methods from my remote service but I want to have an error message if something will be wrong with connection to the srvice or etc.
The code of collectRates carrier class is listed below.
public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
if (!$this->getConfigFlag('active')) {
return false;
}
require_once Mage::getBaseDir() . '/app/code/local/Citymail/CitymailDeliveryOption/lib/syvo_service_connect.php';
$citymail_service_url = $this->getConfigData('service_url') . '/syvo_service';
$connection = new syvo_service_connect($citymail_service_url);
if($connection->connect()) {
$handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
$rate_result = Mage::getModel('shipping/rate_result');
$product_qty = 0;
$products_data = array();
$currency = Mage::app()->getStore()->getCurrentCurrencyCode();
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach($quote->getAllVisibleItems() as $cartItem) {
$product_qty += $cartItem->getQty();
$products_data[$cartItem->getId()] = array(
'length_units' => 'cm',
'length' => 0,
'width' => 0,
'height' => 0,
'weight_units' => 'kg',
'weight' => $cartItem->getProduct()->getWeight(),
'shippable' => 'a',
'sell_price' => $cartItem->getProduct()->getPrice(),
'currency' => $currency,
);
}
$shipping_address = Mage::getModel('checkout/cart')->getQuote()->getShippingAddress();
$password = md5($this->getConfigData('service_password'));
$params = array(
'passw=' . $password,
'login=' . $this->getConfigData('service_login'),
'action=get_shipping_options',
'product_qty=' . $product_qty,
'products_data=' . json_encode($products_data),
'country=' . $shipping_address->getCountryId(),
'postal_code=' . $shipping_address->getPostcode(),
);
$response = $connection->send(implode('&', $params), 0, 30);
$response_status_line = $connection->getResponseStatusLine();
if((int)$response_status_line[1] == 403) {
//Access denied message
}
else {
$response_array = json_decode($response);
$citymail_service_points_data = array();
if($response_array->syvo_service_responce->type == 'success') {
$options = array();
foreach($response_array->syvo_service_responce->data->syvo_shipping_methods as $key_method => $shipping_method) {
$method_key = 'opt--' . $shipping_method->type . '--' . $shipping_method->distributor;
if($shipping_method->type == 'DHL_SERVICE_POINT') {
$citymail_service_points_data['service_points'][$shipping_method->id] = array(
'dhl_service_point_postcode' => $shipping_method->postcode,
'dhl_service_point_address' => $shipping_method->address,
);
$method_key .= '--' . $shipping_method->id;
}
$method = Mage::getModel("shipping/rate_result_method");
$method->setCarrier($this->_code);
$method->setCarrierTitle(Mage::getStoreConfig('carriers/' . $this->_code . '/title'));
$method->setMethod($method_key);
$method->setMethodTitle($shipping_method->text);
$method->setCost($shipping_method->rate);
$method->setPrice($shipping_method->rate + $handling);
$rate_result->append($method);
}
$street = $shipping_address->getStreet();
$citymail_temporary_order_data = array(
'login' => $this->getConfigData('service_login'),
'password' => $password,
'country_id' => $shipping_address->getCountryId(),
'company_name' => $shipping_address->getCompany(),
'name' => trim($shipping_address->getFirstname() . ' ' . $shipping_address->getLastname()),
'postal_code' => $shipping_address->getPostcode(),
'phone_number' => $shipping_address->getTelephone(),
'email' => $shipping_address->getEmail(),
'street' => $street[0],
'city' => $shipping_address->getCity(),
'matrix_id' => $response_array->syvo_service_responce->data->matrix_id,
'service_url' => $citymail_service_url,
);
$citymail_temporary_order_data = $citymail_service_points_data + $citymail_temporary_order_data;
Mage::getSingleton('core/session')->setCitymailTemporaryOrderData($citymail_temporary_order_data);
}
else {
//Some error handler
$error = Mage::getModel("shipping/rate_result_error");
$error->setCarrier('citymaildeliveryoption');
$error->setErrorMessage('sasasas');
$rate_result->append($error);
}
}
}
else {
}
return $rate_result;
}
This code has to return an error message if the module couldn't retrieve the list of shipping methods:
$error = Mage::getModel("shipping/rate_result_error");
$error->setCarrier('citymaildeliveryoption');
$error->setErrorMessage('sasasas');
$rate_result->append($error);
But the error is not displaying.
I checked an existing (core) Magento modules which use error handlers too (Ups, Usps modules) and error handlers of these modules also do not work, error message is not displaying.
Could you please advice some solution of this problem.
Tnank you!
Your code is good. You just need a little bit configuration for your carrier to always displays its methods.
Has shown in Mage_Shipping_Model_Shipping->collectCarrierRates() (line 176), Magento checks for 'showmethod' in your carrier config if a result is in error (and discard it, if it evaluates to 0) :
if ($carrier->getConfigData('showmethod') == 0 && $result->getError()) {
return $this;
}
So you just have to add this in your config.xml :
<?xml version="1.0" encoding="UTF-8"?>
<config>
<default>
<carriers>
<citymaildeliveryoption>
<showmethod>1</showmethod>
</citymaildeliveryoption>
</carriers>
</default>
</config>

Magento custom image upload doesn't work

I need to create an image upload field for posts in Magento Blog Module (AW blog).
Therefore, each post needs to contain an image.
I edited the following files:
/home/clients/websites/w_gale/public_html/gale/app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php
added fieldset like this:
$fieldset->addField('fileinputname', 'image', array(
'label' => Mage::helper('blog')->__('Upload image'),
'required' => false,
'name' => 'fileinputname',
'required' => true,
'style' => 'height:100px;border:2px solid #999;',
));
on top of this file, in the right place, I defined form like this:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
)
);
In Blogcontroller.php I added the following code, just bellow the if ($data = $this->getRequest()->getPost()) { line
if(isset($_FILES['fileinputname']['name']) && (file_exists($_FILES['fileinputname']['tmp_name']))) {
try {
$uploader = new Varien_File_Uploader('fileinputname');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['fileinputname']['name']);
$data['imageurl'] = $_FILES['fileinputname']['name'];
} catch(Exception $e) {
}
} else {
if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1)
$data['imageurl'] = '';
else
unset($data['fileinputname']);
}
However, the upload doesn't work. What am I doing wrong?
I added a special row in appropriate field in a database.
The frontend section displays the database value when I enter it manually.
Thanks
This code of method from data helper which uploads image. You need implement method getBaseDir() (which returns dir where you wish store your uploaded files) by yourself.
/**
* Upload image and return uploaded image file name or false
*
* #throws Mage_Core_Exception
* #param string $scope the request key for file
* #return bool|string
*/
public function uploadImage($scope)
{
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('ImageSize', true, $this->_imageSize);
$adapter->addValidator('Size', true, $this->_maxFileSize);
if ($adapter->isUploaded($scope)) {
// validate image
if (!$adapter->isValid($scope)) {
Mage::throwException(Mage::helper('mycompany_mymodule')->__('Uploaded image is not valid'));
}
$upload = new Varien_File_Uploader($scope);
$upload->setAllowCreateFolders(true);
$upload->setAllowedExtensions(array('jpg', 'gif', 'png'));
$upload->setAllowRenameFiles(true);
$upload->setFilesDispersion(false);
if ($upload->save($this->getBaseDir())) {
return $upload->getUploadedFileName();
}
}
return false;
}
You're using the right code. I solved the problem by using the right MYSQL data type. When I changed the data type from 'text' to 'varchar(255)' it solved the problem
And ... make sure that you add the following code:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
)
);
in /app/code/community/AW/Blog/Block/Manage/Blog/Edit/Form.php
NOT: app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

Resources