php - 在管理面板中显示前端的运输方式?

标签 php wordpress woocommerce cart

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

我该怎么做?

这是我的代码:

<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 需要知道哪个是客户运输区域。因此,在您获得该信息之前,您的脚本将无法正确运行。

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

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

// (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 (made 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. 对于每种运输方式,您都可以设置运输等级费率

所以这要复杂得多......


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

// 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


最近其他相关答案:Display shipping cost on product page - WooCommerce

关于php - 在管理面板中显示前端的运输方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47252728/

相关文章:

javascript - 如何将php数据传递给kinetic js

php - Woocommerce 获取产品

css - 在 WooCommerce 商店页面中将 "add to cart"按钮对齐成一条直线

php - 如何为指向我的服务器的域动态创建新的 LetsEncrypt/Certbot SSL 证书?

javascript - 如何将用户推送到对象,PHP 到 JSON

php - 在 Laravel Auth() 中通过电子邮件/电话号码登录

php - CodeIgniter 中的 Wordpress

wordpress - 结帐页面上的文件上传字段 woocommerce

wordpress - 使用 WooCommerce 产品 CSV 导入套件导入属性/变体

javascript - Ajax 帖子未完成