Php 是否在管理面板中显示发送到前端的方法?
在WooCommerce中,我以添加/更改产品的形式(在前端)。我需要在管理面板中显示用于更改已安装方法的交付成本的字段 我怎么做 这是我的实际代码:Php 是否在管理面板中显示发送到前端的方法?,php,wordpress,woocommerce,cart,shipping,Php,Wordpress,Woocommerce,Cart,Shipping,在WooCommerce中,我以添加/更改产品的形式(在前端)。我需要在管理面板中显示用于更改已安装方法的交付成本的字段 我怎么做 这是我的实际代码: <form> <!-- Various input fields --> <?php foreach ($shipping_methods as $method) { ?> <lebel> <h3><?php echo $method ?></h3>
<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需要知道哪个是客户配送区域。因此,在获得该信息之前,脚本将无法以正确的方式工作
要获得该信息,客户需要登录,如果未登录,则需要在购物车中添加产品
现在,使脚本正常工作的正确代码是:
// (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>
//(为测试目的临时定义$edit\u操作)
//被你的替换掉
$edit_action=true;
?>
您应该先在代码中将
更改为
。@LoicTheAztec,thx,但页面布局没有中断。关于钩子呢?现在,如果你想得到像航运后端设置一样的一切,你需要:1。获取运输区域…2。对于每个装运区,您将获得可用的装运方法…3。对于每种配送方式,您都可以有配送类费率…因此这要复杂得多…您应该查看plugins/woocommerce/includes/admin/settings/class wc settings shipping.php
…这应该对您有用,因为这主要是woocommerce>settings>shipping中涉及的代码…非常感谢很但我在哪里可以指定交付区域?我正在进行更改,单击“提交”按钮。但是运输价格不会改变。@Ааааааааа因为所有运输数据(如成本)都是缓存的…所以您需要找到刷新运输缓存数据的方法…这是一项高级开发…因此您将很难使其正常工作…除此之外,我再也帮不上忙了。再次感谢您。祝你好运
// 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
}
}