dongnuoyi8833 2015-06-16 17:16
浏览 161
已采纳

Woocommerce将可定制产品添加到购物车

I'm trying to add a customizable product to the woocommerce cart.

I have all details calculated and ready to be able to add it to the cart.

Looking into the woocommerce api, it looks like I can use REST, but I'm just thinking there must be an easier way with regular php.

I'm thinking something like this:

function add_product_to_wc(){
                                        global $woocommerce;

                                        $product_id = $name;
                                        $variationid = $type;
                                        $spec = array();
                                        $spec['Dimension'] = $dimension; //user select Dimension
                                        $spec['ColorOne'] = $colorOne; //user select color 1
                                        $spec['ColorTwo'] = $colorTwo; //user select color 2

                                        $woocommerce->cart->add_to_cart( $product_id, $variationid, $spec, null );}

Am I totally off ? or how can I do this ?

  • 写回答

2条回答 默认 最新

  • dqys98341 2015-06-17 13:05
    关注

    This worked for me - inserted in functions.php and the reference to this function via a html form:

    add_action('wp_loaded', 'customcart');
    
    
    function customcart() {
    
      if (isset($_POST["addcustomcarts"])) {
    
        global $woocommerce;
    
        $my_post = array(
          'post_title'    => $_POST["textInput"],
          'post_content'  => 'This is my post.',
          'post_status'   => 'publish',
          'post_author'   => 1,
          'post_type'     =>'product'
        );
    
        // Insert the post into the database
        $product_ID = wp_insert_post( $my_post );
    
        if ( $product_ID ){
          wp_set_object_terms( $product_ID, 'design-selv-skilte', 'product_cat' );
          add_post_meta($product_ID, '_regular_price', 100 );
          add_post_meta($product_ID, '_price', 100 );
          add_post_meta($product_ID, '_stock_status', 'instock' );
          add_post_meta($product_ID, '_sku', 'designselvskilt' );    
          add_post_meta($product_ID, '_visibility', 'hidden' );
          //wp_set_object_terms( $product_ID, 'tekst på mit skilt', text1, False );
    
          $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
    
          exit( wp_redirect( '/kurv' )  );
    
        }
    
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?