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

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 ?

我正在尝试将可自定义的产品添加到woocommerce购物车中。 p>

我已计算好所有细节,并准备将其添加到购物车中。 p>

查看woocommerce api,看起来我可以 使用REST,但我只是认为必须有一个更简单的常规PHP方式。 p>

我在想这样的事情: p>

  function add_product_to_wc(){
 global $ woocommerce; 
 
 $ product_id  = $ name; 
 $ variationid = $ type; 
 $ spec = array(); 
 $ spec ['Dimension'] = $ dimension;  //用户选择Dimension 
 $ spec ['ColorOne'] = $ colorOne;  //用户选择颜色1 
 $ spec ['ColorTwo'] = $ colorTwo;  //用户选择颜色2 
 
 $ woocommerce-> cart-> add_to_cart($ product_id,$ variationid,$ spec,null);} 
  code>  pre> 
 
 

我完全离开了吗? 或者我该怎么做? p> div>

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

    }

  }

}

Yes, you can use the add_to_cart() method available on the WC_Cart object. Your example is mostly correct, however you would need to provide the numeric $product_id and $variation_id.

It is also preferable to access the WooCommerce object using WC() rather than the global $woocommerce.

$product_id = 123; // use the real product ID
$variation_id = 456; // use the real variation ID
$dimension = 'Large';
$colorOne = 'Color One';
$colorTwo = 'Color Two';

WC()->cart->add_to_cart( 
    $product_id, 
    $variation_id, 
    array( 'Dimension'=>$dimension, 'ColorOne'=>$colorOne, 'ColorTwo'=>$colorTwo ), 
    null 
);