簡體   English   中英

WooCommerce 添加到購物車動態創建的產品變體

[英]WooCommerce add to cart dynamic created product variation

我正在嘗試使用此 function 將產品添加到我的購物車: WC()->cart->add_to_cart( $id, $quantity=1,$variation_id ); 但是,它僅在購物車為空時有效。 如果購物車中已有產品,則不會添加新產品。 出於測試目的,我正在使用這樣的表單按鈕:

<form name="addpro" method="post" action="">
    <input type="submit" name="addcustomcarts" value="ADD TOO CART">
</form>

這是我將產品添加到購物車的代碼:

add_action('init', 'customcart');
function customcart() {
    if (isset($_POST["addcustomcarts"])) {
        // global $woocommerce;

        //Create main product
        $product = new WC_Product_Variable();
        $product->set_name("Tdwo");

        //Create the attribute object
        $attribute = new WC_Product_Attribute();

        //pa_size tax id
        $attribute->set_id( 0 ); // -> SET to 0

        //pa_size slug
        $attribute->set_name( 'size' ); // -> removed 'pa_' prefix

        //Set terms slugs
        $attribute->set_options( array(
            'blue',
            'grey'
        ) );

        $attribute->set_position( 0 );

        //If enabled
        $attribute->set_visible( 1 );

        //If we are going to use attribute in order to generate variations
        $attribute->set_variation( 1 );
        $product->set_attributes(array($attribute));

        //Save main product to get its id
        $id = $product->save();

        $variation = new WC_Product_Variation();
        $variation->set_regular_price(10);
        $variation->set_parent_id($id);

        //Set attributes requires a key/value containing
        // tax and term slug
        $variation->set_attributes(array(
            'size' => 'blue' // -> removed 'pa_' prefix
        ));

        //Save variation, returns variation id
        $variation_id = $variation->save() ;
        echo $variation_id;

        // echo get_permalink( $id ); // -> returns a link to check the newly created product

        WC()->cart->add_to_cart( $id, $quantity=1,$variation_id );

        exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
    }
}

嘗試以下操作(清理一下您的代碼並將屬性數組添加到add_to_cart()方法)

add_action('init', 'customcart');
function customcart() {
    if (isset($_POST["addcustomcarts"])) {
        ## -- Settings -- ##
        $variable_product_name = "Tdwo"; // Main variable product name
        $attribute_name_slug   = 'size'; // Define attriburte name (slug) to be used for variations
        $attribute_terms_slugs  = array( 'blue', 'grey'); // Define attriburte term slugs to be used with variations

        // Get an empty instance of the WC_Product_Variable Object
        $product = new WC_Product_Variable(); 
        
        $product->set_name($variable_product_name);

        // Get an empty instance of the WC_Product_Attribute Object
        $attribute      = new WC_Product_Attribute();
        
        $attribute->set_id( 0 ); // Set attribute Id (First, set to 0)
        $attribute->set_name( $attribute_name_slug ); // Set attribute name
        $attribute->set_options( $attribute_terms_slugs ); // Set attribute term slugs to be used with variations
        $attribute->set_position( 0 ); // Position: First one, set to 0
        $attribute->set_visible( 1 ); // Visible on product page
        $attribute->set_variation( 1 ); // Used for variations
        
        $product->set_attributes( array($attribute) );

        // Save main product to get its id
        $product_id = $product->save();
        
        // --------------------------------
        
        ## -- Settings -- ##
        $chosen_term_slug = 'blue'; // Set chosen attribute term slug for the current variation
        $variation_price  = 10; // The variation regular price

        $variation = new WC_Product_Variation();
        
        $variation->set_parent_id( $product_id ); 
        $variation->set_regular_price( $variation_price );
        $variation->set_price( $variation_price );
        
        // Set attributes requires a key/value containing
        $variation->set_attributes( array( $attribute_name_slug => $attribute_term_slug ) );

        //Save variation, returns variation id
        $variation_id = $variation->save();
        
        // Attribute formatted name slug and term slug for add to cart
        $attributes = array( 'attribute_pa_' . $attribute_name => $attribute_term_slug );

        WC()->cart->add_to_cart( $product_id, 1, $variation_id, $attributes, array() );

        exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
    }
}

代碼進入活動子主題(或活動主題)的functions.php文件。 它可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM