Remove and Override WooCommerce Process Registration action - php

I'm developing a plugin to customise the woocommerce registration and trying to avoid direct editing of the core files.
I need to override or replace process_registration action in woocommerces includes/class-wc-form-handler.php file through my plugin.
add_action( 'init', array( $this, 'process_registration' ) );
I tried following links, but they didn't work. Also, the files mentioned on those pages doesn't exist on woocommerce current version. I also checked woocommerce documentation, but it seems they don't have a hook for that.
http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp
Woocommerce Hooks:
http://docs.woothemes.com/document/hooks/
I'd really appreciate any help!

Two options for that and considering that WC method starts like:
class WC_Form_Handler
public function __construct() {
add_action( 'init', array( $this, 'process_registration' ) );
}
public function process_registration() {
if ( ! empty( $_POST['register'] ) ) {
wp_verify_nonce( $_POST['register'], 'woocommerce-register' );
# etc
}
}
}
new WC_Form_Handler();
Add an init hook with top priority and duplicate then unset($_POST['register']). WC doesn't specify a priority, so it's running on 10, which is default.
add_action( 'init', function() { /* dup, unset, do your thing */ }, 1 ); // priority 1
Track down that evil anonymous object where Woo is hidding its hook, so that you can:
// pseudo code, go to WPSE for the real thing
remove_hook( 'init', 'woo_process_registration' );

Related

Using ACF true/false field on WooCommerce single product page

