gpt4 book ai didi

php - 具有特定产品标签的 WooCommerce 产品的批量动态定价

转载 作者:行者123 更新时间:2023-12-04 15:34:06 25 4
gpt4 key购买 nike

我正在尝试为所有具有标签的产品添加动态折扣:“bulk-discount” 我希望在客户购买时发生折扣,例如。 5 个带有标签的相似或不同的产品。

我正在使用 this代码。和 this回答。这就是我所拥有的:

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );

function bbloomer_quantity_based_pricing( $cart ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {

// Get an instance of the WC_Product object
$product = $cart_item['data'];

// Get product id
$product_id = $cart_item['product_id'];

if( method_exists( $product, 'set_name' ) && has_term( 'bulk-discount', 'product_tag', $product_id ) ) {

// Define discount rules and thresholds
$threshold1 = 5; // Change price if items > 4
$discount1 = 0.05; // Reduce unit price by 5%
$threshold2 = 10; // Change price if items > 9
$discount2 = 0.1; // Reduce unit price by 10%

foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
$cart_item['data']->set_price( $price );
}
}

}

最佳答案

第一个循环计算标签在单个产品或同一产品的多个项目上出现的次数

如果满足条件,第二个循环应用折扣

function bbloomer_quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

// count tag found
$tag_found = 0;

// Loop through cart items, count how many times tag occurs
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];

// Get product id
$product_id = $cart_item['product_id'];

// if product has tag
if( has_term( 'bulk-discount', 'product_tag', $product_id ) ) {

// Get quantity from product in cart
$quantity = $cart_item['quantity'];

// if product quantity > 1
if ( $quantity > 1) {
$tag_found = $tag_found + $quantity;
} else {
$tag_found += 1;
}
}
}

// Define discount rules and thresholds
$threshold = 5; // Change price if items > 4
$discount = 0.05; // Reduce unit price by 5%

// if tag found >= $threshold
if ( $tag_found >= $threshold ) {
// Loop through cart items, add discount
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];

// Get product id
$product_id = $cart_item['product_id'];

// if product has tag
if( has_term( 'bulk-discount', 'product_tag', $product_id ) ) {
// calculate new price
$price = round( $cart_item['data']->get_price() * ( 1 - $discount ), 2 );

// set new price
$cart_item['data']->set_price( $price );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 10, 1 );

相关:Multiple bulk dynamic pricing for WooCommerce products with specific product-tag

关于php - 具有特定产品标签的 WooCommerce 产品的批量动态定价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60355779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com