Add a checkbox to product image gallery (like "Disable/Exclude") - magento

I'm trying to code a new checkbox to be appended to the columns in the image gallery, beside "Disable". Its behavior would be the same as "Disable/Exclude" = Yes/No with entry in the database.
The idea is to add a "Use as page" checkbox for each image in the image gallery. The goal being to make a JS carousel with all pictures checked as "Use as page".
I have a few things done but I cannot :
update data in the database => set 0 or 1 to the "page" field (see below)
retrieve data from the database and then check/uncheck the checkbox depending on the "page" field.
--> So my question is : how to update data in the database and retrieve it in the checkbox (0 or 1 depending on the field value) ?
Thanks all for your very precious help.
Here is what I've done (1.4.1.0) :
1- Update table catalog_product_entity_media_gallery_value
Added a new field (which name is "page") :
page tinyint(4) UNSIGNED No 0
2- Made the following changes to class Mage_Catalog_Model_Product_Attribute_Backend_Media
Line 49 :
from
$localAttributes = array('label', 'position', 'disabled');
to
$localAttributes = array('label', 'position', 'disabled', 'page');
Line 223 :
from
$data['disabled'] = (int) $image['disabled'];
to
$data['disabled'] = (int) $image['disabled'];
$data['page'] = (int) $image['page'];
Line 301
from
$mediaGalleryData['images'][] = array(
'file' => $fileName,
'position' => $position,
'label' => '',
'disabled' => (int) $exclude
);
to
$mediaGalleryData['images'][] = array(
'file' => $fileName,
'position' => $position,
'label' => '',
'disabled' => (int) $exclude,
'page' => (int) $exclude,
);
Line 328
from
$fieldsMap = array(
'label' => 'label',
'position' => 'position',
'disabled' => 'disabled',
'exclude' => 'disabled',
);
to
$fieldsMap = array(
'label' => 'label',
'position' => 'position',
'disabled' => 'disabled',
'exclude' => 'disabled',
'page' => 'disabled',
);
3- Made the following changes to template adminhtml/default/default/template/catalog/product/helper/gallery.phtml
Line 64
from
<th><?php echo Mage::helper('catalog')->__('Exclude') ?></th>
to
<th><?php echo Mage::helper('catalog')->__('Exclude') ?></th>
<th><?php echo Mage::helper('catalog')->__('Is Page') ?></th>
Line 77
from
<td class="cell-disable a-center"><input type="checkbox" <?php if($_block->getElement()->getReadonly()):?> disabled="disabled"<?php endif;?> onclick="<?php echo $_block->getJsObjectName(); ?>.updateImage('__file__')" /></td>
to
<td class="cell-disable a-center"><input type="checkbox" <?php if($_block->getElement()->getReadonly()):?> disabled="disabled"<?php endif;?> onclick="<?php echo $_block->getJsObjectName(); ?>.updateImage('__file__')" /></td>
<td class="cell-page a-center"><input type="checkbox" <?php if($_block->getElement()->getReadonly()):?> disabled="disabled"<?php endif;?> onclick="<?php echo $_block->getJsObjectName(); ?>.updateImage('__file__')" /></td>
Line 105
from
to
<td class="cell-disable"><input type="hidden" /> </td>
<td class="cell-page last"><input type="hidden" /> </td>

