Home Forums WC Vendors Pro Support product edit page default values

NOTICE: We've Moved to a Ticket System for Support

As of August 31, 2017 (12am EST) our support forums will be retired (read-only), and we will be moving to a support ticket system.  This will allow us to better organize and answer support requests, and provide a more personalized experience as we assist our customers.

For the time being, we will leave our forums open for reading and learning while we work on creating a more robust Knowledge Base for everyone to use.

If you are a WC Vendors Pro customer please open a support ticket here. 

If you are a WC Vendors user please open a support ticket on the Wordpress.org forums.

The information on this forum is outdated and in most instances no longer relevant. Please be sure to check our documentation for the most up to date information.

https://docs.wcvendors.com/

Thank you to all of our customers!

 

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #15148
    criticalachievement
    Participant

    Hi!

    Is it possible to set default values for the edit product form fields without editing core plugin code?

    In particular, I’m interested in setting default values for the “Manage Stock” checkbox, the “Sold Individually” checkbox, and the “Stock Qty” field.

    I’ve already discovered that I can achieve this by editing class-wcvendors-pro-product-form.php. For example, I can set the default checkbox value to “checked” by adding “‘value’ => ‘yes'” to the end of the checkbox fields in that file like so:

    WCVendors_Pro_Form_Helper::input( apply_filters( ‘wcv_product_manage_stock’, array(
    ‘post_id’ => $post_id,
    ‘id’ => ‘_manage_stock’,
    ‘wrapper_class’ => ‘show_if_simple show_if_variable’,
    ‘label’ => __( ‘Manage stock?’, ‘wcvendors-pro’ ),
    ‘description’ => __( ‘Enable stock management at product level’, ‘wcvendors-pro’ ),
    ‘type’ => ‘checkbox’,
    ‘value’ => ‘yes’
    ) )
    );

    Similarly, I can add “‘value’ => ’99′” to the end of the “Stock Qty” list of attributes to set the default stock quantity to 99.

    However, as you know, it’s never a good idea to edit files that will be replaced on the next plugin update. So, is there another update-safe way to do this?

    #15149
    WC Vendors Support
    Participant

    You’re halfway there! See the “apply_filters”? That means you can write a filter function for “wcv_product_manage_stock” and set new values for everything, and add your value 99 too. 100% update safe, no core plugin mods! 🙂 🙂

    #15213
    criticalachievement
    Participant

    Thanks, Ben.

    Here’s some code that seems to work in case anyone else wants to do the same thing.

    // Changes the default value for manage stock checkbox to checked
    
    	add_filter( 'wcv_product_manage_stock', 'change_default_manage_stock' );
    	function change_default_manage_stock() {
    
    		$field['post_id'] = $post_id;
    		$field['id'] = '_manage_stock';
    		$field['wrapper_class'] = 'show_if_simple show_if_variable';
    		$field['label'] = __( 'Manage stock?', 'wcvendors-pro' );
    		$field['description'] = __( 'Enable stock management at product level', 'wcvendors-pro' );
    		$field['type'] = 'checkbox';
    		$field['value'] = 'yes'; // This checks the checkbox
    		return $field;
    
    	}
    
    // Changes the default value for sold individually checkbox to checked
    
    	add_filter( 'wcv_product_sold_individually', 'change_default_sold_individually' );
    	function change_default_sold_individually() {
    
    		$field['post_id'] = $post_id;
    		$field['id'] = '_sold_individually';
    		$field['wrapper_class'] = 'show_if_simple show_if_variable';
    		$field['label'] = __( 'Sold Individually', 'wcvendors-pro' );
    		$field['desc_tip'] = 'true';
    		$field['description'] = __( 'Enable this to only allow one of this item to be bought in a single order', 'wcvendors-pro' );
    		$field['type'] = 'checkbox';
    		$field['value'] = 'yes'; // This checks the checkbox
    		return $field;
    
    	}
    
    // Changes the default value for stock qty to 99
    
    	add_filter( 'wcv_product_stock_qty', 'change_default_stock_qty' );
    	function change_default_stock_qty() {
    
    		$field['post_id'] = $post_id;
    		$field['id'] = '_stock';
    		$field['label'] = __( 'Stock Qty', 'wcvendors-pro' );
    		$field['wrapper_start'] = '<div class="all-100">';
    		$field['wrapper_end'] = '</div>';
    		$field['desc_tip'] = 'true';
    		$field['description'] = __( 'Stock quantity.', 'wcvendors-pro' );
    		$field['type'] = 'number';
    		$field['data_type'] = 'stock';
    		$field['custom_attributes']['step'] = 'any';
    		$field['value'] = '99'; // This sets the quantity
    		return $field;
    
    	}
    
    #15221
    WC Vendors Support
    Participant

    Good stuff! You did it perfectly. 🙂 I created a gist for this on github so that anyone else can benefit too.

    https://gist.github.com/bentasm1/9167fc3f6433e02cddcf

    #38344
    Erik Bazuin
    Participant

    Hi Guys,

    i have used your code and when testing it worked fine, suddenly it stopped and i do not know why.

    What i need is only 1 in stock when a product is added, and when sold the product needs te dissappear from the site

    changing this to $field[‘value’] = ‘1’; // This sets the quantity should work right?

    Could somebody be of assistance!

    Gr Erik

    #38384
    WC Vendors Support
    Participant

    Yup

    #38407
    Erik Bazuin
    Participant

    Hi,

    Thanks dor your reply!

    Well thats great!

    Could you think of Anything that might cause it not to work?

    Gr Erik

    #38483
    Anna
    Member

    @pixelpuur
    What is no longer working? What action happened previously that is no longer happening? What changes (ANY) did you make to your site in between the time that the code WAS working and now is NOT working?

    #38744
    Erik Bazuin
    Participant

    @Anna
    Well a new product should have 1 in stock and when this product is bought it should dossappear from the website.
    But i can keep buying the product so the stock is not set i guess.

    I can not recall what happende could be anything.

    Gr Erik

    #38811
    Anna
    Member

    Are you also clicking the “manage stock” checkbox? I’m not sure (since this is custom code that someone came up with) if this will also make sure to recognize that the item is default qty 1 AND also out of stock.

    #40269
    Obsi Dian
    Participant

    This code is exactly what I need thanks! But is there a way to make it also apply on the “backend” editor as well? (it’s only working on the “frontend”)

    #44210
    Nofski
    Participant

    I think I am having some problems here. This code achieved exactly what I wanted but now I am getting some funny quirks when I try to change any of the quantities on the front end.

    So I needed this to ensure vendors don’t forget to manage stock and set the default to one, as most items or one offs.

    However there are a few items which are multiples and when I change the figure on the front end, it does not seem to save the stock quantity at the figure I amended it to. its almost as if the code is resetting it back to one, which is what we told it to do in the first place.

    just checked on the dev site with no other plugins and it looks like its happening there too.

    #44247
    Anna
    Member

    Yes, that’s the drawback with how this is written- it prevents you from effectively adding any other stock qty other than one. 🙁
    This is also on my to-do list to see if we can get a snippet that will allow a default of one, but also allow for the change qty to take affect on a per product basis.

    #60109
    Aaron Roessler
    Participant

    I also wanted to use functions.php to filter the Stock QTY and Price to set default values. but the code provided prevents changing it to any other value.

    So I wrote some jQuery code that checks if these fields are empty, if YES then set the value. in my case I needed to set Price to “0” and Stock to “1” if they were empty.

    This was added to product-edit.php

    
    jQuery(document).ready(function(){
    var value=$.trim($("#_regular_price").val());
    var value=$.trim($("#_stock").val());
    
      if ($('#_regular_price').val()==""){
      $('#_regular_price').val("0");
      }
      if ($('#_stock').val()==""){
      $('#_stock').val("1");
      }
    });
    
    
Viewing 14 posts - 1 through 14 (of 14 total)
  • The forum ‘WC Vendors Pro Support’ is closed to new topics and replies.