Is there any method to make a Marketpress product expire at a set date and time?

Hi,

One simple question (i keep mentioning it in other tickets):

Is there any method to make a Marketpress product expire at a set date and time?

This could either be by:

1. expiring the entire product listing and redirecting the user to an alternative location if they click to visit.

2. allowing the user to view the product but remove the options to buy and replace instead with a custom message.

It is option 2 that would be most use to me and many others I believe!

Thanks
Chris

  • Vaughan

    Hi Chris,

    This isn't possible out of the box, I have found a plugin that might work for this, but it will require some customization in order to make it work like you require with marketpress.

    If you're not in a rush, I can take a look at it over the next few days when I have some spare time & see if I can get it working for you?

    https://wordpress.org/plugins/post-expirator/
    or https://wordpress.org/plugins/simple-post-expiration/

    Hope this helps

  • Vaughan

    Hi,

    I've found a solution that uses post-expirator plugin.

    It works by setting post-expirator to move the product into a specific category.

    so when a product expires, it is moved into a category called 'Expired'

    You need to add the following to your theme functions.php

    /*
     * Displays the buy or add to cart button
     *
     * @param bool $echo Optional, whether to echo
     * @param string $context Options are list or single
     * @param int $post_id The post_id for the product. Optional if in the loop
     */
    
    function mp_buy_button($echo = true, $context = 'list', $post_id = NULL) {
    		global $id, $mp;
    		$post_id = ( NULL === $post_id ) ? $id : $post_id;
    
                    $expire_category = 'Expired';
    
    		$meta = (array) get_post_custom($post_id);
    		//unserialize
    		foreach ($meta as $key => $val) {
    				$meta[$key] = maybe_unserialize($val[0]);
    				if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file")
    						$meta[$key] = array($meta[$key]);
    		}
    
    		//check stock
    		$no_inventory = array();
    		$all_out = false;
    		if ($meta['mp_track_inventory']) {
    				$cart = $mp->get_cart_contents();
    				if (isset($cart[$post_id]) && is_array($cart[$post_id])) {
    						foreach ($cart[$post_id] as $variation => $data) {
    								if ($meta['mp_inventory'][$variation] <= $data['quantity'])
    										$no_inventory[] = $variation;
    						}
    						foreach ($meta['mp_inventory'] as $key => $stock) {
    								if (!in_array($key, $no_inventory) && $stock <= 0)
    										$no_inventory[] = $key;
    						}
    				}
    
    				//find out of stock items that aren't in the cart
    				foreach ($meta['mp_inventory'] as $key => $stock) {
    						if (!in_array($key, $no_inventory) && $stock <= 0)
    								$no_inventory[] = $key;
    				}
    
    				if (count($no_inventory) >= count($meta["mp_price"]))
    						$all_out = true;
    		}
    
           $terms = get_the_term_list($post_id, 'product_category', '', ',', '');
    
    		//display an external link or form button
            if(strpos($terms,$expire_category)) {
                $button = '';
            } else if (isset($meta['mp_product_link']) && $product_link = $meta['mp_product_link']) {
    
    				$button = '<a class="mp_link_buynow" href="' . esc_url($product_link) . '">' . __('Buy Now »', 'mp') . '</a>';
    		} else if ($mp->get_setting('disable_cart')) {
    
    				$button = '';
    		} else {
    				$variation_select = '';
    				$button = '<form class="mp_buy_form" method="post" action="' . mp_cart_link(false, true) . '">';
    
    				if ($all_out) {
    						$button .= '<span class="mp_no_stock">' . __('Out of Stock', 'mp') . '</span>';
    				} else {
    
    						$button .= '<input type="hidden" name="product_id" value="' . $post_id . '" />';
    
    						//create select list if more than one variation
    						if (is_array($meta["mp_price"]) && count($meta["mp_price"]) > 1 && empty($meta["mp_file"])) {
    								$variation_select = '<select class="mp_product_variations" name="variation">';
    								foreach ($meta["mp_price"] as $key => $value) {
    										$disabled = (in_array($key, $no_inventory)) ? ' disabled="disabled"' : '';
    										$variation_select .= '<option value="' . $key . '"' . $disabled . '>' . esc_html($meta["mp_var_name"][$key]) . ' - ';
    										if ($meta["mp_is_sale"] && $meta["mp_sale_price"][$key]) {
    												$variation_select .= $mp->format_currency('', $meta["mp_sale_price"][$key]);
    										} else {
    												$variation_select .= $mp->format_currency('', $value);
    										}
    										$variation_select .= "</option>\n";
    								}
    								$variation_select .= "</select> \n";
    						} else {
    								$button .= '<input type="hidden" name="variation" value="0" />';
    						}
    
    						if ($context == 'list') {
    								if ($variation_select) {
    										$button .= '<a class="mp_link_buynow" href="' . get_permalink($post_id) . '">' . __('Choose Option »', 'mp') . '</a>';
    								} else if ($mp->get_setting('list_button_type') == 'addcart') {
    										$button .= '<input type="hidden" name="action" value="mp-update-cart" />';
    										$button .= '<input class="mp_button_addcart" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />';
    								} else if ($mp->get_setting('list_button_type') == 'buynow') {
    										$button .= '<input class="mp_button_buynow" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />';
    								}
    						} else {
    
    								$button .= $variation_select;
    
    								//add quantity field if not downloadable
    								if ($mp->get_setting('show_quantity') && empty($meta["mp_file"])) {
    										$button .= '<span class="mp_quantity"><label>' . __('Quantity:', 'mp') . ' <input class="mp_quantity_field" type="text" size="1" name="quantity" value="1" /></label></span> ';
    								}
    
    								if ($mp->get_setting('product_button_type') == 'addcart') {
    										$button .= '<input type="hidden" name="action" value="mp-update-cart" />';
    										$button .= '<input class="mp_button_addcart" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />';
    								} else if ($mp->get_setting('product_button_type') == 'buynow') {
    										$button .= '<input class="mp_button_buynow" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />';
    								}
    						}
    				}
    
    				$button .= '</form>';
    		}
    
    		$button = apply_filters('mp_buy_button_tag', $button, $post_id, $context);
    
    		if ($echo)
    				echo $button;
    		else
    				return $button;
    }

    Note, you can change the category name by changing the line near the top;

    $expire_category = 'Expired';

    Now install post-expirator.

    Then go to Dashboard > settings > post expirator

    Click default (at top)

    Now set the options for product post type like in the the attached screenshot (1)

    Then go to Dashboard > products

    Create a category called Expired

    Now, when you add/edit a product item.

    Select & enable post-expiry in the meta box that appears in the right column.

    Select category: replace or add (replace will remove it from all other categories, Add will add it to the expired category) It will work with either replace or add.

    And check the box for the Expired category as in screenshot(3)

    Now, whenever a product expires, the buy now or add to cart buttons will disappear. making the item un-buyable but still visible.

    Hope this helps

  • Vaughan

    Hi Chris,

    That shouldn't be happening as the function in template-functions.php has an if function_exists() condition. Can I ask which version of marketpress you are using?

    As you are also using multisite, it might be better to actually place the code in a mu-plugin rather than the theme functions.php

    Simply create a file called functions.php

    Then add the code to that file.

    Make sure that you have the following on the very first line though (all other code should go after this line)

    <?php

    Then upload this functions.php file to wp-content/mu-plugins

    If mu-plugins does not exist, just create it.

    Then remove the code I provided from your theme functions.php

    Hopefully that should fix the issue (providing you are using a more recent marketpress version)

    Hope this helps

  • chris

    Hi Vaughan,

    I am running the latest marketpress.

    I did place the code into a plugin to try it our but it had the same effect.

    It is the rather poorrly named: Functionality Plugin for sites (not theme dependant) plugin I made for this purpose.

    I have installed WPMUDEV Dashboard and opened support access for 72 hours.

    I am not running anything else there which could conflict as this was set up to test marketpress and multisite.

    I am using the gridmarket theme (moved into the main wp-content/themes directory. )

    It is a test site don't worry about messing up anything there. I have ftp open my end but have lots of sites on the server so dont tend to give it out... although can set up ftp access if needed.

    Thanks
    Chris

  • Vaughan

    Hi Chris,

    I have tested this with gridmarket active & do not receive that error. My guess is it could be the plugin you are using where you added the code, it might be running after the function in gridmarket is called rather than before it.

    Could you try actually placing it in a mu-plugin instead.

    create a file called functions.php

    Copy the following to that file.

    <?php
    /*
     * Displays the buy or add to cart button
     *
     * @param bool $echo Optional, whether to echo
     * @param string $context Options are list or single
     * @param int $post_id The post_id for the product. Optional if in the loop
     */
    
    function mp_buy_button($echo = true, $context = 'list', $post_id = NULL) {
    		global $id, $mp;
    		$post_id = ( NULL === $post_id ) ? $id : $post_id;
    
                    $expire_category = 'Expired';
    
    		$meta = (array) get_post_custom($post_id);
    		//unserialize
    		foreach ($meta as $key => $val) {
    				$meta[$key] = maybe_unserialize($val[0]);
    				if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file")
    						$meta[$key] = array($meta[$key]);
    		}
    
    		//check stock
    		$no_inventory = array();
    		$all_out = false;
    		if ($meta['mp_track_inventory']) {
    				$cart = $mp->get_cart_contents();
    				if (isset($cart[$post_id]) && is_array($cart[$post_id])) {
    						foreach ($cart[$post_id] as $variation => $data) {
    								if ($meta['mp_inventory'][$variation] <= $data['quantity'])
    										$no_inventory[] = $variation;
    						}
    						foreach ($meta['mp_inventory'] as $key => $stock) {
    								if (!in_array($key, $no_inventory) && $stock <= 0)
    										$no_inventory[] = $key;
    						}
    				}
    
    				//find out of stock items that aren't in the cart
    				foreach ($meta['mp_inventory'] as $key => $stock) {
    						if (!in_array($key, $no_inventory) && $stock <= 0)
    								$no_inventory[] = $key;
    				}
    
    				if (count($no_inventory) >= count($meta["mp_price"]))
    						$all_out = true;
    		}
    
           $terms = get_the_term_list($post_id, 'product_category', '', ',', '');
    
    		//display an external link or form button
            if(strpos($terms,$expire_category)) {
                $button = '';
            } else if (isset($meta['mp_product_link']) && $product_link = $meta['mp_product_link']) {
    
    				$button = '<a class="mp_link_buynow" href="' . esc_url($product_link) . '">' . __('Buy Now »', 'mp') . '</a>';
    		} else if ($mp->get_setting('disable_cart')) {
    
    				$button = '';
    		} else {
    				$variation_select = '';
    				$button = '<form class="mp_buy_form" method="post" action="' . mp_cart_link(false, true) . '">';
    
    				if ($all_out) {
    						$button .= '<span class="mp_no_stock">' . __('Out of Stock', 'mp') . '</span>';
    				} else {
    
    						$button .= '<input type="hidden" name="product_id" value="' . $post_id . '" />';
    
    						//create select list if more than one variation
    						if (is_array($meta["mp_price"]) && count($meta["mp_price"]) > 1 && empty($meta["mp_file"])) {
    								$variation_select = '<select class="mp_product_variations" name="variation">';
    								foreach ($meta["mp_price"] as $key => $value) {
    										$disabled = (in_array($key, $no_inventory)) ? ' disabled="disabled"' : '';
    										$variation_select .= '<option value="' . $key . '"' . $disabled . '>' . esc_html($meta["mp_var_name"][$key]) . ' - ';
    										if ($meta["mp_is_sale"] && $meta["mp_sale_price"][$key]) {
    												$variation_select .= $mp->format_currency('', $meta["mp_sale_price"][$key]);
    										} else {
    												$variation_select .= $mp->format_currency('', $value);
    										}
    										$variation_select .= "</option>\n";
    								}
    								$variation_select .= "</select> \n";
    						} else {
    								$button .= '<input type="hidden" name="variation" value="0" />';
    						}
    
    						if ($context == 'list') {
    								if ($variation_select) {
    										$button .= '<a class="mp_link_buynow" href="' . get_permalink($post_id) . '">' . __('Choose Option »', 'mp') . '</a>';
    								} else if ($mp->get_setting('list_button_type') == 'addcart') {
    										$button .= '<input type="hidden" name="action" value="mp-update-cart" />';
    										$button .= '<input class="mp_button_addcart" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />';
    								} else if ($mp->get_setting('list_button_type') == 'buynow') {
    										$button .= '<input class="mp_button_buynow" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />';
    								}
    						} else {
    
    								$button .= $variation_select;
    
    								//add quantity field if not downloadable
    								if ($mp->get_setting('show_quantity') && empty($meta["mp_file"])) {
    										$button .= '<span class="mp_quantity"><label>' . __('Quantity:', 'mp') . ' <input class="mp_quantity_field" type="text" size="1" name="quantity" value="1" /></label></span> ';
    								}
    
    								if ($mp->get_setting('product_button_type') == 'addcart') {
    										$button .= '<input type="hidden" name="action" value="mp-update-cart" />';
    										$button .= '<input class="mp_button_addcart" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />';
    								} else if ($mp->get_setting('product_button_type') == 'buynow') {
    										$button .= '<input class="mp_button_buynow" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />';
    								}
    						}
    				}
    
    				$button .= '</form>';
    		}
    
    		$button = apply_filters('mp_buy_button_tag', $button, $post_id, $context);
    
    		if ($echo)
    				echo $button;
    		else
    				return $button;
    }

    then upload this file to wp-content/mu-plugins

    If that doesn't work for you, then I will need FTP access in order to debug this on your site to see what's happening.

    Hope this helps

  • chris

    Hi Vaughan,

    That did the trick perfectly first time. Thanks you.

    It is the first time i have worked with multisite setup in wordpress. Can I check this is sensible practice to use the functions.php in mu-plugins folder.

    I understand this will be used by all sites, always so i don't see any issue.

    I was just wondering if it would be sensible to add the code in my custom plugin into the mu-plugins folder instead of in the functions.php file

    No sure how common it is to have to load files here in multisite but if it was likely to be required quite often then separate plugins might be easier to manage.

    Many thanks for this.
    Chris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.