Woo Conditionals on init - wordpress

I'm trying to add the title above the breadcrumbs. My questions is why WooCommerce Conditionals work better on template_redirect but don't work on init. For example:
/** Woo General Init **/
function woo_general_init() {
echo ( is_product() ) ? 'True' : 'False';
if( is_product() ) {
add_action( 'woocommerce_before_main_content', 'woocommerce_template_single_title', 10 );
add_action( 'woocommerce_before_main_content', 'woocommerce_template_single_excerpt', 11 );
}
}
add_action( 'init', 'woo_general_init' );
Evaluates to False at the top of my document when viewing a single WooCommerce product. On the other-hand, if I change init to template_redirect the conditional outputs True:
/** Woo General Init **/
function woo_general_init() {
echo ( is_product() ) ? 'True' : 'False';
if( is_product() ) {
add_action( 'woocommerce_before_main_content', 'woocommerce_template_single_title', 10 );
add_action( 'woocommerce_before_main_content', 'woocommerce_template_single_excerpt', 11 );
}
}
add_action( 'template_redirect', 'woo_general_init' );
Why do I get "False" when hooked into init but "True" on template_redirect? What's the best hook to run WooCommerce conditional data?

The action init happens too early to know if the current page should display a page. You have to wait at least until template_redirect. Reference from "is_page() not working from within a plugin" - https://wordpress.stackexchange.com/q/115142/26523

Related

Remove product title when product is "out of stock" on WooCommerce shop and archives pages

I am running an art gallery website using WooCommerce and the owner does not want to show the product name/title if the product has been sold/out of stock.
I have got this far with putting together a function, but it doesn't work. I wondered if anyone could give me any tips?
// Hides title on Sold products
add_filter( 'woocommerce_shop_loop_item_title', 'remove_name', 10, 2 );
function remove_name ( $product ) {
if ( ! $product->is_in_stock()) {
$title = '';
}
return $title;
}
Your code contains some errors and mistakes:
woocommerce_shop_loop_item_title is NOT an filter but an action hook
No arguments are passed to this hook , while your code says there would be 2
$product is undefined, use global $product;
Note: I've added a line with debug information, it can be removed
So you get:
function action_woocommerce_shop_loop_item_title() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Should be removed but may be useful towards debugging
echo '<p style="color: red; font-size: 20px;">DEBUG information: ' . $product->get_stock_status() . '</p>';
// NOT in stock
if ( ! $product->is_in_stock() ) {
// Removes a function from a specified action hook.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
}
}
}
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 9 );
Please try to use this hook to remove product title. Use this inside the condition to check if product is in stock:
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
This does what you want but still allows access to the product page.
add_action( 'woocommerce_shop_loop_item_title', function() {
global $product;
if ( ! $product->is_in_stock()) {
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
}
}, 5 );
add_action( 'woocommerce_single_product_summary', function() {
global $product;
if ( ! $product->is_in_stock()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);
}
}, 1);

Issue hide product single price for variable products on WooCommerce shop and archives pages

Im using this snippet to hide single price if the product is variable. Works fine but not apply on first product variable of loop:
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price' );
function hide_single_price() {
global $product;
if ( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
Any idea why?
I don't immediately see a problem with your current code, what you can try:
Adjust the priority value
Echo the text 'debug'. So you can check if that is displayed for the first product.
function hide_single_price() {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
if ( $product->is_type( 'variable' ) ) {
// Debug purposes, delete afterwards!!
echo 'DEBUG!';
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price', 5 );

How to remove gutenberg_render_title_tag from theme function file?

I have try remove action but not working.
add_action( 'wp_loaded', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
or
add_action( 'init', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
i am using yoast plugin. Yoast plugin also using remove action https://github.com/Yoast/wordpress-seo/blob/trunk/src/integrations/front-end-integration.php#L220 but it's not working. Still tag coming two time. i wants to remove gutenberg title tag.
I got the solution for this.
add_action( 'wp', 'remove_default_guten_title' );
function remove_default_guten_title() {
foreach ( gutenberg_get_template_type_slugs() as $template_type ) {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'remove_default_title', 21, 3 );
}
}
function remove_default_title() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
return gutenberg_dir_path() . 'lib/template-canvas.php';
}

Remove action defined within plugin class

I am developing a ecommerce theme.
I have installed the WooCommerce PayPal Checkout Payment Gateway plugin for payment, and I want to change the location of this checkout button, I tried to remove_action display checkout button but it didn't work, How can I remove action in this case?
Hook in plugin file:
plugins/woocommerce-gateway-paypal-express-checkout/includes/class-wc-gateway-ppec-with-spb.php
This is my code in functions.php file, it not works:
function remove_anon_filters( $name, $functionname, $priority ) {
global $wp_filter;
foreach ( $wp_filter[ $name ][ $priority ] as $key => $data ) {
if ( stripos( $key, $functionname ) ) {
remove_action( $name, $key, $priority );
}
}
}
add_action( 'plugins_loaded', 'demo_init', 999 );
function demo_init() {
remove_anon_filters( 'woocommerce_review_order_after_submit', 'display_paypal_button', 10 );
}
// or.
add_action( 'init', 'remove_init', 999 );
function remove_init() {
remove_action( 'woocommerce_review_order_after_submit', array( 'WC_Gateway_PPEC_With_SPB', 'display_paypal_button' ), 10 );
}
Any help is appreciated, thank you.
To remove a non-static method, you should instantiate the class, and remove the action in the wp_head https://developer.wordpress.org/reference/functions/remove_action/
Additionally, the remove_action should be the same priority as the add action, which in this case is not specified.
add_action( 'wp_head', 'remove_ppec_with_spb', 10);
function remove_ppec_with_spb() {
$class = new WC_Gateway_PPEC_With_SPB;
remove_action( 'woocommerce_review_order_after_submit', array( $class , 'display_paypal_button' ) );
}

Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page

I want to show the css and javascript only when the shortcode is used in that page. If the short code not present in the wordpress page then the js and css of contact form should not be shown. For that what i have done is i have pasted the following code in my active themes function.php file.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
The above code totally removes the js and css of contact form 7 plugin. What i need is if contact form 7 shortcode is pasted then both should be shown.
Here is the answer for your question. If there is not shortcode the css and js of contact form will be removed and if there is shortcode css and js will be added.
function rjs_lwp_contactform_css_js() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
wp_enqueue_script('contact-form-7');
wp_enqueue_style('contact-form-7');
}else{
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');
I needed the other version that corresponds widgets and shortcodes in theme file.
add_filter( 'wpcf7_load_css', '__return_false' );
add_filter( 'wpcf7_load_js', '__return_false' );
remove_action( 'wp_enqueue_scripts','wpcf7_recaptcha_enqueue_scripts', 20 );
add_filter('pre_do_shortcode_tag', 'enqueue_wpcf7_css_js_as_needed', 10, 2 );
function enqueue_wpcf7_css_js_as_needed ( $output, $shortcode ) {
if ( 'contact-form-7' == $shortcode ) {
wpcf7_recaptcha_enqueue_scripts();
wpcf7_enqueue_scripts();
wpcf7_enqueue_styles();
}
return $output;
}
You use below code.You can add your pages Id in this code.
function dvk_dequeue_scripts() {
$load_scripts = false;
if( is_singular() ) {
$post = get_post();
if( has_shortcode($post->post_content, 'contact-form-7') ) {
$load_scripts = true;
}
}
if( ! $load_scripts ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'dvk_dequeue_scripts', 99 );
Reference

Resources