dsxml2169 2016-12-14 07:56 采纳率: 0%
浏览 81

如何在钩子中访问WooCommerce自定义计费字段

I'm writing a custom WordPress function that will change the flat_rate shipping when a customer changes the "State" field from a select menu. Currently I'm doing it in my theme's functions.php

I have created a custom field to represent "State" field as a drop down menu in the billing fields. I used "WooCommerce Checkout Manager" plugin to setup the custom field and disabled the default "State" field.

Now I want to change the shipping cost depending on the value of my custom "State" field. I'm unable to retrieve the data of the field. Also I want to know what hook I can use to change the flat rate shipping once this field's value is changed.

I've used this filter hook (woocommerce_package_rates) and it doesn't work.

Here is my code to do it which I got it from another tutorial then made my customization

function wc_ninja_change_flat_rates_cost( $rates, $package ) {
$destination = $package['destination'];
$city = $destination['myfield12']; // getting the city field value

// Make sure flat rate is available

if ( isset( $rates['flat_rate'] ) ) {

    if ( $city == 'Alex' || $city == 'الإسكندرية' ) {
        // Set flat rate to cost $10 more
        $rates['flat_rate']->cost = 30;
    }
    else {
        $rates['flat_rate']->cost = 20;
    }
}

return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_change_flat_rates_cost', 10, 2 );
  • 写回答

1条回答 默认 最新

  • dongyongan9941 2016-12-14 11:32
    关注

    I found this:

    http://phpwpinfo.com/how-to-update-shipping-cost-in-cart-dynamically-based-on-a-custom-field-in-woocommerce/

    Basically it captures the field data through JS and send an Ajax request to the server which then stores the value in the session. Then adds an additional fee. It is not exactly what I was seeking to do but its a functional workaround.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度