I am integrating a WooCommerce website into an application I've built.
I am attempting to retrieve custom fields for products (meta data) using the API.
The below is an excerpt from the docs regarding changes from v1 to v2
v1 does not include order item meta, v2 includes full order item meta (with an optional filter parameter to include protected order item meta)
https://woocommerce.github.io/woocommerce-rest-api-docs/v2.html#version
I cannot seem to find anywhere what this actual filter is. The filter below is what's used to get meta data initially
filter[meta]=true
But through my searching, I cannot find the additional filter to return protected meta data for products. Note I am not trying to update protected meta, but just view protected meta.
I found a workaround for now. I did not find a filter for protected fields in the API call however I added the following code to my functions.php file
add_filter( 'is_protected_meta', function ( $protected, $key, $type ) {
if ( $key === '_my_protected_meta_field' ) {
// Expose the `_my_protected_meta_field` meta value publicly
return false;
}
return $protected;
}, 10, 3 );
The meta data for _my_protected_meta_field now shows in the API call with filter[meta]=true
Related
I build a webshop with a couple Custom fields, these custom fields are in my order emails now but I cant seem to find out how to add a label for this. Currently it shows my meta key name or my id but I want to change it like the label on my custom fields!
The code I used in Functions.php:
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys',
'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'additional_wooccm1';
return $keys;
}
Let me know if you can help me!
[My order email](https://i.stack.imgur.com/ko9vQ.png)
My custom fields
I tried multiple ways like with Snippets into the Function.php but also copied the customer-processing-order.php and tried editing this file without any result.
Is there a hook I can use when I make a change to someone's order via the admin (such as their address, or a custom meta field)? I read this question but unfortunately woocommerce_process_shop_order_meta is fired before the order is saved, meaning I have no access to the newly updated data. What I need is to be able to use the new data that is saved to the order.
UPDATE: An issue with using save_post_shop_order is that the meta is updated before this is hit, so I can't compare the previously saved meta value, for example:
$metaArray = $_POST['meta'];
foreach($metaArray as $meta => $key) {
$metaArr[$key["key"]] = $key["value"];
}
$meta = get_post_meta($order->ID);
if($meta['coverstart'][0] != $metaArr['coverstart']) {
die("COVER START DATE HAS CHANGED");
}
The die() is never hit, because the script always gets the newly saved value.
Sorry but woocommerce_checkout_update_order_meta is fired after the order is saved… See this extract source code located in WC_Checkout create_order() method:
// Save the order.
$order_id = $order->save(); // <== Order is saved here before
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data ); <== // The hook
return $order_id;
So in woocommerce_checkout_update_order_meta you can get the saved order data:
by retrieving the WC_Order object from the $order_id argument and using all methods on it.
or using get_post_meta() on with the $order_id argument to get the data saved in wp_postmeta database table.
Then you can update the data with update_post_meta() function…
You can even use woocommerce_checkout_create_order before the data is saved…
You will be able to get the data from the $order argument using all available methods for the WC_Order class (CRUD getters methods).
You will be able to alter this data and saving it using the CRUD setters methods…
Some examples in stackOverFlow
If you need to do that after the order process the hooks to be used can be:
woocommerce_new_order (on newly created order event)
woocommerce_thankyou (on order received page)
woocommerce_order_status_changed (on order status changing event)
And may be some others…
To alter the data when order is saved in backend, you will use save_post_shop_order that has 3 arguments: $post_id, $post and $update…
I am developing my own plugin and I am trying to figure out how to add WordPress List Tables to my admin page. I am aware I can write the the HTML myself but if there is an API method this is the most desirable.
An example are Meta Postboxes; I can easily write my own markup and have the postbox collapse and show but its much better to use the API function add_meta_box() and do_meta_boxes().
My searching hasn't come up with any functions that create lists/tables like the ones below. What are the API functions that allow me to register and show WordPress List Tables?
To add Bulk actions
Bulk action are implemented by overwriting the method get_bulk_actions() and returning an associated array:
function get_bulk_actions() {
$actions = array(
'delete' => 'Delete'
);
return $actions;
}
I have write a blog on how to create WP_List_Table.
You can check out ** WP_List_Table - a step by step guide**
You can check this link for more information : http://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/
Using this tutorial on this page Adding new tabs and fields to Prestashop products’ back office, I was able to add a new field for author for products in my Prestashop. I was also able to display it on product page by adding Product.php to override/classes/ and inserting this code below:
class Product extends ProductCore
{
/** #var string Custom Product Field */
public $custom_field;
}
I then added {$product->custom_field} to product.tpl to display the new field. My challenge is that the same code does not work when added to product-list.tpl and the homefeatured.tpl module files.
Can any one explain how to achieve this? I am not an expert but I can find my way around tutorials if I have one. Thanks!
Find the function used by the module Homefeatured to get the products and edit the SQL request in this function to add your new field.
You can't display your new propertie because the SQL request don't get it.
Use . instead of ->
In product-list.tpl & homefeatured.tpl $procuct is array not object.
And do not miss the getFields() method in Product class:
public function getFields()
{
$fields = parent::getFields();
$fields['custom_field'] = $this->custom_field;
return $fields;
}
I have a custom multi select attribute which I'd like to take part in filtering of products. The attribute is set as used in Layered Navigation however doesn't appear in the list of available filters. Could be due to custom model implementation?
Anyone have some tips where to check why it doesn't appear? Attribute is set for several products
Magento version used is EE 1.11
Thanks
For those who will struggle with this in the future: the problem is in Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source file on line 191. By default multi select attribute values are being pulled from eav_attribute_option and if your custom attribute uses custom source model the attribute will not be indexed.
I don't know as of yet if it's intended but I couldn't find a better solution than overriding that model in local pull and adding required values in $options array.
Hope this helps someone, someday
What is the backend_type. i.e. are the values stored in the catalog_product_entity_varchar or catalog_product_entity_text table?
The backend_type has to match the checks in Mage_Catalog_Model_Resource_Eav_Attribute::isIndexable(), so text wouldn't work without rewriting the attribute model.
Is the is_filterable and/or is_filterable_in_search attribute property set?
The Mage_Catalog_Model_Product_Indexer_Eav::_registerCatalogAttributeSaveEvent() checks for those when updating the index for the layered navigation.
Are the methods getFlatColums(), getFlatIndexes() and getFlatUpdateSelect() implemented in the custom source model?
This actually is only required for building and updating the flat catalog product tables (so the used_in_product_listing or is_filterable property needs to be set in order for Magento to pick up the attribute).
Check the class Mage_Eav_Model_Entity_Attribute_Source_Table as a reference on what these there methods are supposed to return.
NOTE: I'm adding this in a new answer to use the code format.
How it was said, the problem is with multiselect attributes using a custom source model.
Solution:
Rewrite the class
Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source
Override the method:
_prepareMultiselectIndex
add this code after the $options array is filled with the default code (check line 200 in original file)
foreach($attrIds as $attId){
if( ! isset($options[$attId])){
$options[$attId] = $this->_getOptionsFromSourceModel($attId);
}
}
add this method too:
protected function _getOptionsFromSourceModel($attId)
{
$options = array();
/** #var Mage_Eav_Model_Entity_Attribute_Abstract $attribute */
$attribute = Mage::getResourceSingleton('catalog/product')->getAttribute($attId);
/** #var Mage_Eav_Model_Entity_Attribute_Source_Abstract $source */
$source = $attribute->getSource();
$sourceOptions = $source->getAllOptions();
if($sourceOptions){
foreach($sourceOptions as $sourceOption){
if(isset($sourceOption['value'])){
$options[$sourceOption['value']] = true;
}
}
}
return $options;
}
I couldn't find a less intrusive way to fix this.