Ben Metcalfe
Ben Metcalfe

Reputation: 54

"Warning: Missing argument 2 for WC_Custom_Product" in Woocommerce

I am migrating a site to a new host and I am getting the following error. I do not quite understand what this error relates to and why it would be fine on the current host but showing this on new hosting despite it being an exact replication using the All-on-one Migration Plugin

Warning: Missing argument 2 for WC_Custom_Product::action_woocommerce_before_single_product_summary() in /wp-content/themes/custom-theme/classes/wc-custom-product.php

The function looks like this, there is not a lot to it really but I don't know how to fix

public function __construct() {
    $this->product_id = get_the_ID();
    $this->product = wc_get_product( $this->product_id );
    add_action( 'woocommerce_before_single_product_summary', array($this, 'action_woocommerce_before_single_product_summary'), 10, 2 );
}

function action_woocommerce_before_single_product_summary( 
    $woocommerce_show_product_sale_flash, $int ) { 
    echo "<div class='product-row first'>";
}

Any help would be much appreciated

Upvotes: 1

Views: 738

Answers (2)

TosheX
TosheX

Reputation: 64

The way you defined the function it will only work with 2 parameters passed to it. The woocommerce action woocommerce_before_single_product_summary apparently passes only one parameter, if at all.

So in order to circumvent this you just need to define default values to your parameters in such cases, so that the "missing argument" warning does not fire, like so:

function action_woocommerce_before_single_product_summary(
$woocommerce_show_product_sale_flash = null, $int = 0 ) { 
    echo "<div class='product-row first'>";
}

Notice how for safe measure I defined $woocommerce_show_product_sale_flash as NULL? If the call to action passes any values to this function, the defaults (null and zero) will be overwritten.

Also, keep in mind that your "error" is in fact just a warning. It will not break your code and your function should work, but this depends on your PHP error settings, and you probably have warning messages visible or debug mode enabled in WordPress config.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253921

The Woocommerce action hook woocommerce_before_single_product_summary located in content-product.php template accepts only one argument and it's mostly never used.

Note that action hooks doesn't need to return an argument as filter hooks does.

Also in your constructor function, you don't need to specify the number of arguments, and as no arguments are used in your hooked function, you can just remove them all just like woocommerce does here and here.

So try this instead:

public function __construct() {
    $this->product_id = get_the_ID();
    $this->product = wc_get_product( $this->product_id );
    add_action( 'woocommerce_before_single_product_summary', array($this, 'action_woocommerce_before_single_product_summary') );
}

public function action_woocommerce_before_single_product_summary() { 
    echo "<div class='product-row first'>";
}

It should work without errors.

Upvotes: 1

Related Questions