【问题标题】:Add Woocommerce Temporary product添加 Woocommerce 临时产品
【发布时间】:2019-04-13 19:21:13
【问题描述】:

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

【问题讨论】:

    标签: wordpress woocommerce product variations


    【解决方案1】:

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

    首先我用表格制作了一个页面。操作是页面将存在的任何一个,方法是发布。

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

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

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

    【讨论】:

    • 在哪里可以自动丢弃使用您粘贴上述代码创建的产品的代码,我的意思是function mysite_comepleted($order_id),或者您在订单完成后丢弃您网站上的每件产品?
    • 如果用户在购物车中并且他们删除了购物车中的产品,那么它会被自动删除。
    • 好主意,但这仍然适用于网站上的每个产品,是否可以具体说明?例如,如果产品属于某个类别并且已下订单(这会从用户购物车中删除产品),那么该产品可能会被丢弃
    • 是的,我也遇到了这个问题。我的脑海中没有代码,但我能够创建一个产品,将其放入自定义类别,然后只有当产品属于该特定类别时才会被删除。
    • 请您分享代码...这就是我的问题的设置方式...我有一个自定义类别,我希望在订购时自动删除/删除该类别中的产品完成了。谢谢
    【解决方案2】:

    在成功创建订单后扩展此主题并从类别中删除或删除产品。

    首先,您需要创建一个自定义类别,您希望在成功创建订单后删除或删除的所有产品都会在该类别中有效。然后添加以下代码;

    function delete_product_on_woocommerce_complete_order( $order_id ) { 
    
        if ( ! $order_id ) {
            return;
        }
    
        // 1. Get order object
        $order = wc_get_order( $order_id );
    
        // 2. Initialize $cat_in_order variable
        $cat_in_order = false;
    
        foreach ( $items as $item ) {       
            $product_id = $item['product_id'];  
            if ( has_term( 'machine', 'product_cat', $product_id ) ) { //Where machine is the custom product category slug
    
                // 3. choose whatever suites you, delete the product is what I picked
                //To Delete Product
                wp_delete_post($product_id); 
    
                //To Trash Product
                //wp_trash_post($product_id); 
            }
        }   
    
    }
    add_action( 'woocommerce_thankyou', 'delete_product_on_woocommerce_complete_order', 5 );
    

    代码进入您的活动子主题(或活动主题)或 cutsom 插件的 function.php 文件中。经过测试和工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2022-01-05
      • 2015-03-31
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多