Add attributeSet but based on an existing set - Magento - php

Ok, its possible to add a new attribute set in magento using something like the following:
$entitySetup = new Mage_Eav_Model_Entity_Setup;
$entitySetup->addAttributeSet('catalog_product', $setName);
But how can i base the set on an existing set like the default. This option is available in the admin section so its possible.

I did this 6 months ago, I do not have the code anymore, but I know you have to use the initFromSkeleton() method on your attribute set. You can search Magento's code for calls to this function, there are very few calls (just one perhaps). It will show you its usage.
EDIT: I remember I had the very same problem you are talking about, and I mailed about it. Here is the usage I was advised:
$attrSet = Mage::getModel('eav/entity_attribute_set');
$attrSet->setAttributeSetName('MyAttributeSet');
$attrSet->setEntityTypeId(4);//You can look into the db what '4' corresponds to, I think it is for products.
$attrSet->initFromSkeleton($attrSetId);
$attrSet->save();
The initialization is done before the save.

// create attribute set
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product')
->getTypeId(); // 4 - Default
$newSet = Mage::getModel('eav/entity_attribute_set');
$newSet->setEntityTypeId($entityTypeId);
$newSet->setAttributeSetName(self::ATTRIBUTE_SET_NAME);
$newSet->save();
$newSet->initFromSkeleton($entityTypeId);
$newSet->save();

This is what worked for me.
$i_duplicate_attribut_set_id = 10; // ID of Attribut-Set you want to duplicate
$object = new Mage_Catalog_Model_Product_Attribute_Set_Api();
$object->create('YOUR_ATTRIBUT_SET_NAME', $i_duplicate_attribut_set_id);
Alex

Here you can find useful information about working with attribute sets.

Here:
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product') // This can be any eav_entity_type code
->getTypeId();
$attrSet = Mage::getModel('eav/entity_attribute_set');
$attrSetCollection = $attrSet->getCollection();
$attrSetCollection
->addFieldToFilter('entity_type_id', array('eq' => $entityTypeId))
->addFieldToFilter('attribute_set_name', array('eq' => 'Default')); // This can be any attribute set you might want to clone
$defaultAttrSet = $attrSetCollection->getFirstItem();
$defaultAttrSetId = $defaultAttrSet->getAttributeSetId();
$attrSet->setAttributeSetName('Assinaturas'); // This is the new attribute set name
$attrSet->setEntityTypeId($entityTypeId);
$attrSet->initFromSkeleton($defaultAttrSetId);
$attrSet->save();

Related

Laravel Crud Editing makes a new record

I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
I want a check before it makes a new ExtraRegistration. that it only makes a new registration if the registration_id with that extra_id doesn't already exists. (Not 100% sure though).
Thanks in advance!
you make a new object of ExtraRegistration so its always make a new entry for update first get object of those id after that update
check the below link
https://laravel.com/docs/5.8/eloquent#updates
This happens because you're creating a new ExtraRegistration record:
$registrationextra = new ExtraRegistration();
If you want to update it, you need to find the related $registrationextra for your $options, and then update them (assuming you have relations set up):
$registrationextra = ExtraRegistration::where('options_id', $option->id);
$registrationextra->update([
'your_fields' => value
// etc...
]);
If you want to check if ExtraRegistration exists, and depending on that, create or update it, you can do something like this:
$registrationextra = App\ExtraRegistration::updateOrCreate(
['your_fields' => 'value'],
);
You can read more on official documentation.

setting persistent plugin parameters in Joomla 3

