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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