微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WooCommerce 添加到购物车动态创建的产品变体

如何解决WooCommerce 添加到购物车动态创建的产品变体

我正在尝试使用此功能将产品添加到我的购物车: 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,$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 文件中。它可以工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。