1
votes

While developing a WooCommerce plugin which adds custom products to shoppin cart, I am using the function add_to_cart inside the class-wc-cart.php. But after updating woocomerce to latest version I receive the error.

Sorry, this product cannot be purchased.

because inside the function add_to cart, the section $product_data->is_purchasable() started to return false. I debugged a find out that the reason why is_purchasable() returns false is that get_price() function. Because this function returns empty as price, hence woocommerce tells me the product is not purchasable because of empty price.

The product has a price and can be added to the cart using the interface. The plugin was working fine with old version of woocommerce. Let me know if you need more data.

1

1 Answers

1
votes

You can make the necessary changes using and removing '' !== $product->get_price() from the IF statement, this way:

add_filter( 'woocommerce_is_purchasable', 'customizing_is_purchasable', 20, 2 );
function customizing_is_purchasable( $purchasable, $product ){
    if( $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ) )
        $purchasable = true; 

    return $purchasable;
}

For product variations, you can also try at the same time (untested, but it should work):

add_filter( 'woocommerce_variation_is_visible', 'customizing_variation_is_visible', 20, 4 );
function customizing_variation_is_visible( $visible, $product_id, $parent_id, $product ){
    if( 'publish' === get_post_status( $product->get_id() ) )
        $visible = true;
    else
        $visible = false;

    return $visible;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.