php - WooCommerce 上的第一个订单免运费
问题描述
我希望客户的第一个订单免运费 如何将此功能添加到 WooCommerce?我不想使用折扣代码,我希望自动应用此功能。
解决方案
也许是这样的?所有积分都用于 -在 Woocommerce 3 中以编程方式设置自定义运费
add_filter( 'woocommerce_package_rates', 'free_first_order_shipping', 20, 2 );
function free_first_order_shipping( $rates, $package ) {
// New shipping cost (can be calculated)
$new_cost = 0;
$tax_rate = 0;
//If user is logged in
if(is_user_logged_in()) {
$user_id = get_current_user_id();
//We want to know how many completed orders customer have or remove status if you dont care.
$args = array(
'customer_id' => 1,
'status' => array('wc-completed'),
);
$orders = wc_get_orders($args);
//If there are no orders returned apply free shipping
if(!$orders) {
foreach( $rates as $rate_key => $rate ){
error_log(print_r($rate,true));
// Excluding free shipping methods
if( $rate->method_id != 'free_shipping'){
// Set rate cost
$rates[$rate_key]->cost = $new_cost;
//Set shipping label
$rates[$rate_key]->label = 'Free Shipping';
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 )
$taxes[$key] = $new_cost * $tax_rate;
}
$rates[$rate_key]->taxes = $taxes;
}
}
}
}
return $rates;
}
推荐阅读
- c# - 如何在 NAudio MP3 流媒体部分使用轨迹栏
- c# - 为什么从 unity3d 插件 dll 调用时为 System.ServiceProcess.ServiceController.GetServices() 提供空引用
- c++11 - 在构造函数中使用列表初始化
- angular - 仅选择 ng-tab 的链接
- php - PHP:在类中通过引用传递数组并在其他方法中使用它
- python - 如何有效地将行附加到 pandas DataFrame
- reactjs - 当增量行模式打开时,反应 ag-grid 不重新渲染组行
- android - 我想动态地从几个视图中获取输入。我怎么做?
- javascript - 我如何从文本字段中读取一个值并进行一些计算并将结果写回 javascript 中的另一个文本字段?
- for-loop - 为什么我的 for loop() 没有按预期输出