WordPress WooCommerce Variation Dropdown Html Override - php

Basically, I want to convert my variation dropdown into something fancy, like for example, make it a radio button.
But this filter is not working. As a sample, I'm using it like this.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'override_color_variation_display', 10, 2 );
public function override_color_variation_display( $html, $args ) {
$html = 'Some override';
return $html;
}
Is this the correct way to get this working? Because it's not showing the "Some override" text anywhere on the product display.

Not sure which WooCommerce version are you using but in 2.5.1 this filter accepts only one variable. Try this code:
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'override_color_variation_display');
function override_color_variation_display( $html ) {
$html = 'Some override';
return $html;
}

Related

Wordpress how to get "label" of custom fields

I have this code to get the value for custom field myCustomField which works fine:
get_post_meta( get_the_ID(), 'myCustomField', true )
This code gets the value stored in this field.
But now I need a code to dynamically get and echo the Custom field label (not the stored value) which is "My Custom Field".
This code will be fired for this action:
add_action('woocommerce_product_meta_start' , 'add_in_product_page');
If you've created your custom field using ACF plugin, then what you need is the field object. You could call get_field_object function in order to get the object and then find the label returned in the object, like so:
$your_field_name = "your_custome_field_name";
$your_field_object = get_field_object($your_field_name); // You could also pass the field key instead of field name
echo $your_field_object['label'];
echo "<br>";
echo $your_field_object['value'];
echo "<br>";
echo $your_field_object['key'];
echo "<br>";
echo $your_field_object['type'];
echo "<br>";
echo $your_field_object['ID'];
You could also read more about this function on the documentation page:
ACF get_field_object function
UPDATE
Translating the returned label to your local language!
I would usually use these two filter hooks: gettext and ngettext.
You could read more about these on the documentation page:
WordPress gettext filter hook
WordPress ngettext filter hook
So the translation would be something like this:
$awesome_label = $your_field_object['label'];
add_filter('ngettext', 'your_theme_custom_translation', 20, 3);
add_filter( 'gettext', 'your_theme_custom_translation', 20, 3 );
function your_theme_custom_translation( $translated, $text, $domain ) {
if ( ! is_admin() ) {
if ( $awesome_label == $translated ){
$translated = 'etiqueta impresionante'; // This is the place where you could type anything you want in your local language to replace the default english label
}
}
return $translated;
}

WooCommerce - Add content on the thank you page by the shipping method

I would like to place my own content (in my case a shortcode) on the thank you page at WooCommerce after completing the order, which depends on the shipping method.
So for example:
Shipping Method 1: Content 1
Shipping Method 2: Content 2
Shipping Method 3: No content
I have already found something for text here, but i dont get the shortcode inserted. Alternatively I have tested what works with the shortcode here, where the dependency on the shipping method is missing.
Maybe someone can help me to implement this.
Ideally the content should be above the table with the order information.
Thanks a lot!
May be the way you added the shortcode to the thank you text can be the issue.
See the following shortcode function called avengers which i created for demo.
function call_avangers( $atts ){
return "<p>Avengers . . . . Assemble !!!</p>";
}
add_shortcode( 'avengers', 'call_avangers' );
You can display this shortcode in thank you page using the below function
add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $text, $order ) {
if( $order->get_shipping_method() == 'your_shipping_method_here' ) {
$text .= do_shortcode('[avengers]');
}
return $text;
}
The basic thing is you have to call do_shortcode('your_shortcode_name')

why wp_title filter is not working at all?

