WooCommerce 产品类别和基于城市的运费
WooCommerce Product Category and City Based Shipping Fee
我这里有个场景。
在我设置的 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 );
}
现在我需要一个函数来计算费用,如果客户来自 Bronx 并且尝试购买父类别下列出的产品是 '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 );
}
}
}
感谢任何帮助。
试试下面的代码片段。
并用您的逻辑替换 if 条件。
add_action('woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge', 10, 1);
function woocommerce_custom_surcharge($cart)
{
global $woocommerce;
if (is_admin() && !defined('DOING_AJAX'))
return;
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
if (
$woocommerce->customer->get_shipping_city() == "Bronx" &&
has_term('baby-items-newark', 'product_cat', $product->id)
) :
WC()->cart->add_fee('Fee', 150);
endif;
}
}
您可以从
获取父类别
$parentcats = get_ancestors($product_cat_id, 'product_cat');
我宁愿创建运费 class 并在条件为真时添加它,以便应用不同的运费。
当您在产品中为城市使用自定义字段而不是类别时,这也很容易。
我这里有个场景。
在我设置的 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 );
}
现在我需要一个函数来计算费用,如果客户来自 Bronx 并且尝试购买父类别下列出的产品是 '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 );
}
}
}
感谢任何帮助。
试试下面的代码片段。 并用您的逻辑替换 if 条件。
add_action('woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge', 10, 1);
function woocommerce_custom_surcharge($cart)
{
global $woocommerce;
if (is_admin() && !defined('DOING_AJAX'))
return;
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
if (
$woocommerce->customer->get_shipping_city() == "Bronx" &&
has_term('baby-items-newark', 'product_cat', $product->id)
) :
WC()->cart->add_fee('Fee', 150);
endif;
}
}
您可以从
获取父类别$parentcats = get_ancestors($product_cat_id, 'product_cat');
我宁愿创建运费 class 并在条件为真时添加它,以便应用不同的运费。
当您在产品中为城市使用自定义字段而不是类别时,这也很容易。