【问题标题】:Woocommerce Add Customizable Product to CartWoocommerce 将可定制的产品添加到购物车
【发布时间】:2015-06-16 17:16:17
【问题描述】:

我正在尝试将可定制的产品添加到 woocommerce 购物车。

我已计算好所有详细信息并准备好将其添加到购物车中。

查看 woocommerce api,看起来我可以使用 REST,但我只是认为常规 php 必须有一种更简单的方法。

我在想这样的事情:

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

我完全不在了吗?或者我该怎么做?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    是的,您可以使用WC_Cart 对象上可用的add_to_cart() 方法。您的示例大部分是正确的,但是您需要提供数字 $product_id$variation_id

    最好使用WC() 而不是global $woocommerce 访问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 
    );
    

    【讨论】:

    • 但是如果产品是通过定制器创建的可定制产品怎么办 - 所以事先没有产品 ID。然后我应该在同一个实例中创建产品吗?
    • 总有一个产品 ID,你必须先在 WooCommerce 中创建变量产品。您也可以通过编程方式完成此操作,但仍需要先创建它,以便您可以使用 ID 将其添加到您的购物车中。
    【解决方案2】:

    这对我有用 - 插入 functions.php 并通过 html 表单引用此函数:

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

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 2021-08-22
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      相关资源
      最近更新 更多