I know there are other questions like this but didn't find a reliable answer. So:
First activate the thing (simplyfied code):
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
Second, delete title tag from header.php.
Third, on page templates, before calling get_header(), add something like this:
add_filter('wp_title', 'set_custom_title', 10, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
Well, this is not working at all, in any template, being a page, an archive, a custom taxonomy or post type archive. No nothing. Wordpress is generating titles by itself.
Why? Am I doing something wrong? Note that this code once upon a time just worked: used in other sites/themes.
Is it maybe an issue of wp5.2.0?
So, thanks to #Vel, the answer is to re-add the title tag (even if in previous wp versions > don't know til what version you had to delete it form head instead).
Current working code for me:
//functions.php
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
//header.php
<title><?php wp_title('|', true, 'right'); ?> | <?php echo get_bloginfo('name') ?></title>
//page templates
$window_title = // do something
add_filter('wp_title', function($title, $sep, $seplocation) use($window_title){ return $window_title; }, 10, 3);
Try to use follows code -
add_filter('document_title_parts', function($titles){
return array('title' => 'Custom Title');
});
For anyone still having this issue of the wp_title filter not working, I'd suggest adding a higher priority value. The higher priority value will ensure that your filter is executed and not overriden by other filters in your theme or plugins installed. Please see below: (ref: https://developer.wordpress.org/reference/functions/add_filter/)
// the 9999999 priority value will force this filter to be executed closer to the end. A lower number corresponds with earlier execution
add_filter('wp_title', 'set_custom_title', 9999999, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
In my case Yoast SEO was changing the way title was rendered and only the following worked:
function filter_lp_title($title) {
return 'New title';
}
add_filter( 'pre_get_document_title', 'filter_lp_title', 25 );

Customise Woocommerce cart widget

I am trying to customise the woocommerce cart widget and make it more "visual" like the below example. I have researched and it seems I may be able to override the woocommerce cart widget with my own behaviour through the hook woocommerce_mini_cart().
End result I would like to get to:
Is it possible to modify the core functionality of the cart widget via this approach to achieve something like this, through the functions.php file or would I need CSS aswell?
if ( ! function_exists( ‘woocommerce_mini_cart’ ) ) {
function woocommerce_mini_cart( $args = array() ) {
$defaults = array( ‘list_class’ => ” );
$args = wp_parse_args( $args, $defaults );
woocommerce_get_template( ‘cart/mini-cart.php’, $args );
}
//*MODIFY HERE?*
}
Alternatively does anyone know of a woocommerce plugin which could solve this?
Despite of the question is not actual for its` author already,
I wanna to share some code to modify the default woocommerce_mini_cart() output:
1) The most difficult step - disable ECHO for woocommerce_mini_cart() function
2) change output HTML as you wish;
3) echo formatted HTML.
1)
function disable_echo_for_woocommerce_mini_cart() {
$mini_cart_html = woocommerce_mini_cart();
return $mini_cart_html;
}
2)
$mini_cart_html = disable_echo_for_woocommerce_mini_cart();
$mini_cart_html = str_replace('some_code', 'some_code_2', $mini_cart_html); // change output HTML.
$mini_cart_html = ob_get_clean();
3)
echo $mini_cart_html;

How to remove Text area of comment in Wordpress?

Hellow there all I tried searching everywhere but I coudn't just find it - may be because its a wiered requirement all I want to do is remove my Comment Text area in wordpress comment - I sucessfully removed URL website and name in the comment field by using following code
<?php
function remove_comment_fields($fields) {
unset($fields['url']);
unset($fields['author']);
unset($fields['email']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
?>
But not able to remove the Text area - Actually you all will thought what will I do removing all this I am using a plugin which allow you to post images in your comment and the only option I want to give to user is post images via comment. please guide me.
Two options, the comment_form_defaults filter:
add_filter( 'comment_form_defaults', 'so16856397_comment_form_defaults', 10, 1 );
function so16856397_comment_form_defaults( $defaults )
{
$defaults['comment_field'] = '';
return $defaults;
}
or the comment_form_field_comment filter:
add_filter( 'comment_form_field_comment', 'so16856397_comment_form_field_comment', 10, 1 );
function so16856397_comment_form_field_comment( $field )
{
return '';
}
Check the comment_form source code.
You can also hide the textarea comment box by taking its id set to display:none from CSS
Just simple, add this code in functions.php file
function disable_comments_everywhere( $open, $post_id ) {
return false;
}
add_filter( 'comments_open', 'disable_comments_everywhere', 10 , 2 );

Categories