This is how I solved the problem and is working perfectly. Beside your changes make these too.
1. In Mage_Catalog_Model_Product_Attribute_Backend_Media
change
public function addImage(Mage_Catalog_Model_Product $product, $file,
$mediaAttribute = null, $move = false, $exclude = true)
to
public function addImage(Mage_Catalog_Model_Product $product, $file,
$mediaAttribute = null, $move = false, $exclude = true, $page = false)
change
public function addImagesWithDifferentMediaAttributes(Mage_Catalog_Model_Product $product,
$fileAndAttributesArray, $filePath = '', $move = false, $exclude = true)
to
public function addImagesWithDifferentMediaAttributes(Mage_Catalog_Model_Product $product,
$fileAndAttributesArray, $filePath = '', $move = false, $exclude = true, $page = true)
change
$savedFileName = $this->addImage($product, $filePath . $value['file'], null, $move, $exclude);
to
$savedFileName = $this->addImage($product, $filePath . $value['file'], null, $move, $exclude, $page );
2. Go to Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
change
array('label','position','disabled')
to
array('label','position','disabled','page')
change
array(
'label_default' => 'label',
'position_default' => 'position',
'disabled_default' => 'disabled',
)
to
array(
'label_default' => 'label',
'position_default' => 'position',
'disabled_default' => 'disabled',
'page_default' => 'page'
)
3. In js/mage/adminhtml/product.js
change
this.getFileElement(file, 'cell-label input').value = image.label;
this.getFileElement(file, 'cell-position input').value = image.position;
this.getFileElement(file, 'cell-remove input').checked = (image.removed == 1);
this.getFileElement(file, 'cell-disable input').checked = (image.disabled == 1);
to
this.getFileElement(file, 'cell-label input').value = image.label;
this.getFileElement(file, 'cell-position input').value = image.position;
this.getFileElement(file, 'cell-remove input').checked = (image.removed == 1);
this.getFileElement(file, 'cell-disable input').checked = (image.disabled == 1);
this.getFileElement(file, 'cell-page input').checked = (image.page == 1);
change
this.images[index].label = this
.getFileElement(file, 'cell-label input').value;
this.images[index].position = this.getFileElement(file,
'cell-position input').value;
this.images[index].removed = (this.getFileElement(file,
'cell-remove input').checked ? 1 : 0);
this.images[index].disabled = (this.getFileElement(file,
'cell-disable input').checked ? 1 : 0);
to
this.images[index].label = this
.getFileElement(file, 'cell-label input').value;
this.images[index].position = this.getFileElement(file,
'cell-position input').value;
this.images[index].removed = (this.getFileElement(file,
'cell-remove input').checked ? 1 : 0);
this.images[index].page = (this.getFileElement(file,
'cell-page input').checked ? 1 : 0);
this.images[index].disabled = (this.getFileElement(file,
'cell-disable input').checked ? 1 : 0);
Simply use search text to find where to change the code. Hope this helped.

