首页 文章

添加Woocommerce临时产品

提问于
浏览
2

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

1 回答

  • 3

    这解决了!这是我做的,以防将来有人需要帮助 .

    首先,我创建了一个带有表单的页面 . 动作是页面将存在的任何一个,方法发布 .

    <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>
    

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

    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来删除产品 . 我把它放在功能中 .

    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);
        }
    }
    

相关问题