Woocommerce将产品添加到购物车多次,但作为不同的项目

问题描述:

我有一些自定义代码,将项目添加到购物车,一个可变的产品。如果我两次添加相同的产品,它只会增加数量。我希望它再次添加产品作为购物车中的其他物品。Woocommerce将产品添加到购物车多次,但作为不同的项目

我该怎么做呢?该商品正在使用类似domain.com?add-to-cart=3434 & variation_id = 4434等链接添加到购物车。

我开发的系统是产品设计师。所以我可能想要选择相同的产品,但以不同的方式进行设计,然后将相同的变体添加到购物车。

是否还有一种方法可以在URL中发送唯一标识符来分割这些项目?

我想用add_to_cart但每次我这样做,与变化属性的时间来做到这一点,航运与错误结束,就基本能似乎无法找到发货方式:

$woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate")); 

虽然该代码将该项目添加到购物车,它会导致以下错误:

警告:在/ home/****/public_html/wp-content/plugins/woocommerce/includes/class- wc-shipping.php在线291

似乎并没有y可用的运输方式。请仔细检查您的地址,如果您需要任何帮助,请与我们联系。

它没有任何意义,因为标准添加到woocommerce中的购物车代码使用add_to_cart并没有这些问题。同样如果我使用一个URL,它工作正常!

我不能告诉,如果航运阵列问题是相关的。但是,如果你看看在add_to_cart()方法在车类的开头:

// Generate a ID based on product ID, variation ID, variation data, and other cart item data 
$cart_id  = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data); 

// See if this product and its options is already in the cart 
$cart_item_key = $this->find_product_in_cart($cart_id); 

基本上,我们现在看到的是该物品必须是完全唯一的,以将其添加至购物车再次。否则,它只会增加购物车中已有商品的数量。因此,为了解决这个问题,您需要使add_to_cart()参数具有唯一性...可能需要通过最终的$cart_item_data阵列。

+0

这是有道理的,唯一我一直想知道的,在代码中有相当模糊。 $变量变量采用属性参数。 $ cart_item_data我不确定这到底是什么? 阵列的问题,并说有没有航运的方法,它完全困惑我!该产品添加了完美的变化,但我们没有运输方法和数组问题。一个简单的产品不会导致这个问题! – Glen 2014-10-27 20:36:36

+0

默认情况下,'$ cart_item_data'不会执行任何操作......它可以让您自定义事件。我认为,就你的情况而言,无论是什么使得“设计”产品独一无二,都是包含在$ cart_item_data数组中的好选择。与运输问题有关......正如我在其他问题中提到的那样,您仍未显示足够的自定义代码来诊断问题。例如,当你调用这个'add_to_cart'函数(什么钩子?)? – helgatheviking 2014-10-27 20:59:37

+0

我发现了这个问题。我正在挖掘class-wc-shipping.php,看起来代码有点麻烦。我的运输错误消失了。有两件事情导致包裹中的费率一直返回空。我不确定这是否符合我的所有定制要求,但是我侵入了woocommerce文件以完成此操作(不需要更新)。第352行,将===更改为允许if运行,否则最终死机。然后375 $ package ['rates'] = apply_filters('woocommerce_package_rates',$ package ['rates'],$ package) - 此过滤器已将费率清除 – Glen 2014-10-27 21:12:13

试试这个!

/* 
* @desc Force individual cart item 
*/ 
function force_individual_cart_items($cart_item_data, $product_id){ 

    $unique_cart_item_key = md5(microtime().rand()); 
    $cart_item_data['unique_key'] = $unique_cart_item_key; 

    return $cart_item_data; 

} 

add_filter('woocommerce_add_cart_item_data','force_individual_cart_items', 10, 2); 


/* 
* @desc Remove quantity selector in all product type 
*/ 
function remove_all_quantity_fields($return, $product) { 

    return true; 

} 

add_filter('woocommerce_is_sold_individually', 'remove_all_quantity_fields', 10, 2); 
+0

我可以在第一个函数中添加if语句来设置某些产品的价值,比如5个库存数量,而其他所有产品都设置为1?它只应影响库存数量,而不是价格(即不应使价格为$ i x10)。 – vytfla 2016-03-17 19:00:26