admin管理员组文章数量:1122832
i found solution here to count flat rate based on specific quantity steps in woocommerce, what i need additionally to add an extra specific amount based on shipping zone. for example : i charged customer 4 dollar for each 3 items based on specific shipping class, so if the customer added 6 items to cart with the same shipping class he will charged 8 dollar, what i need then to add 50 dollar additional based on the customer shipping zone so the total shipping cost get 58 dollar (8 for items number + 50 for shipping zone), also i want to be abble to add many shipping classes and many shipping zones to the code.
Here's the code
add_filter( 'woocommerce_package_rates', 'progressive_shipping_cost_based_shipping_class_quantity_steps', 10, 2 );
function progressive_shipping_cost_based_shipping_class_quantity_steps( $rates, $package )
{
// HERE Bellow your settings
$shipping_class = "very-light-weight"; // The shipping class ID
$qty_step = 20; // Items qty threshold for a step
$item_count = 0; // Initializing
// Get the shipping class ID
$class_id = get_term_by('slug', $shipping_class, 'product_shipping_class' )->term_id;
// Loop through in cart items to get the Tshirts count
foreach( $package['contents'] as $cart_item ) {
if ( $cart_item['data']->get_shipping_class_id() == $class_id ){
$item_count += $cart_item['quantity']; // Count Tshirts
}
}
// The rate operand increase each {$qty_step} depending on {$item_count}
$rate_operand = ceil( $item_count / $qty_step );
foreach ( $rates as $rate_key => $rate ){
// Targetting "Flat rate"
if( 'flat_rate' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
$rates[$rate_key]->cost = $rate->cost * $rate_operand;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $rate_operand;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
i found solution here to count flat rate based on specific quantity steps in woocommerce, what i need additionally to add an extra specific amount based on shipping zone. for example : i charged customer 4 dollar for each 3 items based on specific shipping class, so if the customer added 6 items to cart with the same shipping class he will charged 8 dollar, what i need then to add 50 dollar additional based on the customer shipping zone so the total shipping cost get 58 dollar (8 for items number + 50 for shipping zone), also i want to be abble to add many shipping classes and many shipping zones to the code.
Here's the code
add_filter( 'woocommerce_package_rates', 'progressive_shipping_cost_based_shipping_class_quantity_steps', 10, 2 );
function progressive_shipping_cost_based_shipping_class_quantity_steps( $rates, $package )
{
// HERE Bellow your settings
$shipping_class = "very-light-weight"; // The shipping class ID
$qty_step = 20; // Items qty threshold for a step
$item_count = 0; // Initializing
// Get the shipping class ID
$class_id = get_term_by('slug', $shipping_class, 'product_shipping_class' )->term_id;
// Loop through in cart items to get the Tshirts count
foreach( $package['contents'] as $cart_item ) {
if ( $cart_item['data']->get_shipping_class_id() == $class_id ){
$item_count += $cart_item['quantity']; // Count Tshirts
}
}
// The rate operand increase each {$qty_step} depending on {$item_count}
$rate_operand = ceil( $item_count / $qty_step );
foreach ( $rates as $rate_key => $rate ){
// Targetting "Flat rate"
if( 'flat_rate' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
$rates[$rate_key]->cost = $rate->cost * $rate_operand;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $rate_operand;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
Share
Improve this question
edited Jun 10, 2024 at 15:45
Pat J
12.3k2 gold badges28 silver badges36 bronze badges
asked Jun 8, 2024 at 11:27
Shehab ZahranShehab Zahran
32 bronze badges
1 Answer
Reset to default 0To adjust the WooCommerce code so it adds an extra specific amount based on the shipping zone, you can modify the existing function to include shipping zone-based costs. Here’s a streamlined version of the code:
add_filter( 'woocommerce_package_rates', 'add_cost_based_on_zone_and_quantity', 10, 2 );
function add_cost_based_on_zone_and_quantity( $rates, $package ) {
$shipping_class = "very-light-weight"; // Define the shipping class
$qty_step = 20; // Quantity threshold for a step
$item_count = 0; // Initialize item count
// Define extra costs by shipping zone ID
$zone_costs = array(
'zone_1' => 50, // Adjust these with actual zone IDs and costs
'zone_2' => 75,
);
// Get the shipping class ID and count items
$class_id = get_term_by('slug', $shipping_class, 'product_shipping_class')->term_id;
foreach ($package['contents'] as $cart_item) {
if ($cart_item['data']->get_shipping_class_id() == $class_id) {
$item_count += $cart_item['quantity'];
}
}
// Calculate the rate per quantity step
$rate_operand = ceil($item_count / $qty_step);
// Determine the shipping zone and retrieve additional costs
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package($package);
$zone_id = $shipping_zone->get_id(); // Get zone ID
foreach ($rates as $rate_key => $rate) {
if ('flat_rate' === $rate->method_id) {
$extra_cost = isset($zone_costs['zone_' . $zone_id]) ? $zone_costs['zone_' . $zone_id] : 0;
$rates[$rate_key]->cost = ($rate->cost * $rate_operand) + $extra_cost; // Adjust the total cost
}
}
return $rates;
}
Key Adjustments:
- Zone Costs Array: Contains additional costs linked to specific shipping zones.
- Shipping Zone Detection: Identifies the customer’s shipping zone to determine extra charges.
- Cost Adjustment: Modifies the shipping cost based on both item quantity and shipping zone.
Replace 'zone_1'
, 'zone_2'
with your actual shipping zone identifiers and adjust the additional costs accordingly. This code now succinctly adds specific costs based on both the quantity of items and the customer's shipping zone.
本文标签: phpAdding extra cost to woocommerce flat rate based on shipping zone
版权声明:本文标题:php - Adding extra cost to woocommerce flat rate based on shipping zone 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304129a1932126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论