【发布时间】:2020-09-10 19:11:02
【问题描述】:
我已尝试寻找此问题的答案,但大部分代码已过时或不完全符合我的要求。以下是场景——当人们从我们的商店订购数字图像版权时,他们必须支付使用费和复制费。使用费根据几个因素而变化,并且不征税 - 这将是添加到购物车中的产品。复印费是每张图像的固定、应税费用。我希望在添加“图片”类别的产品时自动将其添加到我的购物车中。
我在 WPFactory 上找到了根据运行良好的类别添加费用的代码。
add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category' );
if ( ! function_exists( 'wpf_wc_add_cart_fees_by_product_category' ) ) {
/**
* wpf_wc_add_cart_fees_by_product_category.
*/
function wpf_wc_add_cart_fees_by_product_category( $cart ) {
// You need to enter your fees here, in `category slug` => `fee amount` format
$fees = array(
'images' => 25,
);
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$product_categories = get_the_terms( $values['product_id'], 'product_cat' );
if ( $product_categories && ! is_wp_error( $product_categories ) ) {
$product_categories = wp_list_pluck( $product_categories, 'slug' );
foreach ( $product_categories as $product_category ) {
if ( ! empty( $fees[ $product_category ] ) ) {
$name = 'Digital Image Copy Fee'
$amount = $fees;
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
}
}
}
}
}
问题是,它只添加一次费用,我需要它根据购物车中类别的商品总数乘以费用。我试图计算 $qty 并将 $fee 乘以 $qty 但我的 php 非常生锈而且我没有成功。任何帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce