使用页面模板从前端创建产品似乎有效,但没有产品创建为草稿

Creating Product From Frontend using Page Template Seem To Work But No Product Is Created as Draft

提问人:Learning WooStoreFront 提问时间:11/10/2023 更新时间:11/10/2023 访问量:35

问:

我再次尝试将一个页面模板放在一起,该模板将执行以下操作:

#1:将产品作为草稿提交 #2:向管理员发送电子邮件,请求批准(从草稿更改为发布) #3:提交成功后,重定向到“我的帐户”页面

所有字段都在那里,但没有产品显示在WP-admin的“产品”下。有人可以找出问题所在吗?

这是代码:

使用子主题的函数文件:

add_action( 'init', 'customer_creates_product_frontend' );
function customer_creates_product_frontend() {
    
        if ( isset( $_POST['submit_product'] ) && wp_verify_nonce( $_POST['product_nonce'], 'submit_product_form' ) ) {
            $errors = array();

            $product_title = sanitize_text_field( $_POST['product_title'] );
            if ( empty( $product_title ) ) {
                $errors[] = 'The title of the product is mandatory.';
            }

            $product_condition = sanitize_text_field( $_POST['product_condition'] );
            if ( empty( $product_condition ) ) {
                $errors[] = 'The condition of the product is mandatory.';
            }

            $starting_price = sanitize_text_field( $_POST['starting_price'] );
            if ( empty( $starting_price ) || ! is_numeric( $starting_price ) ) {
                $errors[] = 'A valid starting price of the product is mandatory.';
            }

            $reservation_price = sanitize_text_field( $_POST['reservation_price'] );
            if ( empty( $reservation_price ) || ! is_numeric( $reservation_price ) ) {
                $errors[] = 'The reserveration price of the product is mandatory.';
            }

            $increment_price = sanitize_text_field( $_POST['increment_price'] );
            if ( empty( $increment_price ) || ! is_numeric( $increment_price ) ) {
                $errors[] = 'The increment of how much each bid should increase the product price with is mandatory.';
            }

            $starting_sale_date = sanitize_text_field( $_POST['starting_sale_date'] );
            if ( empty( $starting_sale_date ) ) {
                $errors[] = 'Starting date for the product is mandatory.';
            }

            $end_sale_date = sanitize_text_field( $_POST['end_sale_date'] );
            if ( empty( $end_sale_date ) ) {
                $errors[] = 'End date for the product is mandatory.';
            }

            $buy_now_price = sanitize_text_field( $_POST['buy_now_price'] );
            if ( empty( $buy_now_price ) || ! is_numeric( $buy_now_price ) || $buy_now_price <= $starting_price) {
                $errors[] = 'A valid BUY NOW price for the product is mandatory, and it must be higher than the starting price.';
            }

            $short_description = sanitize_text_field( $_POST['short_description'] );
            if ( empty( $short_description ) ) {
                $errors[] = 'The short description for the product is mandatory.';
            }

            $long_description = sanitize_text_field( $_POST['long_description'] );
            if ( empty( $long_description ) ) {
                $errors[] = 'The long description for the product is mandatory.';
            }

            if ( empty( $errors ) ) {
                $new_product = wc_create_product( array (
                    'name' => $product_title,
                    'regular_price' => $starting_price,
                    'type' => 'auction', // set to "Auction" by the default - simple, grouped, externel, variable are also available
                    'description' => $short_description, // short desctiption
                    'short_description' => $long_description, // long description
                    'meta_input' => array (
                        'product_condition' => $product_condition,
                        'reservation_price' => $reservation_price,
                        'increment_price' => $increment_price,
                        'starting_sale_date' => date( 'Y-m-d \a\t H:i:s', strtotime( $starting_sale_date ) ),
                        'end_sale_date' => date( 'Y-m-d \a\t H:i:s', strtotime( $end_sale_date ) ),
                        'buy_now_price' => $buy_now_price,
                  )
             )
        );

                if ( is_wp_error( $new_product ) ) {
                    echo '<p class="text-danger">Error creating product. Please try again.</p>';
             
             } else {
     
        $the_customer = wp_get_current_user();
        $name_of_customer = $the_customer->get_billing_first_name();
        // send email to the admin with information about the request
        $admin_email = get_option( 'admin_email' );
        $subject = 'Approval Request from '.$name_of_customer.' who wants to publish '.$product_title.'';
        $message = 'The customer wants to publish '.$product_title.', with the following description: '.$short_description.'';
        wp_mail( $admin_email, $subject, $message );
    
    $new_product->set_status( 'draft' );
    wp_safe_redirect( home_url( '/mitt-konto/' ) );
        exit();
     
     }
     
       } else {
              
              foreach ( $errors as $error ) {
                    echo '<p class="text-danger">' . $error . '</p>';

          }
         }
    }
}

使用页面模板:

