I'm using WordPress, specifically WooCommerce, creating a new plugin to allow the user to store multiple shipping addresses. I'm currently storing these new shipping addresses as meta data in the user meta data table as a serialized array. I need a way to store these with some sort of ID.
What's the best way to do this -- give each a key ID with a unique number? Try to increment the last highest ID? Not sure what to do.
Why not just keep the serialized array but extend it to multidimensional?
array(
0 => array(city => 'Dallas', state => 'TX'),
1 => array(city => 'Madison', state => 'WI')
);
Related
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/
On a site where I started with WooCommerce version 2.. and then upgraded through newer releases (3.0.0, 3.0.1, ... 3.0.4), I'm noticing an inconsistency in how the order item meta data is stored for product attributes.
When using an attribute "Class Date" for variations, when an order is placed for some products in the store, the meta data for Class Date is stored using the key "pa_class-date". But for other products, the data for that same attribute is stored using the key "class-date".
My code for fetching attribute data looks like this:
$items = $order->get_items();
if ( isset($item['item_meta']['class-date']) ) {
$class_date = $item['item_meta']['class-date'];
} elseif ( isset($item['item_meta']['pa_class-date']) ) {
$class_date = $item['item_meta']['pa_class-date'];
} else {
error_log("Missing class date. We tried pa_class-date and class-date, but both are empty");
}
I would like to be able to simplify this to just look in one key or the other (pa_class-date or class-date, not both). Is that possible?
Here is what a dump of the order meta data looks like for the purchase of two different products. This one uses pa_class-date.
[467] => stdClass Object
(
[key] => pa_class-date
[value] => 2017-09-01
[display_key] => Class Date
[display_value] => <p>2017-09-01</p>
)
But for a different product, the data stored using the key "class-date":
26-Apr-2017 16:26:06 UTC] Array
(
[480] => stdClass Object
(
[key] => class-date
[value] => 2017-05-18
[display_key] => Class Date
[display_value] => <p>2017-05-18</p>
)
What causes the inconsistency in which key is used to store the data?
What is the most reliable (futureproof) way to retrieve the data?
This inconsistency is possible. But its inconsistent data entry rather. When you add attribute to Products >> Attributes, they become part of woocommerce system. Then you can map them to variation by just selecting on product details page.
However, when you add "Custom Attribute" directly from product details page, that data will be specific for that product and can cause inconsistency.
I am doing some customization for catching gravity form results on form submission. I get feed['meta'] array on form submission. But some fields have merge tag values like '{form_title}'. I need to get real value of the field instead of tokens. Following is the array I get on form submission.
Array
(
[name] => contact_test
[form_id] => 1
[contact_count] => 2
[type] => Contacts
[signature_text_2] => {Name (Prefix):2.2}{Name (Suffix):2.8}
[signature_html_2] => {Name (Suffix):2.8}{Name (Prefix):2.2}
)
I am having issues with last 2 fields where merge tags tokens are present. I need to fetch value of the corresponding fields.
GFCommon::replace_variables() worked for me. Just need to pass the token like {Name (Prefix):2.2}, form object, lead object. Remaining all are format specific options. The function is in gravityforms common.php file.
I use Prestashop 1.6.2 and I have a problem trying to add this function.
I am a little bit new trying to mod prestashop, the thing is that I have some products that can be bought only for professionals. Everytime a user registers it's assigned to a user group (professional and no-professional).
I know I can hide the categories for specific user groups, but this method is not perfect, due to if they know the name of the product or search it, they can still access the product page and buy it.
Is there any smarty variable to edit the product.tpl so it displays the button with the conditions above? Or a module or another way to do this?
I don't know exactly is there is a variable already set in smarty or not, but your can define your customer variable with object of customer:
Find controllers/front/ProductController.php file, find method assignPriceAndTax and in the end of it you will find something similar to this:
$this->context->smarty->assign(array(
'quantity_discounts' => $this->formatQuantityDiscounts($quantity_discounts, $product_price, (float)$tax, $ecotax_tax_amount),
'ecotax_tax_inc' => $ecotax_tax_amount,
'ecotax_tax_exc' => Tools::ps_round($this->product->ecotax, 2),
'ecotaxTax_rate' => $ecotax_rate,
'productPriceWithoutEcoTax' => (float)$product_price_without_eco_tax,
'group_reduction' => $group_reduction,
'no_tax' => Tax::excludeTaxeOption() || !$this->product->getTaxesRate($address),
'ecotax' => (!count($this->errors) && $this->product->ecotax > 0 ? Tools::convertPrice((float)$this->product->ecotax) : 0),
'tax_enabled' => Configuration::get('PS_TAX') && !Configuration::get('AEUC_LABEL_TAX_INC_EXC'),
'customer_group_without_tax' => Group::getPriceDisplayMethod($this->context->customer->id_default_group),
));
Add to this array
'customer_object' => $this->context->customer,
This variable you can use on the product page.
You can use the same way to close those buttons for all 'buy-windows' on your site.
I'm able to create a field, using
$form['title'] = array( '#type' => 'text', '#value' => 'Drupal');
1) But I want to be able to get/set the value from the database. How would I do it?
2 )P.S. what's the point of having a '#" in front of type and value ?
First of all I don't know what Drupal version are you using.
Secondly try to get some more info from the my answer:
1) Use Database API in order to get values from database.
2) Read more about Form API.
Hope this helps.