php – WooCommerce – 如果数量超过1,则将购物车项目分开处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – WooCommerce – 如果数量超过1,则将购物车项目分开处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3104字,纯文字阅读大概需要5分钟。
内容图文
我正在尝试执行以下操作:
客户将数量为“1”的产品添加到购物车(数量字段已在单个产品页面上删除,因此只能添加1个).
如果再次添加具有相同变体的相同产品,而不是将数量增加到“2”,则将其添加为单独的产品.
我设法使用下面的过滤器执行上述操作,我在WordPress论坛上找到了它.
add_filter('woocommerce_add_cart_item_data', 'force_separate_item_add', 10, 2);
function force_separate_item_add($cart_item_data, $product_id) {
$unique_cart_item_key = md5(microtime().rand()."Hi Mom!");
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
这家伙的解释非常好,我明白了:“当一件物品被添加到购物车时,购物车中单个物品的’钥匙’将根据所添加的物品及其相关的元数据创建.如果物品并且它的元数据与购物车中的另一个项目相同,那么生成的密钥也是相同的,并且购物车中已经存在的商品的数量将简单地增加“添加的数量”.
上面的代码只是添加了一个新密钥,然后购物车将它们视为单独的条目.
我现在要做的是在通过购物车更改数量时为此找到合适的过滤器.因此,如果客户将数量更改为“2”,它将为“第二”项生成一个新密钥并单独处理.
我已经尝试了几乎所有可以找到的“购物车”相关过滤器,在WooCommerce文档上搜索,在“更新购物车”,“购物车”等的woocommerce插件文件夹上进行Sublime上的大量搜索,但我害怕我我真的不确定要使用哪个.我最努力的是这一个:
add_action('woocommerce_before_calculate_totals', 'change_cart_item_price');
function change_cart_item_price($cart_object) {
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $cart_key => $cart_item_array) {
if($cart_item_array['quantity'] > 1 ) {
$cart_item_key = md5(microtime().rand()."Hi Mom!");
$cart_item_data['unique_key'] = $cart_item_key;
$woocommerce->cart->set_quantity($cart_item_key, '1');
}
}
return $cart_object;
}
在这里,我正在浏览购物车对象,检查数量是否大于1,如果是,我分配一个新密钥.我真的不知道我是做正确的事情,还是我完全偏离轨道所以任何指针都会非常感激.
如果您需要更多信息,请告诉我,我会尽力提供一切.
非常感谢.
解决方法:
你的woocommerce_add_cart_item_data过滤器对我来说是一个很好的开始.我最终离开了原样并转移到woocommerce_add_to_cart挂钩进行手动处理.
这是目前为我工作的一个苗条版本.
/**
* This hook runs at the end of the default add to cart processes, when you try to add an item to cart.
* If trying to add more than item to the cart, separate them into individual cart items
*
* @author Mike Hemberger <mike@bizbudding.com>
*
* @return void
*/
add_action( 'woocommerce_add_to_cart', 'mai_split_multiple_quantity_products_to_separate_cart_items', 10, 6 );
function mai_split_multiple_quantity_products_to_separate_cart_items( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// If product has more than 1 quantity
if ( $quantity > 1 ) {
// Keep the product but set its quantity to 1
WC()->cart->set_quantity( $cart_item_key, 1 );
// Run a loop 1 less than the total quantity
for ( $i = 1; $i <= $quantity -1; $i++ ) {
/**
* Set a unique key.
* This is what actually forces the product into its own cart line item
*/
$cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );
// Add the product as a new line item with the same variations that were passed
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );
}
}
}
内容总结
以上是互联网集市为您收集整理的php – WooCommerce – 如果数量超过1,则将购物车项目分开处理全部内容,希望文章能够帮你解决php – WooCommerce – 如果数量超过1,则将购物车项目分开处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。