添加Woocommerce临时产品 [英] Add Woocommerce Temporary product

查看:103
本文介绍了添加Woocommerce临时产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助: 是否可以在WooCommerce中将用户生成的临时产品添加到购物车中?我正在尝试将非wordpress网站转移到WordPress,但是该网站已经具有一个复杂的电子商务系统,客户端不希望更改.基本上发生的是,访问者指定客户销售的产品的度量,向其添加不同的变体,然后在提交后,网站根据访问者的输入生成产品的价格.添加产品将非常繁琐,因为它们有太多具有数千个变体的产品.因此,我们的解决方案是这样.除了WooCommerce,我还接受其他插件建议.我已经尝试过使用Gravity Forms,但是最终却陷入困境,因为将客户的当前网站添加到购物车后必须是一个电子商务网站.预先谢谢你!

Help Needed: Is it possible to make a user-generated temporary product to be added to cart in WooCommerce? I'm trying to transfer a non-wordpress website to WordPress, but the site already has a sophisticated e-commerce system that the client doesn't want to change. What basically happens is a visitor specifies the measurement of the product that the client is selling, add different variations to it, and then after submitting, the website generates the product's price based on the visitor's input. Adding products will be very tedious because they have too many products with thousands of variations. So our solution is this. I'm open to other plugin suggestions aside from WooCommerce. I already tried using Gravity Forms, but I got stuck in the end because our client's current website has to be an e-commerce site after it was added to the cart. Thank you in advance!

推荐答案

此问题已解决! 这是我做的,以防万一将来有人需要帮助.

This is solved! Here is what I did just in case someone needs help with this in the future.

首先,我用表格制作了一个页面.无论页面将驻留在哪个页面中,方法都会发布.

First I made a page with a form. Action is whichever the page will live in and method is post.

<form method="post" action="/order-page">

    <label for="tc_name" title="Full Name">Full Name</label>
    <input type="text" name="tc_name"/>

    <label for="tc_title" title="Product Name">Product Name</label>
    <input type="text" name="tc_title">

    <label for="tc_description" title="Product Description">Product Description</label>
    <textarea name="tc_description"></textarea>

    <label for="tc_price" title="Price">Price</label>
    <input type="number" name="tc_price"/>

    <input type="submit" value="Submit"/>
</form>

然后在下一页上,我获取了这些值,根据给定的值创建了产品,然后添加了一个短代码来显示购物车按钮.

Then on the next page, I grabbed the values, created the product based on the values given, and then added a shortcode to display the cart button.

if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['tc_price'])) {

    //grab the values
    $pName = $_POST["tc_name"];
    $pDescription = $_POST["tc_description"];
    $pPrice = $_POST["tc_price"];
    $post_title = $_POST["tc_title"];

    //add them in an array
    $post = array(
        'post_author' => $pName,
        'post_content' => $pDescription,
        'post_status' => "publish",
        'post_title' => $post_title,
        'post_type' => "product",
    );
    //create product
    $product_id = wp_insert_post( $post, __('Cannot create product', 'bones') );

    //type of product
    wp_set_object_terms($product_id, 'simple', 'product_type');

    //add price to the product, this is where you can add some descriptions such as sku's and measurements
    update_post_meta( $product_id, '_regular_price', $pPrice );
    update_post_meta( $product_id, '_sale_price', $pPrice );
    update_post_meta( $product_id, '_price', $pPrice );

    //get woocommerce shortcode for add to cart
    $myButton = do_shortcode('[add_to_cart id="' . $product_id . '"]');

    //display product
    echo $myButton;
}

然后,最后,一旦完成订单,通过将操作挂接到woocommerce_thankyou来删除产品.我把它放在函数中.

and then lastly, once an order is completed, delete the product by hooking an action to woocommerce_thankyou. I put it in the functions.

function mysite_completed($order_id) {
    //get order ID
    $order = new WC_Order( $order_id );

    //grab items from the order id
    $items = $order->get_items();

    //loop thru all products in the order section and get product ID 
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];

        //choose whatever suites you, trash the product is what I picked

        //permanently deletes product
        //wp_delete_post($product_id);

        //trashes post
        wp_trash_post($product_id);
    }
}

这篇关于添加Woocommerce临时产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