Php 恢复Woocommerce中特定装运方式的折扣产品价格
我正试图找到解决以下问题的办法。 我一直在使用Woocommerce动态定价,以便对特定产品类别应用25%的折扣。 然后,我需要删除该折扣,如果航运选择将是本地_皮卡。 我想我可以调整以下代码。它在某处丢失了一些东西,所以我请求你的帮助Php 恢复Woocommerce中特定装运方式的折扣产品价格,php,wordpress,woocommerce,custom-taxonomy,shipping-method,Php,Wordpress,Woocommerce,Custom Taxonomy,Shipping Method,我正试图找到解决以下问题的办法。 我一直在使用Woocommerce动态定价,以便对特定产品类别应用25%的折扣。 然后,我需要删除该折扣,如果航运选择将是本地_皮卡。 我想我可以调整以下代码。它在某处丢失了一些东西,所以我请求你的帮助 function discount_table(){ // Set $cat_in_cart to false $cat_in_cart = false; // Loop through all products in the Cart for
function discount_table(){
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'category_1', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
$total = WC()->cart->subtotal;
$sconto_label = "";
if($total >= 300){
$sconto_label=15;
}elseif($total >= 220){
$sconto_label=10;
}elseif($total >= 150){
$sconto_label=8;
}elseif($total >= 100){
$sconto_label=4;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = explode(':',$chosen_methods[0]);
if($chosen_shipping[0]=='local_pickup' && !$cat_in_cart){
$sconto_label=25;
}
else if ($chosen_shipping[0]=='local_pickup' && $cat_in_cart){
$sconto_label=25;
// there should be something here or not?
}
$sconto_cliente = (($total*$sconto_label)/100);
if($sconto_label!="")
$sconto_cliente_net = ($sconto_loison/1.1);
WC()->cart->add_fee( "Discount ($sconto_label%)", -$sconto_cliente_net, false );
}
add_action( 'woocommerce_cart_calculate_fees','discount_table' );
是否有方法恢复特定类别的一个或多个商品在购物车中已打折的原始价格?若要根据产品类别和所选的特定装运方法恢复购物车商品的价格,请尝试以下操作(用于动态定价): 代码进入活动子主题(或活动主题)的function.php文件。它应该有效 注意:使用负费用(购物车折扣)时,始终适用税收
我对你的代码做了一些修改。感谢一个超级开发人员发现了一个bug!代码如下:
function discount_table(){
$cat_in_cart = false;
$tot_cat = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
$tot_cat_price = $cart_item['data']->get_price();
$tot_cat_qty = $cart_item['quantity'];
$tot_cat += $tot_cat_price * $tot_cat_qty;
//break;
/* commented here, thanks to Michael Ramin: break will stop execution of the loop after found the first product belonging to the category; what if you have more than one? so remove break instruction */
}
}
global $product;
$total = WC()->cart->subtotal;
$discount_label = "";
if($total >= 300){
$discount_label=15;
}elseif($total >= 220){
$discount_label=10;
}elseif($total >= 150){
$discount_label=8;
}elseif($total >= 100){
$discount_label=4;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = explode(':',$chosen_methods[0]);
if($chosen_shipping[0]=='local_pickup'){
$discount_label=25;
}
$discount_brand = ($total-$tot_cat)*$discount_label/100;
if($discount_label!=""){
$discount_brand_net = ($discount_brand/1.1);
if($discount_brand_net != 0) // show discount only if != 0; it may happen if cart includes only targeted category products;
WC()->cart->add_fee( "Discount ($discount_label%)", -$discount_brand_net, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','discount_table' );
谢谢你的回复。不幸的是,激活了Woocommerce动态定价,并在functions.php中使用了上面的一段代码——我需要它作为折扣比例应用于购物车——您的代码似乎不起作用。例如:一辆购物车包含两种商品,一种属于无折扣类别,另一种属于折扣类别。后者保留其折扣价格,3,75欧元,而不是购物车行中显示的5欧元。谢谢您的回复。您的代码似乎可以正常工作,但无法正常工作。我不认为这是你剧本的错。我需要重新加载购物车页面,以便更新购物车行和值。是否有替代方法代替手动重新加载页面?在这种情况下,可以计算税费。@alemarengo使用我正在使用的钩子,通常不会出现数据刷新问题,因为在计算总计之前,
woocommerce\u是ajax驱动的。我已经轻轻地更新了我的答案代码,但我不认为它会改变什么。因此,还有其他一些东西在那里制造问题。现在,一切都可以根据你的技能和花费的时间(以及预算)来完成。
function discount_table(){
$cat_in_cart = false;
$tot_cat = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
$tot_cat_price = $cart_item['data']->get_price();
$tot_cat_qty = $cart_item['quantity'];
$tot_cat += $tot_cat_price * $tot_cat_qty;
//break;
/* commented here, thanks to Michael Ramin: break will stop execution of the loop after found the first product belonging to the category; what if you have more than one? so remove break instruction */
}
}
global $product;
$total = WC()->cart->subtotal;
$discount_label = "";
if($total >= 300){
$discount_label=15;
}elseif($total >= 220){
$discount_label=10;
}elseif($total >= 150){
$discount_label=8;
}elseif($total >= 100){
$discount_label=4;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = explode(':',$chosen_methods[0]);
if($chosen_shipping[0]=='local_pickup'){
$discount_label=25;
}
$discount_brand = ($total-$tot_cat)*$discount_label/100;
if($discount_label!=""){
$discount_brand_net = ($discount_brand/1.1);
if($discount_brand_net != 0) // show discount only if != 0; it may happen if cart includes only targeted category products;
WC()->cart->add_fee( "Discount ($discount_label%)", -$discount_brand_net, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','discount_table' );