Move variations under product thumbnail - wordpress

I'm trying to move the variations under my thumbnails but i'm not sure where to start.. Should i use a hook or a template ?
Here's what i'm trying to do :
Feel free to give me ideas or even a solution if you ever encountered this issue !

You can use woocommerce_product_thumbnails to add your variation under the thumbnail. you need to remove default woocommerce_variable_add_to_cart action hook. try the below code. code will go in your functions.php file.
function move_variation_under_thumbnail(){
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
global $product;
// Enqueue variation scripts.
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Get Available variations?
$get_variations = count( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
// Load the template.
wc_get_template(
'single-product/add-to-cart/variable.php',
array(
'available_variations' => $get_variations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
)
);
}
add_action( 'woocommerce_product_thumbnails', 'move_variation_under_thumbnail', 10 );
Or you can do below as Martin Mirchev suggested.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_product_thumbnails', 'woocommerce_template_single_add_to_cart', 30 );ommerce_product_thumbnails', 'woocommerce_template_single_add_to_cart', 30 );
Tested and works

Related

Problem to use add_action or remove_action

I know the rule of add_action or remove_action like:
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display', 10 );
But how to apply remove_action this type of hook
remove_action( 'woocommerce_cart_actions', ' Points_Rewards_For_WooCommerce_Public–>mwb_wpr_woocommerce_cart_coupon', 10 );
Its not working
I think the following should work. Have you tried ?
remove_action( 'woocommerce_cart_actions', array( 'Points_Rewards_For_WooCommerce_Public', 'mwb_wpr_woocommerce_cart_coupon' ), 10 );
The syntax is like
remove_action( 'hook_name', array( 'class_name', 'callback_name' ), priority );

Activating a plugin from inside Woocomerce single product page using a check box

i am looking for a way to activate a plugin from a woocommerce single product page, the plugin displays shipping countdown for delivery but its not relevant to all products and i would like to use a check box inside the single product to turn on and of per product page
the plugin is being activated across all products at the moment using the add action from theme mod to be able to change the position this really is not necessary so if need this could be removed and replaced with some other way on activating the plugin on a per product basis
$scfwc_render_location = get_theme_mod( 'scfwc_render_location');
switch ( $scfwc_render_location ) :
case 'scfwc_after_heading' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 6 );
break;
case 'scfwc_after_price' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 11 );
break;
case 'scfwc_after_short_desc' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 21 );
break;
case 'scfwc_after_add_cart' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 31 );
break;
case 'scfwc_after_single_product_summary' :
add_action( 'woocommerce_product_thumbnails', array( $this, 'scfwc_html_product' ), 30 );
break;
endswitch;
I don't know what class $this is referencing, or how to get an instance of it, so this is pseudo-code. But once you have the meta field displaying and saving in the admin, you can use the meta field's value to conditionally add the scfwc_html_product() method.
function kia_conditionally_add_countdown() {
global $product;
if( $product->get_meta( '_show_countdown', true ) ) {
$my_class = somehow_get_instance_of_class();
add_action( 'woocommerce_single_product_summary', array( $my_class, 'scfwc_html_product' ), 11 );
}
}
add_action( 'woocommerce_before_single_product_summary', 'kia_conditionally_add_countdown' );
This doesn't take into account the theme mod and just hard-codes the priority/position. You could add support for the theme_mod inside the conditional logic, if needed.

How to remove actions from Yoast SEO plugin

I need to remove the actions that Yoast SEO has added. This is my code:
function remove_actions() {
// deregister all not more required tags
remove_action( 'wp_head', '_wp_render_title_tag', 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'test123' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'front_page_specific_init' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metadesc' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'robots' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metakeywords' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'canonical' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'adjacent_rel_links' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'publisher' ), 50 );
}
add_action( 'wp_head', 'remove_actions', 1000 );
This code does not remove the actions. What is wrong? How can I remove the actions successfully?
Consider these notes from the remove_action Documentation:
You may need to prioritize the removal of the action to a hook that occurs after the action is added.
You cannot successfully remove the action before it has been added.
You also cannot remove an action after it has been run.
To remove an action the priority must match the priority with with the function was originally added.
In your case, I believe several of these issues (especially #3 and #4) are causing problems:
First, the priority on your add_action is too high. By setting it this high, it's running after all of the Yoast wp_head actions are run. Instead, hook into the same action you want to remove, but with a very low number, such as -99999, to cause it to run before the Yoast actions are run. (Further, I've broken into two functions, just to be sure they are run at the correct time - one for each action - wp_head and wpseo_head).
Second, your priorities do not match the priorities in the Yoast code. I've dug through all the Yoast code to find all of these actions and documented / corrected in the code below - and I can tell you for example the metakeywords hook in Yoast code is 11, so your remove_action (with priority 40) will not work.
Finally, Yoast adds these actions to $this (an instantiated version of the WPSEO_Frontend class), not static version of the class methods. This means that remove_action is not able to find them based on the function array(WPSEO_Frontend, head), for example. Instead, you need to load the instantiated version of Yoast, and pass that in to the remove_action functions.
Documented code below:
// Remove ONLY the head actions. Permits calling this at a "safe" time
function remove_head_actions() {
// not Yoast, but WP default. Priority is 1
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// If the plugin isn't installed, don't run this!
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the WPSEO_Frontend instantiated class
$yoast = WPSEO_Frontend::get_instance();
// removed your "test" action - no need
// per Yoast code, this is priority 0
remove_action( 'wp_head', array( $yoast, 'front_page_specific_init' ), 0 );
// per Yoast code, this is priority 1
remove_action( 'wp_head', array( $yoast, 'head' ), 1 );
}
function remove_wpseo_head_actions() {
// If the Yoast plugin isn't installed, don't run this
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the Yoast instantiated class
$yoast = WPSEO_Frontend::get_instance();
remove_action( 'wpseo_head', array( $yoast, 'head' ), 50 );
// per Yoast code, this is priority 6
remove_action( 'wpseo_head', array( $yoast, 'metadesc' ), 6 );
// per Yoast code, this is priority 10
remove_action( 'wpseo_head', array( $yoast, 'robots' ), 10 );
// per Yoast code, this is priority 11
remove_action( 'wpseo_head', array( $yoast, 'metakeywords' ), 11 );
// per Yoast code, this is priority 20
remove_action( 'wpseo_head', array( $yoast, 'canonical' ), 20 );
// per Yoast code, this is priority 21
remove_action( 'wpseo_head', array( $yoast, 'adjacent_rel_links' ), 21 );
// per Yoast code, this is priority 22
remove_action( 'wpseo_head', array( $yoast, 'publisher' ), 22 );
}
Final Notes:.
Remove the WPSEO_Frontend::head action is very heavy handed. This will yank a whole host of other things you probably don't want removed.
Second, It's probably better to modify the output of these actions, rather than removing them completely.
For example,
add_action('wpseo_metakeywords', 'your_metakeywords_function');
function your_metakeywords_function( $keywords ) {
// modify the keywords as desired
return $keywords;
}
Many of these actions have filters and output can be removed by returning false.
// Removes 'meta name="description"' tag from output
add_filter( 'wpseo_metadesc', 'my_custom_metadesc' );
function my_custom_metadesc() {
return false;
}
In some cases like WPSEO_Opengraph there's a filter pattern: wpseo_og_ + property name with underscores instead of colons.
// Filters '<meta property="article:tag" content="Foo" />'
add_filter( 'wpseo_og_article_tag', 'my_custom_article_tag' );