<?php
/**
* Template Name: Create Product
* @package Bootscore
*/
get_header();
?>
<div id="content" class="site-content">
<div id="primary" class="content-area">
<?php bs_after_primary(); ?>
<main id="main" class="site-main">
<div class="entry-content">
<br><br><br>

    <?php if ( is_user_logged_in() && in_array( 'administrator', wp_get_current_user()->roles ) ) { ?>

    <form method="post" enctype="multipart/form-data">
        <div class="mb-3">
            <label for="product_title" class="form-label">Product Name</label>
            <input type="text" id="product_title" name="product_title" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="product_condition" class="form-label">Product Condition</label>
            <select id="product_condition" name="product_condition" class="form-select" required>
                <option value="new">New</option>
                <option value="old">Old</option>
            </select>
        </div>

        <div class="mb-3">
            <label for="starting_price" class="form-label">Starting Price</label>
            <input type="number" id="starting_price" name="starting_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="reservation_price" class="form-label">Reservation Price</label>
            <input type="number" id="reservation_price" name="reservation_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="increment_price" class="form-label">Increment Price</label>
            <input type="number" id="increment_price" name="increment_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="starting_sale_date" class="form-label">Starting Sale Date</label>
            <input type="datetime-local" id="starting_sale_date" name="starting_sale_date" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="end_sale_date" class="form-label">End Sale Date</label>
            <input type="datetime-local" id="end_sale_date" name="end_sale_date" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="buy_now_price" class="form-label">Buy Now Price</label>
            <input type="number" id="buy_now_price" name="buy_now_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="short_description" class="form-label">Short Description</label>
            <textarea id="short_description" name="short_description" class="form-control" required></textarea>
        </div>

        <div class="mb-3">
            <label for="long_description" class="form-label">Long Description</label>
            <textarea id="long_description" name="long_description" class="form-control" required></textarea>
        </div>

        <button type="submit" name="submit_product" class="btn btn-primary">Create Product</button>
        <?php wp_nonce_field( 'submit_product_form', 'product_nonce' ); ?>

    </form>

<?php } ?>
</div>
</main>
</div></div>
<?php get_footer();
PHP WordPress 模板 WooCommerce 产品

评论

