add_action run for the specific page - php

Hi I have faced a problem on my code. I wanted to run add_action for the certain page. add_action is working individually when I Put this on the functions.php but when I tried to run this in wp_head action then its not working.
add_action( 'wp_head', 'remove_my_action', 0);
function remove_my_action($post) {
global $post;
$post_id = $post->ID;
if (512 == $post_id) {
echo "Page Found";
remove_action( 'woocommerce_add_to_cart', 'add_product_to_cart');
}else{
add_action('woocommerce_add_to_cart', 'add_product_to_cart');
}
}

Related

Empty woocommerce cart when visiting page under specific parent page

How can I adjust this PHP code so that it empties the woocommerce cart when I visit a page under a specific parent page?
Example:
I visit www.domain.com/anyparentpage/page
nothing happens
I visit www.domain.com/mtm-user/page
empties cart
`
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$slug = $post->post_name;
if($slug == 'mtm-user') {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
`
I tried adding $post->post_parent but it didnt work
Try replacing slug with parent post id. here 31 is post parent id
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$parent_id = $post->post_parent;
if($parent_id == 31) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}

How can I insert the button on a specific page using php wordpress using this code

add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
global $product;
echo 'Extra Button';
}
how will I add this as a function on a specific page help me out with this
You can do this within a theme template (such as page.php) as:
<?php do_action('woocommerce_single_product_summary'); ?>
or in the Dashboard by using a Shortcode block in the Gutenberg page editor. You will first need to create the shortcode in functions.php.
function your_shortcode($atts, $content = null) {
ob_start();
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
return ob_get_clean();
}
add_shortcode('shortcode_name', 'your_shortcode');
Then the shortcode would be [shortcode_name]

Disable WooCommerce shop page completely, but keep search page

I have an issue with WooCommerce. I want to disable the shop page completely, but I still want users to be able to search for products.
This is what I have now - it works, but it also disables the search page, as this uses the shop page.
function woocommerce_disable_shop_page() {
global $post;
if (is_shop()):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
Hope you can help.
I just modified code in your question & it is working. Please add below code in theme's functions.php file or custom plugin file.
function woocommerce_disable_shop_page() {
global $post;
if (is_shop() && !(is_search())):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );

Add product to cart with form submit after woocommerce initialized

I am working on a plugin and I am stuck on something.
I want to add a product to cart on submit.
At first I got an error that WP Wasn't loaded. So I fixed this by using
add_action('wp_loaded', 'wp_after_load');
So here is my code:
HTML
<form method="POST" enctype="multipart/form-data">
<button type="submit" class="btn btn-primary" name="ws-add-to-cart">Add to Cart</button>
</form>
PHP
if (isset($_REQUEST['ws-add-to-cart'])) {
add_action('wp_loaded', 'wp_after_load');
}
function wp_after_load() {
add_action( 'init', 'add_product_to_cart' );
}
function add_product_to_cart() {
echo '<script>alert("dsa");</script>';
global $woocommerce;
global $product;
$product_ident = $product->id;
$product_id = $product_ident;
$woocommerce->cart->add_to_cart($product_id);
}
The thing is, nothing happens.
And my guess is that add_action( 'init', 'add_product_to_cart' ); gets stuck on something, or this isn't the right hook to use.
I am using this hook to wait after woocommerce is done loading.
Yet I'm not sure if this does what I think it does.
So what am I doing wrong here? I got the code from WooThemes:
https://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/
You just have to use like this:
if (isset($_REQUEST['ws-add-to-cart'])) {
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
echo '<script>alert("dsa");</script>';
global $woocommerce;
global $product;
$product_ident = 175(your product id);
$product_id = $product_ident;
$woocommerce->cart->add_to_cart($product_id);
}
}

WP / WC calling add_action "init" from inside another function

I have a problem with a plugin and WooCommerce.
So I have a plugin with an options page, and a custom checkbox on it.
When this checkbox is activated, I want to hide/remove the default WooCommerce related product container.
I can remove this container if I just add this code:
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
But the problem is that I need to call this function from inside another "add_filter" function.
In the moment I have something like this:
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
But when I change the option in the admin-settings, nothing changes. So I think the "init" hook is not right here.
I cant find the right hook to make this work. What hook must I use when I want it to fire when the plugin options get updated?
Thanks in advanced,
Mo
Thanks to Danijel and his answers.
I dont know why I didnt think of it this way.
Maybe this was to much "action" for me on that late evening ;)
I now placed the "add_action" outside of the "add_filter" function and just do the conditional-check there.
This is working:
add_action( 'init', 'hide_related');
function hide_related () {
if ( get_option( 'prfx_active', 'no' ) == 'yes' ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
};
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
...
Im quite certain that WP init action is fired before woocommerce_after_single_product_summary filter, and also if ( $active = 'yes' { ... expression will always be evaluated as true ( use == ). Try with this simple example:
add_action( 'init', function() {
if ( get_option( 'prfx_active', 'no' ) == 'yes' )
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
});
Try moving your add_action_funciton outside of add_filter_function
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function

Categories