我的 WooCommerce 网站中有一些自定义代码,用于将产品添加到用户购物车。我已经让它检查购物车内容以确保购物车中首先有另一种产品,但我还希望它检查正在添加的产品是否也有库存...
我想不出最好的方法。如果您能让我知道要向函数中添加什么以使其检查“product_id”的库存以及库存是否 > 0,我将不胜感激。这是我的代码:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 21576;
$found = false;
global $woocommerce;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
if (sizeof( WC()->cart->get_cart() ) == 1 && $found == true ) {
$woocommerce->cart->empty_cart();
}
}
}
最佳答案
You can use the WC_Product conditional method
is_in_stock()directly in your if statement.As
$productis already a product object, we don't need anything else to make it work with WC_product methods.
也可以使用 WC_cart 方法 is_empty() 代替 sizeof( WC()->cart->get_cart() ) > 0这样! WC()->购物车->is_empty()。
然后您还可以使用 WC_cart get_cart_contents_count() 替换 sizeof( WC()->cart->get_cart() ) == 1这种方法 WC()->cart->get_cart_contents_count() == 1。
如果您使用
Last thing: 所以你的代码将是: 此代码已经过测试并且可以工作。 代码位于您的事件子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。WC()->cartglobal woocommerce; 声明> 用 all WC_Cart methods 代替 $woocommerce->cart
May be is better to remove the concerned cart item, instead emptying the cart. So we will use remove_cart_item() method instead.
If is not convenient you can use empty_cart() method as in your original code…add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$targeted_product_id = 21576;
$found = false;
//check if product already in cart
if ( ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( $product->id == $targeted_product_id ) {
$found = true;
break; // We can break the loop
}
}
// Update HERE (we generate a new product object $_product)
$_product = wc_get_product( $targeted_product_id );
// If product is not in the cart items AND IT IS IN STOCK
if ( !$found && $_product->is_in_stock() ) {
WC()->cart->add_to_cart( $targeted_product_id );
}
elseif ( $found && WC()->cart->get_cart_contents_count() == 1 ) {
// Removing only this cart item
WC()->cart->remove_cart_item( $cart_item_key );
// WC()->cart->empty_cart();
}
}
}
}
关于php - WooCommerce 在添加之前检查产品 ID 的库存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148592/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/