我正在尝试执行以下操作:
客户将数量为“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;
}
这个人的解释非常好,我明白了:“当一个项目被添加到购物车时,购物车中那个单独项目的‘键’是根据被添加的项目及其关联创建的元数据。如果该项目及其元数据与购物车中的另一个项目相同,则生成的 key 也将相同,并且购物车中已有项目的数量将简单地随着添加的数量增加".
上面的代码只是添加了一个新键,然后购物车将它们视为单独的条目。
我现在要做的是在购物车的数量发生变化时为此找到合适的过滤器。因此,如果客户将数量更改为“2”,它将为“2nd”项目生成一个新 key 并单独处理。
我已经尝试了几乎所有我能找到的与“购物车”相关的过滤器,搜索 WooCommerce 文档,在 Sublime 上的 woocommerce 插件文件夹中大量查找“更新购物车”、“购物车”等,但我恐怕我不确定该用哪个。我工作最多的是这个:
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;
}
在这里,我正在遍历购物车对象,检查数量是否大于一个,如果是,我分配一个新键。我真的不知道我做的是否正确。
最佳答案
您的 woocommerce_add_cart_item_data 过滤器对我来说是一个很好的开始。我最终保留原样,并转到 woocommerce_add_to_cart Hook 以手动处理。
这是目前对我有用的精简版。
/**
* 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,请单独处理购物车商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32485152/
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile
我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS
啊,正则表达式有点困惑。我正在尝试删除字符串末尾所有可能的标点符号:ifstr[str.length-1]=='?'||str[str.length-1]=='.'||str[str.length-1]=='!'orstr[str.length-1]==','||str[str.length-1]==';'str.chomp!end我相信有更好的方法来做到这一点。有什么指点吗? 最佳答案 str.sub!(/[?.!,;]?$/,'')[?.!,;]-字符类。匹配这5个字符中的任何一个(注意,。在字符类中并不特殊)?-前一个字符或组
我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby-vips的github页面上的链接,我们将不胜感激!如果有ruby-
我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d