Forgive me if this answer is easily found in the Codex function reference. I have looked at the reference page for add_action( $hook, $function_to_add, $priority, $accepted_args ); but I have not been able to find the information I need.
What I have already:
I've created a custom field for my custom taxonomy of a custom post type. This custom field is added both to the "add new" and "edit" contexts (see image links):
http://screencast.com/t/CBmkyxrK
http://screencast.com/t/BQacL9AL3G
Yay, the creation part works!
What doesn't work:
Unfortunately, I'm only able to save media links that added using the "edit" context. Media links added using the "add new" quick create tool are not being saved.
Why it doesn't work
If I take a look at the example I followed to create my custom meta field (https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/) you'll see that Pippin does not include a save action for the "add new" part, he only adds the field using add_action( 'category_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
I know that what I am missing is the WordPress specific syntax for saving actions in the "add new" context, and I know that it will be similar to the save action which is used for the "edit" context:
// add custom field to 'edit' and 'add new' contexts
add_action( 'affiche_edit_form_fields', 'affiche_taxonomy_custom_fields', 10, 2 );
add_action( 'affiche_add_form_fields', 'affiche_taxonomy_custom_fields', 10, 2 );
// save field input
add_action( 'edited_affiche', 'save_affiche_taxonomy_custom_fields', 10, 2 );
Reviewing the last line of the above code block, what is the equivalent string to 'edited_[taxonomy_name]' for adding new (not editing existing) custom taxonomy entries?
For adding new you need to use the create_{$taxonomy} hook
add_action( 'create_affiche', 'save_affiche_taxonomy_custom_fields', 10, 2 );
Related
I'm trying to use the WordPress do_shortcode function for the WooCommerce One Page Checkout Plugin which uses shortcode like this: [woocommerce_one_page_checkout template="product-table" product_ids="product, ids, here"]. It seems like I can only use this shortcode IF it's in the content editor and won't allow me to add this to a page template using the do_shortcode function.
Their documentation here says:
If you wish to display the One Page Checkout Shortcode using WordPress’ do_shortcode() function instead of including the shortcode in a post or page’s content, you will also need to attach custom code to the 'is_wcopc_checkout' filter and make sure a boolean true value is returned.
So I tried adding the following to the functions.php file:
add_filter( 'is_wcopc_checkout', function(){ return true; } );
and it didn't seem to do the trick.
I also tried:
add_filter( 'is_wcopc_checkout', 'my_one_page_checkout' );
function my_one_page_checkout(){
return true;
}
add_filter( 'is_wcopc_checkout', 'true' );
That didn't seem to do it either.
Am I adding this code to the functions.php wrong? Any help on how I can get the One Page Checkout Plugin to work using do_shortcode?
Here's my full code in the page template for reference:
<?php
echo do_shortcode('[woocommerce_one_page_checkout template="product-table" product_ids="62, 122, 438, 52, 433, 435, 512, 514"]');
?>
Thanks for your help.
(I tried contacting WooCommerce support and they were no help saying that this is custom code and they can't do anything to help.)
The simplest way to return a true to a filter is like sitting the call back to WP default __return_true. So the function will be like
add_filter( 'is_wcopc_checkout', '__return_true' );
There is no filter named is_wcopc_checkout in the code of WooCommerce one page checkout version 1.0.2
From their doc- You can also manually add a shortcode [woocommerce_one_page_checkout] to any page or post and use the shortcode's attributes.
Usage: [woocommerce_one_page_checkout product_ids="30,45,12"]
Some context from One page checkout readme.
To register your template, attach a callback to the 'wcopc_templates' filter and add a new array of your template's details to the $templates array passed to your function.
For example, to register a custom pricing table template, the code would be similar to:
function eg_add_opc_template( $templates ) {
$templates['my-custom-pricing-table'] = array(
'label' => __( 'My Pricing Table', 'eg' ),
'description' => __( "Display a sophisticated and colourful pricing table with each product's attributes, but not weight or dimensions.", 'eg' ),
);
return $templates;
}
add_filter( 'wcopc_templates', 'eg_add_opc_template' ) );
The key used in the $templates array should be the template's file name (excluding the extension). The label element of the array is the name displayed on the One Page Checkout dialog. The description element is used for the tooltip next to the template's name.
I've added a 'Customize Now' button and few dropdowns after the 'add to cart' button using woocommerce_after_add_to_cart_button.
But now when I try to hide the 'add to cart' button (which I have to for a specific scenario my website need) using woocommerce_is_purchasable, the 'Customize Now' button and dropdowns are also hidden. Is there any proper order/sequence to do this?
Filter to add the Customize button and dropdowns:
add_action('woocommerce_after_add_to_cart_button', array($this, 'pn_get_calc_and_customize_btn'));
Filter to remove the add to cart button:
add_filter('woocommerce_is_purchasable', array($this, 'pn_hide_add_to_cart_button'), 10, 2);
As add-to-cart templates display condition is:
if ( ! $product->is_purchasable() ) {
return;
}
2 ways:
1) Use instead woocommerce_single_product_summary hook with a priority between 30 and 40:
add_action('woocommerce_single_product_summary', array($this, 'pn_get_calc_and_customize_btn'), 35 );
Then your function output code should be embedded in a custom <form> and you will need to add some more code to save the data in cart or elsewhere…
2) To remove cart button, use woocommerce_product_is_in_stock filter hook instead of woocommerce_is_purchasable so you will have to change a bit your hooked function code too...
add_filter('woocommerce_product_is_in_stock', array($this, 'pn_hide_add_to_cart_button'), 10, 2);
I have two suggestions here:
The first one will be to try to add the priority to your add_action() as well. As per documentation, the lower the number, the earlier the execution. I would try to add a greater priority to add_action() and try to force the woocommerce_after_add_to_cart_button to be executed after your filter. However, I don't know if removing the button also inhibits the filter (it might be).
Another suggestion I may have is to override the default template for the page (i don't know if you're editing the shop page or the single_product page) and have some if{}else{} login in there to show hide buttons based on the situation.
I don't know if either of these solutions is any good for you but this was just my tough and how I would tackle it.
Hope it helps in any way
I've added a custom field to my product for admin only meta data, I have hidden it using CSS.
However it still shows up in emails. Is there any way I can create a custom field where the meta data only shows up in the admin orders page?
You could try to use woocommerce_email_order_meta_fields filter hook to remove this custom field from order metadata, using unset() php function this way:
add_filter( 'woocommerce_email_order_meta_fields', 'wc_email_order_meta_remove_custom_field', 10, 3 );
function wc_email_order_meta_remove_custom_field( $fields, $sent_to_admin, $order ) {
// Replace HERE 'meta_key' by your custom field meta key or slug.
unset($fields['meta_key']);
return $fields;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work, but not sure as you don't provide any information and code related to the way you have set this custom field.
First,I know wordpress won't allow you to use template for posts (WooCommerce's product page is built via post). So I look for the Template Hierarchy.
It says there:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post.
For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.
So I created a template and name it single-product-ccc (ccc is one of my product's slug name), but nothing happened, nothing was affected.
But by creating a template named single-product will affect all of the product pages.
Why is that happening?
I don't get it. Even a single-2313.php (2313 is one post's id) will overwrite the default single.php for that 2313 post.
Why single-product-slug is not working in the same way?
Thanks.
Let me first correct you at one point. You say: "WooCommerce's product page is built via post".
This is not correct. WooCommerce creates a new post type 'product'. You can see all custom posts types that WooCommerce creates here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/
The 'product' post type uses the template single-product.php.
As you say, WordPress Template Hierarchy Docs say that you can create a template for a particular product:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
WooCommerce is a WordPress plugin and as so, it doesn't always follow WordPress rules strictly, and this is one of those cases. But you can use a filter to correct this very easily. In your theme's functions.php simply add:
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
if ( is_singular('product') && (get_the_ID()==30)) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-30.php';
}
return $template;
}
where get_the_ID() is the id of your Product. Now, you can create a new template as for example single-product-30.php
Try adding this in functions.php
function your_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'your_theme_add_woocommerce_support' );
and now onwards, you can use woocommerce/single-product.php for customisation
As I created custom post type with taxonomy, when I add a new custom field, it is saving in the database but not showing in custom fields. I don't understand how to show the custom field in admin panel.
There is a very good plugin called Advanced Custom Fields for Wordpress. It's very easy to use and features conditional logic for page layouts, post type and much more.
We can easily create a meta box without using any plugin and customize it as per our needs:
here is the Wordpress documentation to create a meta box and here is the example to easily implement it with custom post type. Here is the example:
<?php
add_action('add_meta_boxes', 'meta_box_add_function');;
function meta_box_add_function()
{
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
'post' // Post type ['post', 'custom_post_type']
);
// You can add multiple boxes like above
}
function wporg_custom_box_html($post){
echo 'What you put here, show\'s up in the meta box';
}
?>
And here you can save the post data using below hook:
<?php add_action( 'save_post', 'meta_box_save_function' ); ?>