0赞 SeoSiteSoft 11/10/2023
在 $new_product->set_status( 'draft' 之后;把这个 $new_product->save();

答:

2赞 LoicTheAztec 11/10/2023 #1

更新

函数 wc_create_product() 不存在...从 WooCommerce 3 开始,您应该改用 CRUD 方法......

我已经用一个简单的产品(来自WC_Product_Simple类)测试了代码,它有效,所以你需要在下面的代码中,用正确的产品拍卖类(类似于 WC_Product_Auction)替换 2 次:WC_Product_Simple

add_action( 'init', 'customer_creates_product_frontend' );
function customer_creates_product_frontend() { 
    $errors = array();

    if ( isset( $_POST['submit_product'] ) && wp_verify_nonce( $_POST['product_nonce'], 'submit_product_form' ) ) {

        $product_title = sanitize_text_field( $_POST['product_title'] );
        if ( empty( $product_title ) ) {
            $errors[] = 'The title of the product is mandatory.';
        }

        $product_condition = sanitize_text_field( $_POST['product_condition'] );
        if ( empty( $product_condition ) ) {
            $errors[] = 'The condition of the product is mandatory.';
        }

        $starting_price = sanitize_text_field( $_POST['starting_price'] );
        if ( empty( $starting_price ) || ! is_numeric( $starting_price ) ) {
            $errors[] = 'A valid starting price of the product is mandatory.';
        }

        $reservation_price = sanitize_text_field( $_POST['reservation_price'] );
        if ( empty( $reservation_price ) || ! is_numeric( $reservation_price ) ) {
            $errors[] = 'The reserveration price of the product is mandatory.';
        }

        $increment_price = sanitize_text_field( $_POST['increment_price'] );
        if ( empty( $increment_price ) || ! is_numeric( $increment_price ) ) {
            $errors[] = 'The increment of how much each bid should increase the product price with is mandatory.';
        }

        $starting_sale_date = sanitize_text_field( $_POST['starting_sale_date'] );
        if ( empty( $starting_sale_date ) ) {
            $errors[] = 'Starting date for the product is mandatory.';
        }

        $end_sale_date = sanitize_text_field( $_POST['end_sale_date'] );
        if ( empty( $end_sale_date ) ) {
            $errors[] = 'End date for the product is mandatory.';
        }

        $buy_now_price = sanitize_text_field( $_POST['buy_now_price'] );
        if ( empty( $buy_now_price ) || ! is_numeric( $buy_now_price ) || $buy_now_price <= $starting_price) {
            $errors[] = 'A valid BUY NOW price for the product is mandatory, and it must be higher than the starting price.';
        }

        $short_description = sanitize_text_field( $_POST['short_description'] );
        if ( empty( $short_description ) ) {
            $errors[] = 'The short description for the product is mandatory.';
        }

        $long_description = sanitize_text_field( $_POST['long_description'] );
        if ( empty( $long_description ) ) {
            $errors[] = 'The long description for the product is mandatory.';
        }

        if ( count($errors) == 0 && class_exists('WC_Product_Simple') ) {
            $product = new WC_Product_Simple(); // Create an empty product object (type "auction")
            $product->set_name($product_title);
            $product->set_regular_price($starting_price);
            $product->set_price($starting_price);
            $product->set_short_description($short_description);
            $product->set_description($long_description);
            $product->add_meta_data('product_condition', $product_condition, true);
            $product->add_meta_data('reservation_price', $reservation_price, true);
            $product->add_meta_data('increment_price', $increment_price, true);
            $product->add_meta_data('starting_sale_date', date( 'Y-m-d \a\t H:i:s', strtotime( $starting_sale_date ) ), true);
            $product->add_meta_data('end_sale_date', date( 'Y-m-d \a\t H:i:s', strtotime( $end_sale_date ) ), true);
            $product->add_meta_data('buy_now_price', $buy_now_price, true);
            $product->set_status('draft');
            $product_id = $product->save();
        }

        if ( ! $product_id ) {
            echo '<p class="text-danger">Error creating product. Please try again.</p>';
        } else {
            global $current_user;

            $first_name  = $current_user->billing_first_name;
            $admin_email = get_option( 'admin_email' );
            $subject     = 'Approval Request from '.$first_name.' who wants to publish '.$product_title.'';
            $message     = 'The customer wants to publish '.$product_title.', with the following short description: '.$short_description.'';
            
            // Send email to the admin with information about the request
            wp_mail( $admin_email, $subject, $message );
            // Redirect User to My account
            wp_safe_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
            exit();
        }
    } elseif ( count($errors) > 0 ) {
        foreach ( $errors as $error ) {
            echo '<p class="text-danger">' . $error . '</p>';
        }
    }
}

代码进入子主题的 functions.php 文件(或插件)。经过测试并工作。

模板文件:

/**
 * The template for displaying in frontend the product Auction creation form.
 *
 * Template name: Create Product
 *
 * @package Bootscore
 */

global $current_user;

get_header(); ?>

<div id="content" class="site-content">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div class="entry-content">
<br><br><br>

    <?php if ( is_a($current_user, 'WP_User') && in_array( 'administrator', $current_user->roles ) ) { ?>

    <form method="post" enctype="multipart/form-data">
        <div class="mb-3">
            <label for="product_title" class="form-label"><?php _e('Product Name'); ?></label>
            <input type="text" id="product_title" name="product_title" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="product_condition" class="form-label"><?php _e('Product Condition'); ?></label>
            <select id="product_condition" name="product_condition" class="form-select" required>
                <option value="new"><?php _e('New'); ?></option>
                <option value="old"><?php _e('Old'); ?></option>
            </select>
        </div>

        <div class="mb-3">
            <label for="starting_price" class="form-label"><?php _e('Starting Price'); ?></label>
            <input type="number" id="starting_price" name="starting_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="reservation_price" class="form-label"><?php _e('Reservation Price'); ?></label>
            <input type="number" id="reservation_price" name="reservation_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="increment_price" class="form-label"><?php _e('Increment Price'); ?></label>
            <input type="number" id="increment_price" name="increment_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="starting_sale_date" class="form-label"><?php _e('Starting Sale Date'); ?></label>
            <input type="datetime-local" id="starting_sale_date" name="starting_sale_date" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="end_sale_date" class="form-label"><?php _e('End Sale Date'); ?></label>
            <input type="datetime-local" id="end_sale_date" name="end_sale_date" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="buy_now_price" class="form-label"><?php _e('Buy Now Price'); ?></label>
            <input type="number" id="buy_now_price" name="buy_now_price" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="short_description" class="form-label"><?php _e('Short Description'); ?></label>
            <textarea id="short_description" name="short_description" class="form-control" required></textarea>
        </div>

        <div class="mb-3">
            <label for="long_description" class="form-label"><?php _e('Long Description'); ?></label>
            <textarea id="long_description" name="long_description" class="form-control" required></textarea>
        </div>

        <button type="submit" name="submit_product" class="btn btn-primary"><?php _e('Create Product'); ?></button>
        <?php wp_nonce_field( 'submit_product_form', 'product_nonce' ); ?>

    </form>

<?php } ?>
</div>
</main>
</div>
</div>
<?php
get_footer();

相关新闻: 在 Woocommerce 3 中使用 CRUD 方法以编程方式创建产品

评论

1赞 Learning WooStoreFront 11/10/2023
是的,该类存在于第三方插件(来自 WooCommerce)中。我将测试您的代码,看看这是否会有所不同。感谢您抽出宝贵时间。
0赞 Learning WooStoreFront 11/10/2023
使用您的代码并让模板保持不变,我从 WordPress 得到“白屏”,说填写表单并单击提交后出现严重错误。将打开调试并检查。
0赞 LoicTheAztec 11/10/2023
@LearningWooStoreFront 我已经更新了我的代码,使用一个简单的产品进行测试,并且它运行良好......因此,您必须在我的代码中将两次出现的情况替换为正确的产品拍卖类...WC_Product_Simple
0赞 Learning WooStoreFront 11/11/2023
好的,我会测试它。感谢您抽出宝贵时间进行测试和发布。