Sell any custom post type integrating with MarketPress. Requires MarketPress
<?php | |
/* | |
Plugin Name: CPT MarketPress integration | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Sell any custom post type integrating with MarketPress. Requires MarketPress | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if( ! class_exists( 'WPMUDEV_CPT_MP_integration' ) ){ | |
class WPMUDEV_CPT_MP_integration | |
{ | |
private static $_instance = null; | |
public static $post_type; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_CPT_MP_integration(); | |
} | |
return self::$_instance; | |
} | |
private function __construct(){ | |
self::$post_type = apply_filters( 'WPMUDEV_CPT_MP_integration/post_type', 'book' ); | |
add_action( 'init', array( $this, 'create_cpt' ), 10 ); | |
add_action( 'add_meta_boxes_' . self::$post_type, array( $this, 'cpt_meta_boxes' ), 10 ); | |
add_action( 'save_post', array( $this, 'update_product' ), 10, 1 ); | |
add_filter( 'the_content', array( $this , 'add_to_cart'), 10 ); | |
} | |
public function create_cpt(){ | |
$labels = apply_filters( 'WPMUDEV_CPT_MP_integration/cpt_label', array( | |
'name' => _x( 'Books', 'Books post type general name', 'wpmudev-books' ), | |
'singular_name' => _x( 'Book', 'Book Type post type singular name', 'wpmudev-mpi' ), | |
'add_new' => _x( 'Add New', 'Book', 'wpmudev-mpi' ), | |
'add_new_item' => __( 'Add New Book', 'wpmudev-mpi' ), | |
'edit_item' => __( 'Edit Book', 'wpmudev-mpi' ), | |
'new_item' => __( 'New Book', 'wpmudev-mpi' ), | |
'all_items' => __( 'All Books', 'wpmudev-mpi' ), | |
'view_item' => __( 'View Book', 'wpmudev-mpi' ), | |
'search_items' => __( 'Search Books', 'wpmudev-mpi' ), | |
'not_found' => __( 'No Books found', 'wpmudev-mpi' ), | |
'not_found_in_trash' => __( 'No Books found in the Trash', 'wpmudev-mpi' ), | |
'parent_item_colon' => '', | |
'menu_name' => __( 'Books' , 'wpmudev-mpi' ) | |
) | |
); | |
$supports = apply_filters( 'WPMUDEV_CPT_MP_integration/cpt_supports', array( | |
'title', 'editor', 'thumbnail', 'excerpt' | |
) | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => __('Custom post type.', 'wpmudev-mpi'), | |
'public' => true, | |
'menu_position' => 5, | |
'supports' => $supports, | |
'has_archive' => true, | |
'hierarchical' => false, | |
'taxonomies' => array('post_tag'), | |
'menu_icon'=> 'dashicons-admin-page', | |
'capability_type' => 'post' | |
); | |
register_post_type( self::$post_type, $args ); | |
} | |
public function cpt_meta_boxes( $post ){ | |
add_meta_box( | |
'wpmudev-mpi-details-metabox', | |
'<div class="wpmudev-mpi-heading">' .__( 'Book Price', 'wpmudev-mpi' ).'</div>', | |
array( $this, 'cpt_price_metabox' ), | |
$post->post_type, | |
'normal', | |
'high' | |
); | |
} | |
public function cpt_price_metabox( $post ){ | |
$price = (float)get_post_meta( $post->ID, 'price', true ); | |
wp_nonce_field( 'wpmudev_mpi_meta_box_nonce', 'mpi_meta_box_nonce' ); | |
?> | |
<p> | |
<label>Price:</label> | |
<input type="number" step="0.1" name="price" value="<?php echo $price; ?>" /> $ | |
</p> | |
<?php | |
} | |
public function update_product( $post_id ){ | |
if ( wp_is_post_revision( $post_id ) ){ | |
return; | |
} | |
$post = get_post( $post_id ); | |
if( empty( $post ) || $post->post_type != self::$post_type ){ | |
return; | |
} | |
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; | |
if( !isset( $_POST['mpi_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['mpi_meta_box_nonce'], 'wpmudev_mpi_meta_box_nonce' ) ) return; | |
if( !current_user_can( 'edit_post', $post_id ) ) return; | |
global $wpdb; | |
$price = (float)$_POST['price']; | |
update_post_meta( $post_id, 'price', $price ); | |
if( ! class_exists( 'MP_Product' ) ){ | |
return; | |
} | |
$product_id = get_post_meta( $post_id, 'product_id', true ); | |
$product = array( | |
'post_title' => $post->post_title, | |
'post_content' => $post->post_content, | |
'post_excerpt' => $post->post_excerpt, | |
'post_type' => MP_Product::get_post_type(), | |
'ping_status' => 'closed', | |
'comment_status' => 'closed', | |
'post_parent' => $post->ID, | |
'post_status' => $post->post_status, | |
); | |
$formats = array( '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', ); | |
if( empty( $product_id ) ){ | |
$product_id = wp_insert_post( $product ); | |
//Assign product id to book | |
update_post_meta( $post_id, 'product_id', $product_id ); | |
} | |
else{ | |
$product['ID'] = $product_id; | |
wp_update_post( $product ); | |
} | |
update_post_meta( $product_id, 'regular_price', $price ); | |
self::update_product_thumbnail( $product_id, $post->ID ); | |
} | |
public static function update_product_thumbnail( $product_id, $book_id = 0 ) { | |
$thumbnail_id = get_post_thumbnail_id( $product_id ); | |
if ( ! empty( $thumbnail_id ) ) { | |
return; | |
} | |
/** | |
* Check is set cpt? | |
*/ | |
if ( empty( $book_id ) ) { | |
$book_id = wp_get_post_parent_id( $product_id ); | |
} | |
if ( empty( $book_id ) ) { | |
return; | |
} | |
/** | |
* Only works if the cpt actually has a thumbnail. | |
*/ | |
$thumbnail_url = get_post_meta( $book_id, 'cp_listing_image', true ); | |
if ( empty( $thumbnail_url ) ) { | |
return; | |
} | |
/** | |
* Get thumbnail id from thumbnail_url, if it is custom image, do not | |
* set thumbnail for product. | |
*/ | |
global $wpdb; | |
$thumbnail_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $thumbnail_url ) ); | |
if ( empty( $thumbnail_id ) ) { | |
return; | |
} | |
/** | |
* Finally ... set product thumbnail. | |
*/ | |
set_post_thumbnail( $product_id, $thumbnail_id ); | |
$mp_product_images = explode( ',', get_post_meta( $product_id, 'mp_product_images', true ) ); | |
array_unshift( $mp_product_images, $thumbnail_id ); | |
$mp_product_images = implode( ',', array_filter( array_unique( $mp_product_images ) ) ); | |
update_post_meta( $product_id, 'mp_product_images', $mp_product_images ); | |
} | |
public function add_to_cart( $content ){ | |
global $post; | |
if( ! isset( $post->post_type ) || $post->post_type != self::$post_type ){ | |
return $content; | |
} | |
$product_id = get_post_meta( $post->ID, 'product_id', true ); | |
if( empty( $product_id ) ){ | |
return $content; | |
} | |
$shortcode = sprintf( '[mp_buy_button product_id="%s"]', $product_id ); | |
return $content . '<div class="wpmudev-mpi add_to_cart">' . do_shortcode( $shortcode ) . '</div>'; | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_CPT_MP_integration'] = WPMUDEV_CPT_MP_integration::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment