marketpress show quantity in product list

Quantity box not showing in product list when option is selected in store settings. I want people to be able to enter the quantity before hitting the 'add to cart' button.

  • Tyler Postle

    Hey Kristin,

    Thanks for your question. So, the single page product quantity box is fine, but you want that to show on the product listing/grid as well? Unfortunately, this option isn't built into the plugin.

    However, I will flag second level support (coding gurus) to see if they can custom code a solution for you :slight_smile:

    In the meantime, let us know if you have any further questions!

    Cheers,
    Tyler

  • Jose

    Hi there @Kristin,

    Hope you are doing well today!

    I've coded a tailored solution for you. :slight_smile:

    You just need to put the attached .php file into the following folder:
    ../wp-content/mu-plugins/
    (create the folder if it doesn't exists).

    It should work now. No further changes are needed and you can skip the explanation bellow.

    To clarify a bit, changing this in your template is not a good approach. The best way to do this is by hooking into the filter 'mp_buy_button_tag' provided by MarketPress.

    I wrapped the hook into a mu-plugin, but you can put the code in your theme functions.php or in a custom plugin as well.

    Basically, the hook handler will inject the quantity input field into the $button form. This input is explicitly not rendered for the list view, but only for the product detail view. So we need to check the context to avoid rendering the field twice.

    function __construct(){
    
         add_filter('mp_buy_button_tag', array($this, 'add_quantity_field'), 10, 3);
    }
    
    public function add_quantity_field( $button, $post_id, $context ) {
     //Check if this is teh list view to avoid rendering the field twice in single view.
      if('list' !== $context){
          return $button;
      }
    //Define Quantity input field.
     $qty_tag = '<span class="mp_quantity"><label>' . __('Quantity:', 'mp') . ' <input class="mp_quantity_field" type="text" size="1" name="quantity" value="1" /></label></span>&nbsp;';
    
     //Insert the Quantity field at the beginning of button form.
     $new_button = substr_replace($button, $qty_tag, strpos($button, '<input'), 0);
    
     return $new_button;
    }

    Please let me know if it works as expected on your end.

    Cheers!
    JJ

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.