WooCommerce产品类别和城市运输费用

问题描述 投票:0回答:1

我在这里有一个方案。

在我已经设置的WooCommerce商店中,产品类​​别'Bronx'是其父类别,而'Baby Items Bronx'是其子类别。这里的父类别名称Bronx是销售'Baby Items Bronx'下产品的城市。

另一个产品类别'Newark'是其父类别,'Baby Items Newark'是其子类别。这里的父类别名称Newark是销售'Baby Items Newark'下产品的城市。

[的一位顾客正试图购买'Baby Items Newark'类别的产品。

我正在尝试在WooCommerce中设置自定义运费,以便它可以根据客户的位置计算运费并添加自定义运费。如果客户使用此代码在帐单邮寄地址中将城市选择为

Bronx

,则可以实现增加费用的目的。function from_to_city_add_checkout_fee() { if (($_POST['city']=='Bronx')){ WC()->cart->add_fee( 'Delivery Fee', 15 ); }
现在,如果客户来自

布朗克斯

,并且尝试购买父类别下列出的产品为'Newark',则我需要一个可以计算费用的函数>我尝试了以下代码,但它不起作用。

function from_to_city_add_checkout_fee() { if (($_POST['city']=='Bronx') && (has_term( array( 'baby-items-newark' ), 'product_cat' ))){ WC()->cart->add_fee( 'Fee', 150 ); } }

[我尝试使用从https://businessbloomer.com/woocommerce-check-product-category-cart/获得的代码段执行类似操作,以帮助检查购物车中的产品类别,但仍然无法使用。

add_action('woocommerce_before_cart', 'bbloomer_check_category_in_cart'); function bbloomer_check_category_in_cart() { // 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 Cart has category "download", set $cat_in_cart to true if ( has_term( 'baby-items-newark', 'product_cat', $cart_item['product_id'] ) ) { $cat_in_cart = true; break; } } // Do something if category "download" is in the Cart if ( $cat_in_cart == true) { // For example, print a notice // Or maybe run your own function... function from_to_city_add_checkout_fee() { if (($_POST['city']=='Bronx')){ WC()->cart->add_fee( 'Delivery Fee', 150 ); } } }

感谢您的任何帮助。

我在这里有一个场景。在我设置的WooCommerce商店中,产品类​​别“布朗克斯”是其父类别,而“婴儿物品布朗克斯”是其子类别。此处父类别名称Bronx是...

php wordpress woocommerce checkout shipping
1个回答
1
投票
请尝试以下代码段。并将if条件替换为您的逻辑。
© www.soinside.com 2019 - 2024. All rights reserved.