简体   繁体   English

Commerce JS 将产品变体添加到购物车

[英]Commerce JS add product variant to cart

I'm trying to make a marketplace on my webpage with different types of products.我正在尝试在我的网页上创建一个包含不同类型产品的市场。 Some are universal, but some have different sizing/type/color or whatnot.有些是通用的,但有些有不同的尺寸/类型/颜色等等。

So far I have the product listing and cart functionalities on point, except for the fact that I can't add to cart a products variant.到目前为止,我已经掌握了产品列表和购物车功能,但我无法将产品变体添加到购物车中。

I've been on the look out on how to do it but there's really not much I could find and I'm not too advanced either.我一直在寻找如何做到这一点,但我真的找不到太多,而且我也不是太先进。 However, I found this in their docs但是,我在他们的文档中发现了这一点

// If you have udpated your API version and have not yet generated variants
// from your variant groups and options, you\'ll need to update the previous
// `vrnt` object prefix to `vgrp` along with your option id
commerce.cart.add('prod_1ypbroE658n4ea', 1, {
  vgrp_dKvg9l6vl1bB76: 'optn_VPvL5z6O95AQkX'
});

// If you have updated to the latest API version and have variants generated
// from your variant groups and options, all you need to do is send the
// variant id as the third argument in your add to cart method
commerce.cart.add('prod_1ypbroE658n4ea', 1, 'vrnt_dKvg9l6vl1bB76');

and I adjusted to code accordingly: DO notice the commented code in handleAddToCart, when I select that specific product(with that part uncommented, and the other commented) and that specific variant (id based) and add it to cart it works, and adds it to cart with the variant included and it works of course only for that specific product, code & variant - but the uncommented code doesn't work.我相应地调整了代码:请注意handleAddToCart中的注释代码,当我select该特定产品(该部分未注释,另一部分已注释)和该特定变体(基于ID)并将其添加到购物车时,它可以工作,并添加它与包含的变体一起购物,它当然仅适用于该特定产品、代码和变体 - 但未注释的代码不起作用。

    const handleAddToCart = async (productId, quantity,variant ) => {
        // const item = await commerce.cart.add(productId, quantity, {
        //     vgrp_Kvg9l6Lpbo1bB7: 'optn_AYrQlWXv1JwnbR'
        // });
        const item = await commerce.cart.add(productId, quantity, variant);
        setCart(item.cart);
    };

Here is how I manage the product cart functionalty这是我管理产品购物车功能的方式

    const [variant, setVariant] = useState('');

    const handleAddToCart = () => {
        if (product.variant_groups.length >= 1) {
            if (variant === '') {
                setShowPicker(true);
            } else {
                onAddToCart(
                    product.id,
                    1,
                    Object.defineProperty(Object(), product.variant_groups[0].id, {
                        value: variant,
                        writable: false
                    })
                );
            }
        } else {
            onAddToCart(
                product.id,
                1,
                Object.defineProperty(Object(), product.variant_groups[0].id, {
                    value: variant,
                    writable: false
                })
            );
            setShowPicker(false);
        }
    };

                    {showPicker && (
                        <div>
                            <label htmlFor="variant-picker">{product.variant_groups[0].name}</label>
                            <select
                                name="variant-picker"
                                id="variant-picker"
                                value={variant}
                                onChange={(e) => setVariant(e.target.value)}
                            >
                                {product.variant_groups[0].options.map(({ id, name }) => (
                                    <option value={id} key={id}>
                                        {name}
                                    </option>
                                ))}
                            </select>
                        </div>
                    )}

What makes me confuses me the most is that最让我困惑的是

        Object.defineProperty(Object(), product.variant_groups[0].id, {
            value: variant,
            writable: false
        })

prints exacty as the docs say, or even as the handleAddToCart commented code:正如文档所说的那样精确打印,甚至像 handleAddToCart 注释的代码一样:

{vgrp_ypbroE4k1w8n4e: 'optn_aZWNoyY0kjo80J'}

But it still won't work, it adds the product to cart, but doesn't register the variant.但它仍然不起作用,它将产品添加到购物车,但不注册变体。 Can someone point out please what am I doing wrong?有人可以指出我做错了什么吗?

Such an over-engineered approach.这种过度设计的方法。 I just finished making mine as simple as I could.我刚刚完成了我的尽可能简单。

Just pass the group Id from a product w variant_groups over 0, this would be product.variant_groups[0].id.只需从产品 w variant_groups 中传递超过 0 的组 ID,这将是 product.variant_groups[0].id。

Then you get the option id from product.variant_groups[0].options when you are looping through them to display on a dropdown.然后,当您循环浏览它们以显示在下拉列表中时,您会从 product.variant_groups[0].options 获取选项 ID。 The variable would be set in setOptionId(optionId)该变量将在 setOptionId(optionId) 中设置

once you've set the state for these variables you can call commerce.cart.add(productId, 1, {[groupId]: optionId}) and it will work.为这些变量设置 state 后,您可以调用 commerce.cart.add(productId, 1, {[groupId]: optionId}) 它将起作用。 I promise.我保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM