3

I am using woocommerce at work for equipment requests, therefore all products I will create will need to be £0. Is it possible to set a default value of 0 for the regular price field on the add product form?

Thanks Jack

7

You could run a check on the save_post hook, but WooCommerce already has a hook for processing meta where the post type and security checks have already been done. So using their hook, you just check for a null string on the regular price and set it to 0.

function wpa104760_default_price( $post_id, $post ) {

    if ( isset( $_POST['_regular_price'] ) && trim( $_POST['_regular_price'] ) == '' ) {
        update_post_meta( $post_id, '_regular_price', '0' );
    }

}
add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price' );

Not sure what you are trying to do with WooCommerce, but I had a client use http://a3rev.com/shop/woocommerce-quotes-and-orders/ to switch from a normal price/cart store to a "request for quote" catalog.

Edit: While the above will save a 0 as the price any time a product is created/updated, the following will always allow a product to be purchasable regardless of the price:

add_filter('woocommerce_is_purchasable', '__return_TRUE'); 

To totally remove the "sale" flash, simply unhook it from its action hook:

function woocommerce_remove_stuff(){
  remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
}
add_action('woocommerce_before_single_product', 'woocommerce_remove_stuff');
| improve this answer | |
  • Thanks for your response. I have tried the above function but it doesn't seem to allow the product to be added to the cart. I am trying to create an equipment request system where logged in users can add equipment (that has no price) to the cart and place an "order" for these items. The users are employees of my company and the equipment is already owned by the company. – lupo Jul 1 '13 at 20:23
  • I thought that a free product could be added to the cart. You could also try overriding the woocommerce_is_purchasable filter to always be true. – helgatheviking Jul 1 '13 at 21:03
  • When I manually type 0 into price I can add it to the cart but using the above function and leaving it blank does not show the add to cart button but is displaying the sale icon. How would I go about overriding the woocommerce_is_purchasable filter? – lupo Jul 1 '13 at 21:14
  • My code was for when you create and/or edit a product and don't fill out the price...so a default price. It will automatically set the price to 0 but only when you save a product. It has no bearing on the front-end except that which is effected by having a zero price. Again, I think woocommerce_is_purchasable might be the answer. – helgatheviking Jul 1 '13 at 21:29
  • add_filter('woocommerce_is_purchasable', 'woocommerce_is_always_purchasable'); function woocommerce_is_always_purchasable(){ return true; } – lupo Jul 1 '13 at 21:44
0

I found the solution for this problem, this function sets the product regular price and sale price to 0 after updating the product:

function set_default_price( $post_id, $post ) {

    if ( isset( $_POST['_regular_price'] ) && trim( $_POST['_regular_price'] ) == '' ) {
        update_post_meta( $post_id, '_regular_price', '0' );
    }

if ( isset( $_POST['_sale_price'] ) && trim( $_POST['_sale_price'] ) == '' ) {
        update_post_meta( $post_id, '_sale_price', '0' );
    }

}
add_action( 'woocommerce_process_product_meta', 'set_default_price' );

The line of code below make new product purchasable, you can use this code for new products:

add_filter('woocommerce_is_purchasable', '__return_TRUE'); 
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.