我尝试根据我在 Woocommerce 购物车上所做的一些计算来添加费用,但我想将其从增值税中排除。这是我的代码:
function woo_add_cart_fee( $cart ) {
global $woocommerce; $bookable_total = 0;
foreach(WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
//doing my stuff to calculate $fee variable
WC()->cart->add_fee( 'Fees: ', $fee, false, '' );
//WC()->cart->add_fee( 'Fees: ', $fee, true, '' );
//WC()->cart->add_fee( 'Fees: ', $fee, false, 'zero rate' );
//WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
我已经尝试了所有的评论版本,每个版本都包含增值税费用。
知道如何实现它吗?
最佳答案
(更新):TAX OPTIONS add_fee() method
IMPORTANT: The fact that TAX is working or not with
add_fee()method depends first of your tax settings in woocommerce. As you have not tell in your question what are your TAX settings, It's not possible to help you (Tax settings can be much more different for each e-commerce web site).
例如,如果您想使用“零税率”税级,但您尚未为客户国家/地区定义正确的“零税率”税级,如果您尝试这样做将不起作用与它一起使用:
WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
……取决于您的全局税收设置。
这是购物车中 3 件商品的真实结帐总计的屏幕截图(使用下面的代码):
Class WC_Cart add_fee() method, adds additional fee to the cart.
add_fee( string $name, float $amount, boolean $taxable = false, string $tax_class = '' )Parameters: $name Unique name for the fee. Multiple fees of the same name cannot be added. $amount Fee amount. $taxable (default: false) Is the fee taxable? $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class.
原始答案 (更新代码):
Your main problem is in this line:
global $woocommerce, $bookable_total = 0;
- As you are using
WC()->cartsyntax instead of$woocommerce->cartsyntax, you don't really needglobal $woocommerce;.- If you use
global $bookable_total = 0;this$bookable_totalwill be always= 0.
Instead you will useglobal $bookable_total;without a value to get the value defined outside your function.
But if you want to set the value to zero, in case is not defined outside your function, you will do it this way:woo_add_cart_fee( $bookable_total=0 )
我们现在可以在函数外定义$bookable_total变量值。
这是您的代码的工作示例:
// This variable value is passed to our function
$bookable_total = 1;
function woo_add_cart_fee( $bookable_total = 0 ) {
global $bookable_total;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// just for this example
$item_count = 0;
$item_fee = 5;
// going through each cart items
foreach( WC()->cart->get_cart() as $values ) {
$item = $values['data'];
if ( empty( $item ) )
break;
// getting the cart item_id
$item_id = $item->id;
$item_count++;
// your calculations
}
// We test $bookable_total value, defined to '1' outside our function
// and to 'O' if not defined outside (in this case the fee will be '0')
$fee = $item_count * $bookable_total * $item_fee;
// add_fee method (TAX will NOT be applied here)
WC()->cart->add_fee( 'Fees: ', $fee, false );
}
add_action( 'woocommerce_cart_calculate_fees','woo_add_cart_fee' );
此代码已经过测试并且可以正常工作。 它位于您的事件子主题或主题的 function.php 文件中。
如果 $bookable_total 变量未在外部定义,则该值将为 0。
Note: is better to get the $items ids with:
$item = $values['data']; $item_id = $item->id;
引用:
Class WC_Cart - add_fee( $name, $amount, $taxable = false, $tax_class = '' ) method
关于php - 以编程方式向 WooCommerce 购物车添加免税费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38894444/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我有一个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";我尝试了所有不同的路径格式,但它
我正在使用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].有没有一种方法可以
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司