Remove add to cart button from shortcode in woocommerce

I am implementing a shop that only allows logged in users to view the add to cart buttons.
I have successfully hidden most of them with the following code:
function thread_remove_loop_button(){
if(!is_user_logged_in() ){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
add_action('init','thread_remove_loop_button');
However, there are still some showing in the New Products & Best seller widgets. Looking at the code, I can see these are calling the shortcode
do_shortcode('[add_to_cart id="'.$product->id.'"]');
What is the best way to amend these so the add to cart button only shows for logged in users. Obviously in the template I can do something along the lines of
if(is_user_logged_in())
echo do_shortcode('[add_to_cart id="'.$product->id.'"]');
}
but it seems like there should be a better way? Along the lines of a hook or something?
add_action('init', 'bbloomer_hide_price_add_cart_not_logged_in');
function bbloomer_hide_price_add_cart_not_logged_in() {
if ( !is_user_logged_in() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_single_product_summary', 'bbloomer_print_login_to_see', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}
function bbloomer_print_login_to_see() {
echo '' . __('Login to see prices', 'theme_name') . '';
}
put this in your plugins/woocommerce/woocommerce.php
it will hide the price and add to cart button and print a statement login to see price
The add to cart template function is pluggable, meaning that if you define a function in your theme with the same name it will override that of WooCommerce.
function woocommerce_template_loop_add_to_cart( $args = array() ) {
if(is_user_logged_in()){
wc_get_template( 'loop/add-to-cart.php' , $args );
}
}
You need to use hook which not affect other code.
add_action('init', 'hide_add_cart_not_logged_in');
function hide_add_cart_not_logged_in() {
if (!is_user_logged_in()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
that only allows logged in users to view the add to cart button.
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html

Menus panel in wordpress customizer

I am trying to remove Menus panel in wordpress customizer using below code but its not working.Someone please guide me.Thanks
$wp_customize->remove_panel( 'nav_menus' );
There are 2 Trac tickets that govern this issue. The complete solution to this is
add_action( 'customize_register', function( $wp_customize ) {
/** #var WP_Customize_Manager $wp_customize */
remove_action( 'customize_controls_enqueue_scripts', array( $wp_customize->nav_menus, 'enqueue_scripts' ) );
remove_action( 'customize_register', array( $wp_customize->nav_menus, 'customize_register' ), 11 );
remove_filter( 'customize_dynamic_setting_args', array( $wp_customize->nav_menus, 'filter_dynamic_setting_args' ) );
remove_filter( 'customize_dynamic_setting_class', array( $wp_customize->nav_menus, 'filter_dynamic_setting_class' ) );
remove_action( 'customize_controls_print_footer_scripts', array( $wp_customize->nav_menus, 'print_templates' ) );
remove_action( 'customize_controls_print_footer_scripts', array( $wp_customize->nav_menus, 'available_items_template' ) );
remove_action( 'customize_preview_init', array( $wp_customize->nav_menus, 'customize_preview_init' ) );
}, 10 );
More details:
https://core.trac.wordpress.org/ticket/33552
https://core.trac.wordpress.org/ticket/33411
In the old versions of wordpress (<4.3) this worked, but not now:
function your_customizer( $wp_customize ) {
$wp_customize->remove_panel( 'widgets' );
}
add_action( 'customize_register', 'your_customizer' );
Thank you dingo_d, that worked for me too.
Incidentally, $wp_customize->remove_panel('widgets); still works in Wordpress v. 4.5.1

Resources