New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Warnings when creating a variable product programmatically #16653

Closed
jesucarr opened this Issue Aug 30, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@jesucarr

jesucarr commented Aug 30, 2017

I'm getting PHP warnings with Woocommerce 3.1.2 when creating a variable product programmatically:

$product = new WC_Product_Variable();
...
$parent_id = $product->save(); // This creates the warnings
// I need the parent id for to add the variations
$variation = new WC_Product_Variation();
$variation->set_parent_id( $parent_id );
...

When I follow those steps, I see in the PHP error log:
PHP Notice: Undefined index: regular_price in .../wp-content/plugins/woocommerce/includes/class-wc-product-variable.php on line 430
PHP Notice: Undefined index: sale_price in .../wp-content/plugins/woocommerce/includes/class-wc-product-variable.php on line 430

The problem is on the is_on_sale function, which is called on save, when trying to call the get_variation_prices function, which doesn't return the regular_price or sale_price because it still doesn't have variation products assigned.

mikejolley added a commit that referenced this issue Aug 31, 2017

@mikejolley

This comment has been minimized.

Show comment
Hide comment
@mikejolley

mikejolley Aug 31, 2017

Member

That function does return those values. Even if there are no variations, the array keys are set here before passing to a transient.

$transient_cached_prices_array[ $price_hash ] = array(
'price' => $prices,
'regular_price' => $regular_prices,
'sale_price' => $sale_prices,
);
set_transient( $transient_name, json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 );

Maybe you have something else changing these values via the filter. Possibly extension.

I've adding a unit test to prove core is not at fault. The array values exist, and the ID is set without a notice being thrown. As long as that test passes, we're good.

1e9e6c4

Thanks

Member

mikejolley commented Aug 31, 2017

That function does return those values. Even if there are no variations, the array keys are set here before passing to a transient.

$transient_cached_prices_array[ $price_hash ] = array(
'price' => $prices,
'regular_price' => $regular_prices,
'sale_price' => $sale_prices,
);
set_transient( $transient_name, json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 );

Maybe you have something else changing these values via the filter. Possibly extension.

I've adding a unit test to prove core is not at fault. The array values exist, and the ID is set without a notice being thrown. As long as that test passes, we're good.

1e9e6c4

Thanks

@mikejolley mikejolley closed this Aug 31, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment