Home / Admin / Product Expiry For Woo
Duplicate Snippet

Embed Snippet on Your Site

Product Expiry For Woo

Supports the Expiry for Woocommerce plugin - WC Vendor

Code Preview
php
<?php
/**
 * Add support for Product Expiry for WooCommerce Plugin 
 */
 if ( class_exists( 'WOO_Product_Expiry') ){ 
	/**
	 * Add fields to the product edit form under prices 
	 */
	function wcv_pefwc_add_fields( $post_id ){ 
		// Expiry date field.
		$expiry_date = get_post_meta( $post_id, 'woo_expiry_date', true );
		WCVendors_Pro_Form_Helper::input( 
			array( 
				'post_id'           => $post_id, 
				'id'                => 'woo_expiry_date', 
				'class'             => 'wcv-datepicker wcv-init-picker',
				'label'             => __( 'Expiry Date', 'wcvendors-pro' ),
				'value'             => $expiry_date, 
				'custom_attributes' => array(
					'data-start-date' => '',
					'data-close-text' => __( 'Close', 'wcvendors-pro' ),
					'data-clean-text' => __( 'Clear', 'wcvendors-pro' ),
					'data-of-text'    => __( ' of ', 'wcvendors-pro' ),
				),
			)
		);
		// Expiry date action.
		$expiry_action = get_post_meta( $post_id, 'woo_expiry_action', true );
		WCVendors_Pro_Form_Helper::select( 
			array(
				'id'        => 'woo_expiry_action',
				'label'     => __( 'Action', 'product-expiry-for-woocommerce' ),
				'options'   => array(
					'' => __( 'Nothing', 'product-expiry-for-woocommerce' ),
					'draft' => __( 'Make it Draft', 'product-expiry-for-woocommerce' ),
				),
				'desc_tip'  => __( 'What to do when this product expires?', 'product-expiry-for-woocommerce' ),
				'description'  => __( 'What to do when this product expires?', 'product-expiry-for-woocommerce' )
				)
		);
	}
	add_action( 'wcvendors_product_options_general_product_data_after', 'wcv_pefwc_add_fields' );
	
	/**
	 * Save the product fields
	 *
	 * @param int $post_id
	 * @return void
	 */
	function wcv_pefwc_save_fields( $post_id ){
		$product = wc_get_product( $post_id );
        // Save the woo_expiry_date setting
        $woo_expiry_date = isset( $_POST['woo_expiry_date'] ) ? sanitize_text_field( $_POST[ 'woo_expiry_date' ] ) : '';
        $woo_expiry_action = isset( $_POST['woo_expiry_action'] ) ? sanitize_text_field( $_POST[ 'woo_expiry_action' ] ) : '';
        $product->update_meta_data( 'woo_expiry_date', sanitize_text_field( $woo_expiry_date ) );
        $product->update_meta_data( 'woo_expiry_action', sanitize_text_field( $woo_expiry_action ) );
        if ($woo_expiry_date != '' && $woo_expiry_action != '') {
            wp_clear_scheduled_hook( 'woo_expiry_schedule_action', array($post_id) );
            wp_schedule_single_event( strtotime($woo_expiry_date), 'woo_expiry_schedule_action', array($post_id) );
        }
        $product->save();
	}
	add_action ('wcv_save_product','wcv_pefwc_save_fields');
	/**
	 * Show the expiry date on the product listing page
	 */
	function wcv_pefwc_show_date( $row_status, $status, $product_type, $date, $stock_status, $product ){ 
		$expiry_date = get_post_meta( $product->get_id(), 'woo_expiry_date', true );
		// Only show the expiry if it has one. 
		if ( !empty( $expiry_date ) ){ 
			$row_status = sprintf(
				'<span class="status %s">%s</span><br />
				<span class="product_type %s">%s</span><br />
				<span class="product_date">%s</span><br />
				<span class="product_expiry"><strong>'.__( 'Expires: ', 'wcvendors-pro' ) .'%s</strong></span><br />
				<span class="stock_status">%s</span>',
				$status,
				$status,
				$product_type,
				$product_type,
				$date,
				date_i18n( get_option( 'date_format' ), strtotime( $expiry_date ) ),
				$stock_status
			); 
		}
		return $row_status;
	}
	add_filter( 'wcv_product_row_status', 'wcv_pefwc_show_date', 10, 6 );
 }

Comments

Add a Comment