I'm developing a Joomla 3.x plugin, and want to be able to change the plugin parameter set in the plugin's manifest file programmatically. I believe I need to use a JRegistry object, but I'm not sure about the syntax.
Here's the issue:
// token A is set in plugin params as defined in plugin's XML manifest
var_dump($this->params->get('token')); // prints token "A" as expected
// do some stuff to get a fresh access token, called token "B"
$tokenB = $function_to_get_fresh_token();
// set the new token
if ($tokenB) $this->params->set('token', $tokenB);
var_dump($this->params->get('apptoken')); // prints token "B" as expected
the problem is that on subsequent page reloads, the token reverts to tokenA rather than what I assumed would be the stored value of tokenB.
How do I store the tokenB value in the plugin's parameters in the database?
This is a working example of how to change plugin params from within the plugin (J! 3.4):
// Load plugin called 'plugin_name'
$table = new JTableExtension(JFactory::getDbo());
$table->load(array('element' => 'plugin_name'));
// Params can be changed like this
$this->params->set('new_param', 'new value'); // if you are doing change from a plugin
$table->set('params', $this->params->toString());
// Save the change
$table->store();
Note: If new params are added by plugin dynamically and the plugin is saved afterwards, these new params gets deleted. So one way to deal with it is to add those params as hidden fields to plugin's config XML.
This is just an outline, but something along these lines
$extensionTable = new JtableExtension();
$pluginId = $extensionTable->find('element', 'my_plugin');
$pluginRow = $extensionTable->load($pluginId);
// Do the jregistry work that is needed
// do some stuff to get a fresh access token, called token "B"
$tokenB = $function_to_get_fresh_token();
// set the new token
if ($tokenB) $this->params->set('token', $tokenB);
// more stuff
$extensionTable->save($pluginRow);
I spent a lot of time googling and reading and found no real answer to this. Oddly enough this doesn't seem to have been provided for in Joomla. So here's what I ended up doing:
1) build a function to get your plugin ID, since it will change from one installation to another
private function getPlgId(){
// stupid hack since there doesn't seem to be another way to get plugin id
$db = JFactory::getDBO();
$sql = 'SELECT `extension_id` FROM `#__extensions` WHERE `element` = "my_plugin" AND `folder` = "my_plugin_folder"'; // check the #__extensions table if you don't know your element / folder
$db->setQuery($sql);
if( !($plg = $db->loadObject()) ){
return false;
} else {
return (int) $plg->extension_id;
}
}
2) use the plugin id to load the table object:
$extension = new JTableExtension($db);
$ext_id = $this->getPlgId();
// get the existing extension data
$extension->load($ext_id);
3) when you're ready to store the value, add it to the params, then store it:
$this->params->set('myvalue', $newvalue);
$extension->bind( array('params' => $this->params->toString()) );
// check and store
if (!$extension->check()) {
$this->setError($extension->getError());
return false;
}
if (!$extension->store()) {
$this->setError($extension->getError());
return false;
}
If anyone knows a better way to do this please let me know!

Drupal 7 Render a Comment Object

So this is the problem I am running into. If I have a comment object, I want to create a renderable array that is using the display settings of that comment. As of now this is what I have:
$commentNew = comment_load($var);
$reply[] = field_view_value('comment', $commentNew, 'comment_body', $commentNew->comment_body['und'][0]);
Which works fine because I dont have any specific settings setup for the body. But I also have image fields and video embed fields that I need to have rendered the way they are setup in the system. How would I go about doing that?
Drupal core does it with the comment_view() function:
$comment = comment_load($var);
$node = node_load($comment->nid);
$view_mode = 'full'; // Or whatever view mode is appropriate
$build = comment_view($comment, $node, $view_mode);
If you need to change a particular field from the default, use hook_comment_view():
function MYMODULE_comment_view($comment, $view_mode, $langcode) {
$comment->content['body'] = array('#markup' => 'something');
}
or just edit the $build array received from comment_view() as you need to if implementing the hook won't work for your use case.

Get product atribute value in magento

Hi i have got the maximum elements of products using below code but it doesn't show the size attribute of my product, the size attribute is visible on front-end but i cant understand why it is not printing with this code
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
//for order update
include '../../../../app/Mage.php';
Mage::app('default');
echo '<pre>';
if(isset($_REQUEST['productid'])){
$productId = $_REQUEST['productid'];
}else{
$productId = '12402'; // product ID 10 is an actual product, and used here for a test
}
$product = Mage::getModel('catalog/product')->load($productId); //load the product
//$product_id = $product->getId();
//$created_at = $product->getcreated_at();
//$description = $product->getdescription();
//$short_description = $product->getshort_description();
//$sku = $product->getsku();
//$size_fit = $product->getsize_fit();
//$style_ideas = $product->getstyle_ideas();
//$name = $product->getname();
//$price = $product->getprice();
//$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
If its the size_fit attribute (i'm guessing that because its the only size attempt in your code..) use $product->getSizeFit(). For just size use $product->getSize(). When this is not returning anything, please post the attribute installer if you have one. Mufadall his answer is also correct but judging your code you are just using wrong syntax.
Basicly according to the magic get method the first letter is turned into a capital and all other letters after an underscore.
Ex.: To fetch my_sample_attribute use getMySampleAttribute().
getData('my_sample_attribute') would also be an option but you shouldn't make a habbit of doing that because in some cases, for some attributes getData('attribute') returns a different value then getAttribute()....
$product->getData($attribute_code);
will return you the actual attribute value. For attribute with type dropdown it will return option id
$product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
will return actual value
You can use this:
$product->getData('Your Attribute ID');
Go to Size attribute and check for Drop Down used in product listing if it set to No then set it to Yes, after that you can get your size attribute with other product attribute
You can use the getters, or getData();
The getter is set in magento with the magic __get() method, and you can use it in the following way:
$product->getDescription() // ( to get description attribute)
$product->getShortDescription() // (to get short_description attribute)
So basically you explode the attribute with underscores, and capitalize the words, and you will get what you need to put after "get".
Here is something very useful I use all the time
Zend_Debug::dump($product->getData());
This gets you all the data you have to work with. If you are missing data, it means it's not loaded.
Good luck!

create attribute set in Magento setup script

Creating attributes and assigning them to existing attribute sets is a solved problem but we are running into an issue trying to create an attribute set and populate it with default and specific attributes is failing. This is the code in use:
$setup->addAttributeSet('catalog_product', 'women_sizing_denim');
$oAttributeSetModel = Mage::getModel("eav/entity_attribute_set")
->load($setup->getAttributeSetId('catalog_product', 'women_sizing_denim'))
->initFromSkeleton($setup->getAttributeSetId('catalog_product', 'default'))
->save();
I can verify by debugging through that the initfromSkeleton method does load the attributes from the default attribute_set as advertised, however after the save(), the new set is empty.
Adding new attributes to the set is possible, so it does exist and is created correctly, but the missing default attributes make it unusable since SKU, price, name, etc are all required.
I remember that the issue with creating attribute sets based on the default attribute set was, that you need to save the attribute set twice, once before calling initSkeleton() and once after it.
I don't remember the exact reason anymore, it's too long ago. Anyway, here's what worked for me:
// Mage_Eav_Model_Entity_Setup
$oEntitySetup = $this;
$oEntitySetup->startSetup();
$sNewSetName = 'myset';
$iCatalogProductEntityTypeId = (int) $oEntitySetup->getEntityTypeId('catalog_product');
$oAttributeset = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId($iCatalogProductEntityTypeId)
->setAttributeSetName($sNewSetName);
if ($oAttributeset->validate()) {
$oAttributeset
->save()
->initFromSkeleton($iCatalogProductEntityTypeId)
->save();
}
else {
die('Attributeset with name ' . $sNewSetName . ' already exists.');
}
$oEntitySetup->endSetup();
Please notice that setup class needs to extend
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
so that
$oEntitySetup->getEntityTypeId('catalog_product');
can be called.
I used Jürgen Thelen answer which worked.
But I found the new attribute set did not have default options and options group such as general and invoice etc.
So to get round this include $installer->getAttributeSetId('catalog_product', 'default') in the initFromSkeleton()
if($attributeSet->validate()) {
$attributeSet
->save()
->initFromSkeleton($installer->getAttributeSetId('catalog_product', 'default'))
->save();
} else {
die('Attributeset with name ' . $setName . ' already exists.');
}

Categories