I have a WooCommerce site and in the single product page I have a custom checkbox to agree to terms before the add to cart button, but I am trying to add a true/false field in the dashboard so that this checkbox can be moved to a different position on the page.
My functions look like this:
add_action( 'acf/init', "acf_move_checkbox", 10 );
function acf_move_checkbox() {
$move_cart = get_field('move_cart');
if ($move_cart) {
add_action( 'woocommerce_after_single_product', "acf_product_terms", 10 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', "acf_product_terms", 10 );
}
// More code
}
And nothing is happening, am I along the right lines with this or way off?
While acf/init is similar to the WordPress init action, I wouldn't use it.
Init hooks are performed constantly.. since you want to apply an action on the single product page it's best to use a hook that only applies to that page, and will only run on those kinds of pages.
For example you can use the woocommerce_single_product_summary hook. It might also be useful to test fields by hard coding before retrieving them.
So you get:
function action_woocommerce_single_product_summary() {
// Get field
//$move_cart = get_field( 'move_cart' );
// Set true OR false
$move_cart = false;
// When true
if ( $move_cart ) {
add_action( 'woocommerce_after_single_product', 'my_callback_function', 9 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', 'my_callback_function', 9 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
function my_callback_function() {
echo '<p style="color: red; font-size: 20px;">Hello World!</p>';
}
If the above step works, you can replace the hard coded field with the desired code/field

remove woocommerce storefront homepage title php

I am using storefront theme by woocommerce. I need to remove homepage title (h1) with php, i know css solution, but i don't want to use it, because i want to add h1 to other place in that page and it's bad for seo to have 2 h1's in one page! I also know about plugins that remove page title, but they are working as css display:none; property!
I tried all the snippets that i could find in web, but no luck!
Here is my site domain BrightBells.com
Here is PHP code snippets that i tried one by one by adding to my functions.php file, but none of its help!
Snippet 1
function wc_hide_page_title() {
if( is_front_page() )
return true;
}
add_filter( 'woocommerce_show_page_title', 'wc_hide_page_title' );
Snippet 2
function sf_change_homepage_title( $args ) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'init', 'sf_change_homepage_title' );
Snippet 3
function sf_change_homepage_title( $args ) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'wp', 'sf_change_homepage_title' );
Snippet 4
function sf_change_homepage_title( $args ) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'after_setup_theme', 'sf_change_homepage_title' );
Snippet 5
function sf_change_homepage_title( $args ) {
if(is_front_page()) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
}
add_action( 'init', 'sf_change_homepage_title' );
Snippet 6
if ( is_front_page() ) {
remove_action( 'storefront_page', 'storefront_page_header' );
}
Snippet 7
if ( is_page('page id') )
{
add_filter( 'the_title', '__return_false' );
}
None of this snippets help, please help!
Here is page settings screenshots!
setting page screenshot
page screenshot
Thanks in advance
I hope you are using a child theme or plugin in doing this.
Because it will just be removed when your theme updates.
In storefront version 2.2.5, it can be done with this code:
remove_action( 'storefront_homepage', 'storefront_homepage_header', 10 );
Update:
if on a child theme please do something like this:
if ( ! function_exists( 'storefront_homepage_header' ) ) {
function storefront_homepage_header() { }
}
Child themes are run first before the parent theme. By doing so, we will define this storefront_homepage_header function first. In that case, the parent theme will not create it. Applicable only on themes that uses function_exists, luckily, storefront does.
remove_action will not work because the action has not been added yet.
Try this ---
if ( is_front_page() || is_home() )
{
add_filter( 'the_title', '__return_false' );
}

WooCommerce Points and Rewards - Move render_product_message

I'm using the WooCommerce Points and Rewards plugin.
By default, the single product message (Earn up to x Points.) appears immediately after product excerpt. However, I want change it.
I already tried a lot of things, but without success... but I think that I'm close to the right answer.
I hope that someone can give me a help.
function test() {
global $wc_points_rewards;
remove_action( 'woocommerce_single_product_summary', array($wc_points_rewards->product, 'render_product_message' ) );
}
add_action( 'woocommerce_single_product_summary', 'test', 5 );
The class that adds the "render_product_message" is WC_Points_Rewards_Product that is initialized on WC_Points_Rewards.
WC_Points_Rewards_Product (i'm only posting the related method)
public function __construct()
{
// add single product message immediately after product excerpt
add_action('woocommerce_single_product_summary', array($this, 'render_product_message'));
}
WC_Points_Rewards (i'm only posting the related method).
The includes method is called on __construct method of this class.
private function includes()
{
// product class
require('includes/class-wc-points-rewards-product.php');
$this->product = new WC_Points_Rewards_Product();
}
This works for me:
add_action( 'init', 'remove_points');
function remove_points()
{
global $wc_points_rewards;
remove_action( 'woocommerce_before_add_to_cart_button', [ $wc_points_rewards->product, 'render_product_message' ], 15 );
}
You are better off renaming the fields in the class-wc-points-rewards-product.php file to show "after" or "before" an element. Sure, you'll have to rename every time there's an update but writing a hook might mess something up.

WooCommerce Smart Coupons override "available coupons" position

I am using Wordpress 4.6 with WooCommerce 2.6.4 and Smart Coupon 3.1.2 plugin.
I would like to move the "available coupons" section from the top of my checkout page to the bottom.
In the plugin file I've detected this structure and the action that I need to change.
I need to change it to 'woocommerce_after_checkout_form' but I can't get my head around on how to override it from the functions.php file.
I've already tried directly from the plugin to change it and it works like I would but we all know that it's a wrong approach to edit a plugin's behaviour.
Any help and documentation would be really helpful.
class WC_Smart_Coupons {
...
public function __construct() {
...
add_action( 'woocommerce_before_checkout_form', array( $this, 'show_available_coupons_before_checkout_form' ), 11 );
...
}
}
...
function initialize_smart_coupons() {
$GLOBALS['woocommerce_smart_coupon'] = new WC_Smart_Coupons();
}
add_action( 'plugins_loaded', 'initialize_smart_coupons' );
So, I had similar problem today. You can do following to move that section to different place.
To remove:
remove_action( 'woocommerce_before_checkout_form', array( $GLOBALS['woocommerce_smart_coupon'], 'show_available_coupons_before_checkout_form' ), 11 );
To add:
add_action( 'any_action', array( $GLOBALS['woocommerce_smart_coupon'], 'show_available_coupons_before_checkout_form' ), 11 );
The accepted solution doesn't work for the Smart Coupons version I use (4.12.2).
To change the "available coupons" position use the following instead:
$obj_inst = WC_SC_Display_Coupons::get_instance();
remove_action( 'woocommerce_before_checkout_form', array( $obj_inst, 'show_available_coupons_before_checkout_form' ), 11 );
add_action( 'woocommerce_checkout_after_customer_details', array( $obj_inst, 'show_available_coupons_before_checkout_form' ), 11 );
A nifty visual list of woocommerce checkout page hooks can be found here.

Woocommerce Order Delivery plugin change display location

I'm working on a Woocommerce site that is using the following plugin: https://docs.woothemes.com/document/woocommerce-order-delivery/ ("WooCommerce Order Delivery")
The plugin is now displayed on the Checkout page under the billing fields in the checkout_shipping section, I am trying to change this location to the checkout_order_review section by hooking in to the functions.
But I can't seem to get it working.
My code in my functions.php:
function action_woocommerce_checkout_shipping( $instance ) {
global $woocommerce;
if ( is_checkout() && $woocommerce->cart->needs_shipping() ) {
echo 'Hi World!';
if ( wc_od() ){
echo 'Found wc_od function';
}
remove_action( 'woocommerce_checkout_shipping', 'checkout_content' );
}
};
// add the action
add_action( 'woocommerce_checkout_shipping', 'action_woocommerce_checkout_shipping' );
My thought behind this code was that I remove the function 'checkout_content' which retrieves the plugin template and then add the action to the woocommerce_checkout_order_review function to display it in the order review section.
But my remove_action doesn't seem to work.
The code in the plugin that adds the checkout_content action:
protected function __construct() {
// WP Hooks.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// WooCommerce hooks.
add_action( 'woocommerce_checkout_shipping', array( $this, 'checkout_content' ), 99 );
add_action( 'woocommerce_checkout_process', array( $this, 'validate_delivery_date' ) );
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'update_order_meta' ) );
// Delivery date validation hooks.
add_filter( 'wc_od_validate_delivery_date', array( $this, 'validate_delivery_day' ), 10, 2 );
add_filter( 'wc_od_validate_delivery_date', array( $this, 'validate_minimum_days' ), 10, 2 );
add_filter( 'wc_od_validate_delivery_date', array( $this, 'validate_maximum_days' ), 10, 2 );
add_filter( 'wc_od_validate_delivery_date', array( $this, 'validate_no_events' ), 10, 2 );
}
Can someone maybe give me a push in the right direction?
Am I doing this wrong ? or is there a better way of achieving this?
You have two issues.
Firstly you need to specify the execution priority when you remove action, or at least you do when it is anything other than the default, 10. In your case the execution priority is 99.
Secondly, checkout_content() is a class function, not a standalone function so you need to specify this in the function reference.
So your remove_action line of code will be:
remove_action( 'woocommerce_checkout_shipping', array( $the_class_variable, 'checkout_content' ), 99 );
where $the_class_variable is the instance of the class containing that __construct() function. How you refer to this depends on how the class has been instantiated in the plugin.
You can read about different ways of instantiating a class and the corresponding remove_action at http://jespervanengelen.com/different-ways-of-instantiating-wordpress-plugins/
You can read about remove_action this in https://codex.wordpress.org/Function_Reference/remove_action

Categories