I want to create a new variant group in Akeneo with some fixed attributes to "simulate" the behaviour of a product family. Unfortunately I don't know how to get a ProductTemplate with some attributes with empty values.
When I tried my code below, I get an error after opening the created variant group in the Akeneo GUI:
Error: Option "attributes" must only contains instances of "Pim\Bundle\CatalogBundle\Entity\Attribute", got "Pim\Bundle\EnrichBundle\Form\Type\AvailableAttributesType"
My code looks like this:
$groupType = $this->groupManager
->getGroupTypeRepository()
->findOneBy(['code' => 'VARIANT']);
$group = $this->groupFactory->createGroup($groupType);
$group->setCode('MY_VARIANT_GROUP');
$attributes = array($this->attributeRepository->findOneByIdentifier('AXIS_ATTRIBUTE'));
$group->setAxisAttributes($attributes);
// ??? How can I create a new product value?
$productValue1 = new ProductValue();
$productValue1->setId('PREDEFINED_ATTRIBUTE1');
$productValue1->setAttribute($this->attributeRepository->findOneByIdentifier('PREDEFINED_ATTRIBUTE1'));
$productTemplate = new ProductTemplate();
$productTemplate->setValuesData(array($productValue1));
$group->setProductTemplate($productTemplate);
$this->groupSaver->save($group);
I advise you to use the Pim\Bundle\CatalogBundle\Builder\ProductTemplateBuilder to create and add attributes to your product template.
This will ensure that the product template will be properly created with empty product values.
Related
I'm using wp_insert_post_data filter hook to save some extra data which I get from a meta box in a custom post type. I want to save these extra data as the post_content not the post_meta_data.
There is a select box with Select2 which is possible to select more than one values (pillbox). Finally Select2 prepares an array of values. I've checked browser Network section and the value is an array but when I try to access the value in wp_insert_post_data, I just get the last item of the array as an string and not an array!
Here is my function :
function xxx_save_country_data(array $data): array
{
$countries = $_POST['xxx_countries_field']; //It should be an array but it is only string
return $data;
}
add_filter('wp_insert_post_data', 'xxx_save_country_data');
Posted data screenshot: https://i.stack.imgur.com/0kX97.png
In my plugin case, I'm using woocommerce_form_field to generate the select element and if you don't specify id parameter as the $args in this function, both id and name properties get the same string and so it becomes xxx_countries_field and not xxx_countries_field[] which was the problem!
I created a custom field group named "Agenda 2" using Advanced Custom Field plugin in Wordpress. Under the group, I created a Post Object field named Panelist Chair. I can't seem to pull the data in the sub field named "Panelist Chair". But I can able to pull the data from another Post Object field I created outside the group.
$thegroupfield = get_field('agenda_2');
print_r($thegroupfield); //this returns all the arrays which is fine
$pchair = get_field('panelist_chair');
print_r($pchair); //this doesn't return any value
$pchair = get_sub_field('panelist_chair');
print_r($pchair); //this doesn't return any value too
$mod = get_field('moderator'); //another post object field outside the group
print_r($mod); //this returns array which is fine
How can I get to show the data of the post object field in the group?
Have you tried the following? The field panelist_chair is part of your agenda_2 field. You can t acces it with a get_field function because it is inside the array of agenda_2.
$thegroupfield = get_field('agenda_2');
print_r($thegroupfield['panelist_chair'])
Context
We've created (I actually inherited code) few forms with Gravity forms. One of the form is used to manage profil informations.
Description
The form doesn't update entries but instead creates new entries. Therefore the result is that in the profil page we have the initial information if we modify and save, after reload nothing changes. But, in the database, a new entry is created.
Tried solution
Setting a field to be unique doesn't fix it but returns an error saying that the value used in the field has already been used.
Try updating the entry ID before the entry is saved. This example from the gform_entry_id_pre_save_lead documentation will help:
add_filter( 'gform_entry_id_pre_save_lead', 'my_update_entry_on_form_submission', 10, 2 );
function my_update_entry_on_form_submission( $entry_id, $form ) {
$update_entry_id = rgpost( 'my_update_entry_id' );
return $update_entry_id ? $update_entry_id : $entry_id;
}
I'm working on a custom theme and googled the whole day but can't find an answer to my question:
how to print out a cck list field's key instead of its label?
I think it's the right way to access fields via the field api, right?
so I try
$output = field_view_field('node', $node, 'field_list');
print render($output);
that seems to be the way to get the label value of the key|label pair. but even if i set the display options format to 'key' - it only prints the label value. what am I doing wrong?
I know there are other options to render the key but how is it possible using field api?
You can get the field value itself (which will be the allowed values array key) with field_get_items():
$items = field_get_items('node', $node, 'field_list');
$key = $items[0]['value'];
If you need to match those up again at any point you can get the full key/value list from the field metadata:
$info = field_info_field('field_list');
$values = $info['settings']['allowed_values'];
$label = $values[$key];
Here's a similar way. This example works for a Country list with ISO codes.
The idea is to render the Name of the Country that was selected because the dump returns only the key ( iso code in this case)
Format of the select list on the backend:
AR|Argentina
US|United States
Assuming you have selected United States on the backend and you want to print the country name on the node template:
$country_field = field_info_field('field_country_iso');
$country_iso = $node->field_country_iso[LANGUAGE_NONE][0]['value'];
$country_name = $country_field['settings']['allowed_values'][$country_iso];
then
print $country_name; // This will return United States, not US code
I hope this helps.
if i have two product collections is there a way to merge them into one?
for example (my final intent isn't to actually just get a collection of 2 cats, this is just to illustrate the issue):
$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection();
$collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection();
$merged_collection = merge_collections($collection1,$collection2);
any help would be appreciated!
Assuming the items you wish to group together are of the same type and exist in the database then you can do this:
$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection();
$collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection();
$merged_ids = array_merge($collection1->getAllIds(), $collection2->getAllIds());
// can sometimes use "getLoadedIds()" as well
$merged_collection = Mage::getResourceModel('catalog/product_collection')
->addFieldToFilter('entity_id', array('in' => $merged_ids))
->addAttributeToSelect('*');
Here I know to filter by entity_id because that is products' key field, like it is for most entity types, some flat tables have a different primary key. Often you can generalise that with one of the collection's getIdFieldName() method. Products are a bad example in this case because it's ID field name isn't filled out correctly.
Almost every (or every?) collection in Magento inherits from a Varien Data Collection. A collection is a special object that holds objects of another type. There's no method for merging collections, but you can add additional items of the appropriate type to the collection.
Code like this should get you where you want to go, although there's probably more efficient ways to loop and do the actual merging.
$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection();
$collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection();
//load an empty collection (filter-less collections will auto-lazy-load everything)
$merged = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id',-1);
//add items from the first collection
foreach($collection1 as $item)
{
$merged->addItem($item);
}
//add items from the second collection
foreach($collection2 as $item)
{
//magento won't let you add two of the same thing to a collection
//so make sure the item doesn't already exist
if(!$merged->getItemById($item->getId()))
{
$merged->addItem($item);
}
}
//lets make sure we got something
foreach($merged as $product)
{
var_dump($product->getName());
}
I don't think there is such a method, but you can probably do something like that :
foreach ($collection1 as $item){
$collection2->addElem($item);
}
you can filter your collection directly without using 2.
$products = Mage::getModel('catalog/product');
$_collection = $products->getCollection();
->addCategoryFilter(2)
->load();
or try using 'addItem' to add your results to a collection. See also in Magento wiki
for the collection of products of several categories, you can use the following code
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku');
$collection->getSelect()
->join(
'catalog_category_product',
'product_id=entity_id',
array('category_id')
)
->where('catalog_category_product.category_id IN (?)', $categories);