[英]Apply fee based on product category and product length in WooCommerce
我正在尝试根据产品类别和产品长度收取费用。
如果产品属于某个类别并且长度小于 30m,我想对每个产品收取切割价格费用(切割地板卷的成本)。
我有一个 function 工作,可以将降价添加到某个类别,但我似乎无法弄清楚如何使其成为有条件的,所以它只在项目长度小于 30m 时才增加费用
请在下面查看我尝试过的两个版本:
Function 1:下面的代码在没有长度条件的情况下工作,因此向购物车中具有特定类别的每个项目添加费用
// Add cut price to category flotex
function woo_add_cart_fee() {
$category_ID = '83'; // Flotex Category is 83
global $woocommerce;
$cpfee = 0.00; // initialize special fee
//Getting Cart Contents.
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity in cart
foreach($cart as $cart_val => $cid){
$qty += $cid['quantity'];
}
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
foreach ($terms as $term) {
// 83 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $category_ID){
$cutprice = 30;
}
$cpfee = $qty * $cutprice;
}
$woocommerce->cart->add_fee('Cut Price', $cpfee, $taxable = true, $tax_class = 'standard');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
?>
Function 2:下面的代码尝试添加条件长度规则,但它不起作用。 即使长度为 30,它也总是为每个项目返回 0 或收取 30
// Add cut price to category flotex
function woo_add_cart_fee() {
$category_ID = '83'; // Flotex Category is 83
global $woocommerce;
$cpfee = 0.00; // initialize special fee
$qty = 0;
$cutpricesmall = 0;
//Getting Cart Contents.
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity in cart
foreach($cart as $cart_val => $cid){
$qty += $cid['quantity'];
}
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$product = $values['data'];
$length = $product->get_length();
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
if ( $length < 29 ) {
// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
foreach ($terms as $term) {
// 83 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $category_ID){
$cutprice = 30;
}
}
} elseif ( $length > 29 ){
foreach ($terms as $term) {
// 83 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $category_ID){
$cutpricesmall = 0;
}
}
}
$cpfee = $qty * ($cutprice + $cutpricesmall);
$woocommerce->cart->add_fee('Cut Price', $cpfee, $taxable = true, $tax_class = 'standard');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
对此的任何建议将不胜感激。
您的代码包含一些错误或可以优化:
$woocommerce;
没有必要get_the_terms()
替换为has_term()
foreach
循环,而 1 个就足够了所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 83, 'categorie-1' );
// Settings
$cut_price = 30;
$length = 30;
// Initialize
$cp_fee = 0;
// Gets cart contents
foreach ( $cart->get_cart_contents() as $cart_item ) {
// Has certain category
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// Get length
$product_length = $cart_item['data']->get_length();
// NOT empty and less than
if ( ! empty ( $product_length ) && $product_length < $length ) {
// Get quantity
$quantity = $cart_item['quantity'];
// Addition to the total
$cp_fee += $cut_price * $quantity;
}
}
}
// Greater than
if ( $cp_fee > 0 ) {
// Add fee
$cart->add_fee( __( 'Cut Price', 'woocommerce' ), $cp_fee, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
注意:如果不考虑每个产品的产品数量
代替:
// NOT empty and less than
if ( ! empty ( $product_length ) && $product_length < $length ) {
// Get quantity
$quantity = $cart_item['quantity'];
// Addition to the total
$cp_fee += $cut_price * $quantity;
}
和:
// NOT empty and less than
if ( ! empty ( $product_length ) && $product_length < $length ) {
// Addition to the total
$cp_fee += $cut_price;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.