After much toil I discovered that in addition to the original post and the 2nd poster's recommendations, you also need to open up /app/code/core/Mage/Catalog/sql/catalog_setup/mysql4-upgrade-1.5.9.9-1.6.0.0.php, go to around line 2023 where it starts with this:
$installer->getTable('catalog/product_attribute_media_gallery_value') => array(
Change this:
'disabled' => array(
'type' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Is Disabled'
)
To this:
'disabled' => array(
'type' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Is Disabled'
),
'page' => array(
'type' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Page'
)
When Magento saves it checks this file to make sure the fields getting passed match the values in these arrays.

I got the error
Notice: Undefined index 'page'
in the class
Mage_Catalog_Model_Product_Attribute_Backend_Media
in the line 224.
I had to change
js/mage/adminhtml/product.js
newImage.position = this.getNextPosition();
to
newImage.position = this.getNextPosition();
newImage.page = 0;
It works great now.
Thanks.

I was working on something similar recently, and found that this piece of code was part of the solution:
$fieldset->addField('entire_range', 'checkbox', array(
'checked' => $this->getEntireRange()==1 ? 'true' : 'false',
'onclick' => 'this.value = this.checked ? 1 : 0;'
));
I couldn't get it to save the value to the database either. It's something to with the Varien_Data_Form_Element_Checkbox class.
Hope this helps, please post your solution if you find it!
Cheers,
JD

Related

Preview image in ACF gutenberg block

Is it possible to add an image to the gutenber block preview when using ACF to register the block ?
Here's the code to register the block:
acf_register_block(array(
'name' => 'bk-raisons',
'title' => __('Les raisons', 'diezel'),
'description' => __('Les raisons', 'diezel'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'spira-custom',
'icon' => 'align-wide',
'keywords' => array('bk-raisons'),
));
The preview appears when hovering the block.
Thank you !
I finally found a solution.
I don't know if you use Twig (Timber) or not.
If not check this : https://stackoverflow.com/a/67846162/6696150
For my part with Timber
When you declared your block add example attributes :
$img_quadruple = array(
'name' => 'img-quadruple',
'title' => __('Quatre images'),
'title_for_render' => 'img-quadruple',
'description' => __(''),
'render_callback' => 'ccn_acf_block_render_callback',
'category' => 'imgs',
'icon' => '',
'mode' => 'edit',
'keywords' => array( 'quatre images' ),
'example' => array(
'attributes' => array(
'mode' => 'preview',
'data' => array(
'preview_image_help' => get_template_directory_uri().'/assets/img/preview/preview_img_quadruple.jpg',
),
)
)
);
And when your declared your callback :
function ccn_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
$context = Timber::context();
// Store block values.
$context['block'] = $block;
// Store field values.
$context['fields'] = get_fields();
// back-end previews
if ( $is_preview && ! empty( $block['data'] ) ) {
echo '<img src="'. $block['data']['preview_image_help'] .'" style="width:100%; height:auto;">';
return;
} elseif ( $is_preview ) {
echo 'Other condition';
return;
}
// Store $is_preview value.
$context['is_preview'] = $is_preview;
// Render the block.
Timber::render('gutenberg/gut_' . strtolower($block['title_for_render']) . '.twig', $context );
}

Passing extra data to yii uploader

Am creating a file uploader using the kartik uploader bu the problem is that it doesn't pass extra data
The form(view) code
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'case_description')->textArea();?>
<?php echo $form->field($evidence, 'path')->widget(FileInput::classname(), [
'options' => ['accept' => '*','multiple' => true,'name'=>'images'],
'pluginOptions' => [
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
'showPreview' => true,
'showCaption' => true,
'showRemove' => true,
'showUpload' => true,
'browseLabel' => 'Insert Evidence',
'uploadUrl' => Url::to(['cases/upload']),
'maxFileCount' => 10,
'uploadExtraData' => [
'album_id' => "javascript: return $('#cases-case_description').val());",
],
],
]
);?>
The $evidence ,path is a table related to the models table
The controller that I have tested with (code)
public function actionUpload(){
$ann = empty($_POST['album_id']) ? '' : $_POST['album_id'];
var_dump($ann)
}
This returns null showing that the album_id is not passed to the controller and yet the $model->case_description is the field above the upload widget
The new controller
public function actionUpload(){
$images = $_FILES['evidence'];
$success = null;
$paths= ['uploads'];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
//$ext = explode('.', basename($filenames[$i]));
$target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
if(move_uploaded_file($images['name'], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
echo $success;
}
// check and process based on successful status
if ($success === true) {
$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
Due the problem with dinamic/variable assign to extraData i suggest a simple solution based on POST / submit method (eventually set the proper action in your form)
use kartik\widgets\FileInput
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'case_description')->textArea();?>
<?php echo $form->field($evidence, 'path')->widget(FileInput::classname(), [
'options' => ['accept' => '*','multiple' => true,'name'=>'images'],
'pluginOptions' => [
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
'showPreview' => true,
'showCaption' => true,
'showRemove' => true,
'showUpload' => true,
'browseLabel' => 'Insert Evidence',
'uploadUrl' => Url::to(['cases/upload']),
'maxFileCount' => 10,
],
]
);
echo Html::submitButton($model->isNewRecord ? 'Upload' : 'Update', [
'class'=>$model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
);
ActiveForm::end();
?>

output child terms of current custom taxonomy term with images

Just wondering if anyone can help with this - banging my head for days...
From a custom taxonomy page (taxonomy_genre.php), I need to output the child terms with images.
I'm using the 'Taxonomy Images' plugin to set the image.
Code below - thanks in advance if anyone can give me some pointers!
code #1 outputs all terms including parent terms.
code #2 outputs the correct terms but with no images
essentially I'm trying to amalgamate the two.
code #1:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'taxonomy' => $current_term->taxonomy,
'child_of' => $current_term->term_id,
'term_args' => array(
'orderby' => 'id',
'order' => 'ASC',
'hierarchical' => 0,
),
);
$cats = apply_filters( 'taxonomy-images-get-terms', '', $args );
foreach ($cats as $cat) {
echo '<li><a href="' . get_category_link($cat) . '" title="'. $cat->name .'">' ;
echo wp_get_attachment_image( $cat->image_id, 'detail' );
echo $cat->name ;
echo '</a></li>';
}
code #2
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$cats = wp_list_categories( array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => false,
'depth' => 2,
'title_li' => ''
));
foreach ((array)$cats as $cat) {
$catdesc = $cat->category_description;
echo '<li>'. wp_get_attachment_image( $cat->image_id, 'detail' ) . $cat->cat_name . '</li>'; }
Sorted - used:
'parent' => $current_term->term_id,
instead of:
child_of => $current_term->term_id,

Wordpress get image url (currently using wp_get_attachment_url)

Having a little trouble in getting image attachment urls in wordpress. This is my code so far:
<?php // find images attached to this post / page.
global $post;
$thumb_ID = get_post_thumbnail_id( $post->ID );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'exclude' => $thumb_ID,
'post_parent' => $post->ID
);
$images = get_posts($args);
?>
<?php // Loop through the images and show them
if($images)
{
foreach($images as $image)
{
echo wp_get_attachment_image_url($image->ID, $size='attached-image');
}
}
?>
Which returns nothing. If I swap out wp_get_attachment_image_url($image->ID, $size='attached-image'); for wp_get_attachment_image($image->ID, $size='attached-image'); this works fine, but brings in the image rather than just the url.
The following code will loop through all image attachments and output the src url. Note that there are two methods shown, depending on your need.
<?php
global $post;
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
// Method #1: Allows you to define the image size
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {echo $src[0];}
// Method #2: Would always return the "attached-image" size
echo $attachment->guid;
}
}
?>

