在管理面板中向前端显示运输方式?
Display shipping methods to frontend as in the admin panel?
在 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:
- 获取送货区域
- 对于每个送货区域,您将获得可用的送货方式
- 对于每种送货方式,您可以使用 classes 费率
所以这要复杂得多……
这是开始的方法,但我没有找到 get/set 成本的方法,因为每种运输方式类型都不同…
// 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
}
}
使运费class为WC_Shipping_Rate
其他相关近期回答:
在 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:
- 获取送货区域
- 对于每个送货区域,您将获得可用的送货方式
- 对于每种送货方式,您可以使用 classes 费率
所以这要复杂得多……
这是开始的方法,但我没有找到 get/set 成本的方法,因为每种运输方式类型都不同…
// 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
}
}
使运费class为WC_Shipping_Rate
其他相关近期回答: