Good day! Save, help! The button is there but it does not work, the page refreshes, but nothing drops into the cart. And you need to immediately transfer Paypal to pay when you click it, but it does not even fall into the basket. In general, you need to do so that Woocommerce perceived my CPT "Films" as products, but I do not know how. Is there any way to do this without linking CPT and Woocommerce Product? Here is the listing of the plugin itself.

<?php /* Plugin Name: Films Description: Declares a plugin that will create a custom post type displaying films. Version: 1.0 Author: Kolomiets Igor */ do_action( 'woocommerce_register_post_type' ); add_action( 'init', 'create_film' ); function create_film() { register_post_type( 'films', array( 'labels' => array( 'name' => 'Films', 'singular_name' => 'Film', 'add_new' => 'Add New Film', 'add_new_item' => 'Add New Film', 'edit' => 'Edit', 'edit_item' => 'Edit Film', 'new_item' => 'New Film', 'view' => 'View', 'view_item' => 'View Film', 'search_items' => 'Search Film', 'not_found' => 'No Film found', 'not_found_in_trash' => 'No Film found in Trash', 'parent' => 'Parent Film' ), 'public' => true, 'menu_position' => 15, 'supports' => array( 'title', 'editor', 'thumbnail'), 'taxonomies' => array( 'category' ), 'menu_icon' => plugins_url( 'images/image.png', __FILE__ ), 'has_archive' => true ) ); } add_action( 'admin_init', 'my_admin' ); function my_admin() { add_meta_box( 'film_meta_box', 'Custom attributes', 'display_film_meta_box', 'films', 'normal', 'high' ); } function display_film_meta_box( $film ) { // Retrieve current name of the Director and Movie Rating based on review ID $subtitle = esc_html( get_post_meta( $film->ID, 'subtitle', true ) ); $_price = esc_html( get_post_meta( $film->ID, '_price', true ) ); ?> <table> <tr> <td style="width: 100%">Subtitle</td> <td><input type="text" size="70" name="subtitle" value="<?php echo $subtitle; ?>" /></td> </tr> <tr> <td style="width: 100%">Price</td> <td><input type="text" size="40" name="_price" value="<?php echo $_price; ?>" /></td> </tr> </table> <?php } add_action( 'save_post', 'add_film_fields', 10, 2 ); function add_film_fields( $movie_review_id, $movie_review ) { // Check post type for movie reviews var_dump($movie_review->post_type); if ( $movie_review->post_type == 'films' ) { // Store data in post meta table if present in post data if ( isset( $_POST['subtitle'] ) && $_POST['subtitle'] != '' ) { update_post_meta( $movie_review_id, 'subtitle', $_POST['subtitle'] ); } if ( isset( $_POST['product_id'] ) && $_POST['product_id'] != '' ) { update_post_meta( $movie_review_id, 'product_id', $_POST['product_id'] ); } if ( isset( $_POST['_price'] ) && $_POST['_price'] != '' ) { update_post_meta( $movie_review_id, '_price', $_POST['_price'] ); } } } add_filter('woocommerce_get_price','reigel_woocommerce_get_price',20,2); function reigel_woocommerce_get_price($price,$post){ if ($post->post->post_type === 'films') $price = get_post_meta($post->id, '_price', true); return $price; } add_filter('the_content','rei_add_to_cart_button', 20,1); function rei_add_to_cart_button($content){ global $post; if ($post->post_type !== 'films') {return $content; } ob_start(); ?> <form action="" method="post"> <input name="add-to-cart" type="hidden" value="<?php echo $post->ID ?>" /> <input name="quantity" type="number" value="1" min="1" /> <input name="submit" type="submit" value="Add to cart" /> </form> <?php return $content . ob_get_clean(); } ?> 

Here is the listing template

 <?php /*Template Name: New Template */ get_header(); ?> <div class="wrap"> <div id="content" role="main"> <?php $mypost = array( 'post_type' => 'films', ); $loop = new WP_Query( $mypost ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post();?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header" style="text-align:center"> <!-- Display featured image in right-aligned floating div --> <div style="float: right; margin: 10px"> <?php the_post_thumbnail(); ?> </div> <!-- Display Title and Author Name --> <h1><?php the_title(); ?></h1> <h4><?php echo esc_html( get_post_meta( get_the_ID(), 'subtitle', true ) ); ?></h4> Price: <?php echo esc_html( get_post_meta( get_the_ID(), '_price', true ) ); ?> <form action="" method="post"> <input name="add-to-cart" type="hidden" value="<?php echo $post->ID ?>" /> <input name="quantity" type="number" value="1" min="1" /> <input name="submit" type="submit" value="Add to cart" /> </form> <? var_dump(get_button_code_for_product(get_the_ID())); ?> <br /> </header> <!-- Display movie review contents --> <div class="entry-content"><?php the_content(); ?></div> </article> <?php endwhile; ?> </div> </div> <?php wp_reset_query(); ?> <?php get_footer(); ?> 

    1 answer 1

    WooCommerce uses only one custom post type - product .

    So you can’t slip a post with the type of films your store. Most likely, any attempts to cross the WC and CPT functionality are doomed to failure.

    • That is, you need to separately make a new Post and a new Product and link them? - Igor Kolomiets
    • I do not see how to tie them. - KAGG Design
    • on meteo. Here I get the product to which $ product = get_product is attached (get_post_meta (get_the_ID (), 'product_id', true)); but the buy button <? echo "<button> <a href = '". $ product-> add_to_cart_url (). "'style =' color: #FFF '> Buy in one click </a> </ button>";?> Such a crutch - Igor Kolomiets
    • Well, that’s possible - then you have two entities: a post in a CPT and a post in a WC. I meant that I did not see the possibility of combining the functions of two plug-ins in one entity (one record of a custom type). - KAGG Design
    • Thanks for the help - Igor Kolomiets