是否在管理面板中将送货方式显示在前端? [英] Display shipping methods to frontend as in the admin panel?

查看:56
本文介绍了是否在管理面板中将送货方式显示在前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我正在进行添加/更改产品的形式(在前端)。我需要在管理面板中显示用于更改已安装方法的交付成本的字段。

In WooCommerce, I'm doing the form of adding / changing product (at the front-end). I need to display the fields for changing the cost of delivery of the installed methods as in the admin panel.

我该怎么办?

这是我的实际代码:

<form>
<!-- Various input fields -->

<?php foreach ($shipping_methods as $method) { ?>
  <lebel>
    <h3><?php echo $method ?></h3>
    <input type="number" value="<?php //hook for input - output to make changes of shipping price of this method??Which one ?>">
  </lebel>
<?php } ?>

<input type="submit" value="<?php echo $edit_action ? "Save" : "Add"; ?> Product">
</form>


推荐答案

由于运输方法已连接到运输区域,因此WooCommerce需要知道哪个是客户运输区域。因此,您的脚本将无法正确使用,直到您获得该信息为止。

As Shipping Methods are connected to a Shipping Zone, WooCommerce need to know which is the customer shipping zone. So your script will not work in a correct way until you get that information.


要获取该信息,客户需要登录或尚未登录,则应该在购物车中添加产品…

To get that information, customer needs to logged in or if is not logged in it should need to add a product in cart…

现在,使脚本正常工作的正确代码是:

Now the correct code to make your script working is:

// (temporary defining $edit_action for test purpose)
// To be removed and replaced by yours
$edit_action = true;
?>
<form>
    <!-- Various input fields -->

    <?php

    // Get the active shipping methods (Customer is logged in or cart is not empty)
    $rates = WC()->session->get("shipping_for_package_0")['rates'];
    if( ! empty($rates) ){
        foreach( $rates as $rate_key => $rate ){
            $method_id = $rate->method_id; // The method ID slug
            $instance_id = $rate->instance_id;
            $rate_id = $rate_key; // The rate ID (maid of: $method_id + ':' + $instance_id)
            // Or $rate_id = $rate->id;
            $label = $rate->label; // The method ID Label
            $cost = $rate->cost;
            $taxes_array = $rate->taxes;
        ?>
        <label>
            <h3><?php echo $label ?></h3>
            <input type="number" value="<?php echo number_format($cost, 2); ?>">
        </label>
        <?php
        }
    }
    ?>

    <input type="submit" value="<?php echo $edit_action ? "Save" : "Add"; ?> Product">
</form>

经过测试并有效(仅在定义了客户运输区域时)


现在,如果您想获得运输后端设置中的所有信息,则需要:

Now if you want to get everything like in shipping backend settings, you will need to:




  1. 获取运输区域

  2. 每个运输区域您将获得可用的运输方式

  3. 对于每种运输方式,您都可以拥有运输等级费率

所以这要复杂得多……

这是开始的方法,但我没有找到方法获取/设置成本,因为每种运输方式类型都不同...

Here is the way to start, but I didn't find the way to get/set cost as it's different for each shipping method type…

// Initializing variable
$zones = array();

// Rest of the World zone
$zone                                              = new \WC_Shipping_Zone(0);
$zones[$zone->get_id()]                            = $zone->get_data();
$zones[$zone->get_id()]['formatted_zone_location'] = $zone->get_formatted_location();
$zones[$zone->get_id()]['shipping_methods']        = $zone->get_shipping_methods();

// Merging shipping zones
$shipping_zones = array_merge( $zones, WC_Shipping_Zones::get_zones() );

foreach ( $shipping_zones as $shipping_zone ) {
    // Row output (for testing)
    // echo '<pre>'; print_r($shipping_zone); echo '</pre>';

    $zone_id = $shipping_zone['id'];

    $zone_name = $zone_id == '0' ? __('Rest of the word', 'woocommerce') : $shipping_zone['zone_name'];
    $zone_locations = $shipping_zone['zone_locations']; // (It's an array)
    $zone_location_name = $shipping_zone['formatted_zone_location'];
    $zone_shipping_methods = $shipping_zone['shipping_methods'];

    foreach ( $zone_shipping_methods as $shipping_method_obj ) {
        // Row output (for testing)
        // echo '<pre>'; print_r($shipping_method_obj); echo '</pre>';

        echo $shipping_method_obj->id.'<br>';
        echo $shipping_method_obj->get_instance_id().'<br>';
        echo $shipping_method_obj->get_rate_id().'<br>';
        echo $shipping_method_obj->get_method_title().'<br>'; // Generic name
        echo $shipping_method_obj->get_title().'<br>'; // Customized name
    }
}

产生运费的类是 WC_Shipping_Rate

最近相关的其他答案:在产品页面上显示运费-WooCommerce

Other related recent answer: Display shipping cost on product page - WooCommerce

这篇关于是否在管理面板中将送货方式显示在前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