symfony2 money field doesn't story comma into database - symfony

I used the field "money" in my product entity to store the price of a product in euros.
->add('pRetailPrize' ,'money', array(
'currency' => 'EUR',
'divisor' => 100,
))
It shows the price in euros (ex 1300,35 €) when you retrive or insert a price through the money entity field, but when you have to show the price using getRepository:
$entity = $em->getRepository('MyBundle:ProductInfo')->find($id);
And the you pass the variable $entity (entity.pRetailPrize) to a twig it doesn't show the comma before decimals. I then realized it didn't save the comma into the database. Is there a way to save the comma into database using the money field or to use the money field in a way it shows the comma when you retrieve a price from the database in euro currency system using getRepository as shown above? Also any solution or suggestion to do it differently is appreciated.

And it shouldn't. Comma separation is a output thing and, because of the localization's specifics, it should not be stored persistently.
In order to print it, you should use built-in number_format filter: http://twig.sensiolabs.org/doc/filters/number_format.html
Example:
{{ number|number_format(2, '.', ',') }}
Hope this helps...

There is only one thing: you set field mapping as integer instead of float in your entity.

Related

Select subfield returns empty string when looped by wc_get_related_products

On my single product page, I am trying to get the related products using woocommerce's built in wc_get_related_products() function, which returns an array of product IDs.
So I loop through the related product IDs and runs a get_field( 'workshop_details', $product_id ) inside it. The workshop_details field is a group field, which has the following subfields: workshop_type (select field), workshop_date (date field), and workshop_row (text field).
Problem is, the return value of workshop_type subfield is always an empty string, but the other subfields are returning fine. I tried to change the return value of the select field to value, label, and both, but nothing changes.
Another problem is, when I loop through all the products by just using get_posts(), and running the get_field( 'workshop_details', $product_id ), it returns the workshop_type properly.
But I can't just use get_posts() instead of wc_get_related_products() because it would ignore the upsells/custom related products on the product.
Closing this question now, apparently it was a data problem. 🤦🏻‍♀️

Woocommerce: Translate/change order item meta key before output

There's an action called 'woocommerce_checkout_create_order_line_item' where you can use the method 'add_meta_data(meta_key, meta_value)' to add meta data to the order item.
Every example did use this syntax: add_meta_data(__('Some key name', 'woocommerce'), $value);
This is perfect as long as the admin and the customer have set the same language and the field is only informal.
But I have to process the field programmatically later. So the key must always be the same, no matter which language is used.
So I use a simple string, but it won't be translated in the admin order area, on the Thank-You page, in the shopping cart etc. anymore.
How can I override the automatic output of the order item meta data? I've tried to hook i.e. into 'woocommerce_before_order_itemmeta($item_id, $item, $product)' (Admin order page), but there I can only delete $item->delete_meta_data('my_key'), I cannot add temporary meta keys => $item->add_meta_data(__('Translated key'), $value);
I also tried:
$item->set_meta_data(array('id' => 0, 'key' => __('Translated key'), 'value' => 'test123'));
print_r($item->get_meta_data());
Thanks!
There is no need to do the translation in the meta key.
What you could do is use the hook woocommerce_order_item_display_meta_key. There you can catch your key and output a tranlation for display.
Also you can filter keys to not display all custom meta keys if you have some keys that should not show up in the order details, but are for internal use only. This can be done with woocommerce_hidden_order_itemmeta
Here is a good post on working with order item meta data: https://www.ibenic.com/manage-order-item-meta-woocommerce/

How can i select extra fields and map them to entity via doctrine query builder in symfony?

I have two entities: Category and Product. Category entity has not link to Product (protected $products // ArrayColection). I need to calculate total amount of product prices inside each category, and i need to do it ONLY IN SINGLE request via query builder.
Can i do something like this:
$categories = $categoryRepository->createQueryBuilder('c')->join('AppBundle:Product', 'p')->addSelect('SUM(p.price) as c.totalPrice')->addGroupBy('c.id')->getQuery()->getResults();
And then map attribute totalPrice to entity, to have access to it somehow like this:
$categories->get(0)->totalAmount
I CAN'T build extra query, and i CAN'T add relationsip field to Category ($products) to calculte totalSum in entity method by adding price of each product. I should done it only with single query...
What you are talking about is possible with Doctrine > 2.4
Check out their docs for more details on joins: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html
Meanwhile - this is what should work for you:
$query = $this->createQueryBuilder('c')
->leftJoin('AppBundle:Product', 'p', 'WITH', 'p.category = c.id')
->select('SUM(p.price) as totalPrice')
->groupBy('c.id');
Note how we use the leftJoin method parameters - this is what allows you to run the query despite there is no relation between tables.
Hope that works for you

Symfony 2 custom DQL request inside an array field

I'm trying to make a custom request in my repository with a WHERE clause inside an array field. I tried something like that, not working, but can better show my problem :
$qb ->andWhere( "p.addresses[:index] = :address" )
->setParameter( "index" , $p_idLang )
->setParameter( "address" , $p_address );
Extracted from the documentation about array type:
Maps and converts array data based on PHP serialization. If you need
to store an exact representation of your array data, you should
consider using this type as it uses serialization to represent an
exact copy of your array as string in the database. Values retrieved
from the database are always converted to PHP’s array type using
deserialization or null if no data is present.
Your query doesn't make sense. You have a few options though:
Retrieve p.adresses and check using php if p.adresses[$index] = $address
Try something much less reliable but that could work:
$val_length = strlen($p_address);
$qb ->andWhere( "p.addresses LIKE :indexAddress" )
->setParameter( "indexAddress" , "%i:$p_idLang;s:$val_length:$p_address%" );
Create a new entity and a relation oneToMany between this entity and the new one.
I'd definetely try option 3. Option 1 isn't an option if the array is big or will become big in the future. I wouldn't go for option 2, but as an experiment could be worth trying.

drupal 7: how to access list-field's key instead of its label (field api)

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.

Resources