Add custom options to dropdown via code in Magento

i had to add custom options automatically when a product is added , the code works fine but i need to create a drop down menu with options and i dont know how to add options to the drop down created ,
my code is
public function Add_CustomOptions_Automatically($observer) {
$product = $observer->getEvent()->getProduct();
$save = false; if (!$product->getOptions()) $save = true;
$optionData = array(
'previous_group' => 'text',
'title' => 'Size',
'type' => 'drop_down',
'is_require' => 0,
'sort_order' => 0,
'price' => 0,
'price_type' => 'fixed');
if($save):
$product->setHasOptions(1)->save();
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($optionData);
$option->save();
$product->addOption($option);
endif;
}
}
I've created 'type' => 'drop_down' but how can I add options? I have no idea how to add the options, and any help would be very much appreciated.
thanks,
You can supply an array of values to the option array, this will then be added
to the option. as below :-)
$product = Mage::getModel('catalog/product');
$product->load(200); // product id here
$opt = array(
'is_delete' => 0,
'is_require' => false,
'previous_group' => '',
'title' => 'New Example Option',
'type' => 'drop_down',
'price_type' => 'fixed',
'price' => '20.0000',
'sort_order' => 0,
/** array of values for this option **/
'values' => array(
array(
'is_delete' => 0,
'title' => 'Option One Here',
'price_type' => 'fixed',
'price' => 999,
'sku' => 'test-sku-here',
'option_type_id'=> -1,
),
array(
'is_delete' => 0,
'title' => 'Another Option',
'price_type' => 'fixed',
'price' => 999,
'sku' => 'another-sku-here',
'option_type_id'=> -1,
)),
);
$option = Mage::getModel('catalog/product_option')
->setProduct($product)
->addOption($opt)
->saveOptions();
public function Add_CustomOptions_Automatically($observer) {
$product = $observer->getEvent()->getProduct();
$optionData = array(
'previous_group' => 'text',
'title' => 'Size',
'type' => 'drop_down',
'is_require' => 0,
'sort_order' => 0,
'price' => 0,
'price_type' => 'fixed');
if(!(bool)$product->getOptions()):
$product->setHasOptions(true)->save();
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($optionData);
// Answer starts here
$value = Mage::getModel('catalog/product_option_value');
$value->setOption($option)
->setTitle('Hello world!');
$option->addValue($value);
// Answer ends here
$option->save();
$product->addOption($option);
endif;
}
As you can see you are adding a Mage_Catalog_Model_Product_Option_Value with a title and associating it with the $option you created. It can also have an SKU, price and sort order. Create as many values as you want in this way.
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "http:www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<pre>
<?php
$res = mysql_pconnect('localhost', 'your data base name', 'passward');
mysql_select_db('your database name');
mysql_query("SET NAMES 'utf8';", $res);
mysql_query("SET CHARACTER SET 'utf8';", $res);
$query = 'select entity_id from catalog_product_entity';
$res = mysql_query($query);
$products=array();
while ($ret = mysql_fetch_array($res)) {
$products[]=$ret[0];
$query = "UPDATE catalog_product_entity set has_options=1 where entity_id=$ret[0]";
//echo "$query<br>";
//$res1 = mysql_query($query);
}
echo "Set all products for has_options in catalog_product_entity.<br>";
//$res = mysql_query('DELETE from catalog_product_option');
//$res = mysql_query('DELETE from catalog_product_option_title');
//$res = mysql_query('DELETE from catalog_product_option_type_price');
//$res = mysql_query('DELETE from catalog_product_option_type_title');
//$res = mysql_query('DELETE from catalog_product_option_type_value');
echo "Deleted all rows from catalog_product_option* tables.<br>";
$ress=array();
foreach ($products as $product){
$query = "insert into catalog_product_option (product_id,type,is_require,image_size_x,image_size_y,sort_order) values ($product,'drop_down',0,0,0,0)";
echo "$query<br>";
$res1 = mysql_query($query);
$ress[] = array("option_id" => mysql_insert_id());
}
echo '<pre>';
print_r($ress);
echo "Populated catalog_product_option.<br>";
$option_titles=array('en_US'=> 'Warrenty' );// here change title of option titles #Optional Coating
$res = mysql_query('SELECT * FROM `core_config_data` WHERE path="general/locale/code"');
while ($ret = mysql_fetch_array($res)) {
$stores[$ret['value']][]=$ret['scope_id'];
}
$res = mysql_query('select * from catalog_product_option');// get all product here which agains data inserted
$sort_orders=array(
1,
2,
3,
4);
//while ($ret = mysql_fetch_array($res)) {
foreach ($ress as $key => $ret)
{
//echo '<br>'.$ret[$key]["option_id"];
foreach($stores as $locale=>$scopes){
foreach($scopes as $scope){
$query = "insert into catalog_product_option_title (option_id,store_id,title) values ($ret[option_id],$scope,'$option_titles[$locale]')";
echo "$query<br>";
$res1 = mysql_query($query);
}
}
foreach($sort_orders as $order){
$query = "insert into catalog_product_option_type_value (option_id,sort_order) values ($ret[option_id],$order)";
echo "$query<br>";
$res1 = mysql_query($query);
}
}
echo "Populated catalog_product_option_title.<br>";
echo "Populated catalog_product_option_type_value.<br>";
$prices=array(
0.00,//Standard (12 months)
29.95,//Silver (12 months +loan phone + pic up)
59.95,//Gold(12 months)
89.95//Platinum (24 months +loan phone + pic up)
);
$option_type_titles=array('en_US'=>array(
'Standard (12 months).',
'Silver (12 months +loan phone + pic up).',
'Gold(12 months).',
'Platinum (24 months +loan phone + pic up).'
)
);
$res = mysql_query('select * from catalog_product_option_type_value');
$i = 0;
$j = count($prices)-1;
while ($ret = mysql_fetch_array($res)) {
foreach($stores as $locale=>$scopes){
foreach($scopes as $scope){
$query = "insert into catalog_product_option_type_price (option_type_id,store_id,price,price_type) values ($ret[0],$scope,$prices[$i],'fixed')";
echo "$query<br>";
$res1 = mysql_query($query);
$query = "insert into catalog_product_option_type_title (option_type_id,store_id,title) values ($ret[0],$scope,'{$option_type_titles[$locale][$i]}')";
echo "$query<br>";
$res1 = mysql_query($query);
}
}
($j==$i) ? $i= 0 : $i++ ;
}
echo "<br>Populated catalog_product_option_type_price.<br>";
echo "<br>Populated catalog_product_option_type_title.<br>";
?>
</pre>
</body>
</html>
dear this code is work and i check it . but this code when excute it will add this custom option with all products in your shop. you can change sort ordar and price and option values and also dropdown name . i think its work better.
Try this, its working for me...
$eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,$attributeCode, [
'type' => 'varchar',
'label' => 'My Code',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array(
0 => 'January',
1 => 'February',
2 => 'March',
3 => 'April',
4 => 'May',
5 => 'June',
6 => 'July',
7 => 'August',
8 => 'September',
9 => 'October',
10 => 'November',
11 => 'December'
),
),
]);

Resources