也许我一直在盯着WooCommerce核心文件太长时间,但我无法弄清楚如何在Checkout页面上只显示最终的运送选择。
这是一些背景知识:
我正在努力简化结帐流程。我想在购物车页面上选择仅的送货方式。如果是本地提货,则会在订单备注下显示条件字段以选择其提货地点。但是,当您在Chekcout页面上进行最终订单审核时,Woo会让您重新选择您的运输选项,这会使工作中出现问题。我基本上只想回应他们的选择和运费价格,不要让他们改变选择,除非他们回到购物车页面。
所以我查看了woocommerce / templates / checkout / review-order.php,这样就可以在购物车上调用这样的运输选项:
<?php wc_cart_totals_shipping_html(); ?>
此功能实际上来自购物车:
function wc_cart_totals_shipping_html() {
$packages = WC()->shipping->get_packages();
$first = true;
foreach ( $packages as $i => $package ) {
$chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
$product_names = array();
if ( sizeof( $packages ) > 1 ) {
foreach ( $package['contents'] as $item_id => $values ) {
$product_names[ $item_id ] = $values['data']->get_name() . ' ×' . $values['quantity'];
}
$product_names = apply_filters( 'woocommerce_shipping_package_details_array', $product_names, $package );
}
wc_get_template( 'cart/cart-shipping.php', array(
'package' => $package,
'available_methods' => $package['rates'],
'show_package_details' => sizeof( $packages ) > 1,
'show_shipping_calculator' => is_cart() && $first,
'package_details' => implode( ', ', $product_names ),
// @codingStandardsIgnoreStart
'package_name' => apply_filters( 'woocommerce_shipping_package_name', sprintf( _nx( 'Shipping', 'Shipping %d', ( $i + 1 ), 'shipping packages', 'woocommerce' ), ( $i + 1 ) ), $i, $package ),
// @codingStandardsIgnoreEnd
'index' => $i,
'chosen_method' => $chosen_method,
) );
$first = false;
}
}
现在,我也查看了woocommerce / includes / class-wc-checkout.php,发现了这个:
/**
* Add shipping lines to the order.
*
* @param WC_Order $order Order Instance.
* @param array $chosen_shipping_methods Chosen shipping methods.
* @param array $packages Packages.
*/
public function create_order_shipping_lines( &$order, $chosen_shipping_methods, $packages ) {
foreach ( $packages as $package_key => $package ) {
if ( isset( $chosen_shipping_methods[ $package_key ], $package['rates'][ $chosen_shipping_methods[ $package_key ] ] ) ) {
$shipping_rate = $package['rates'][ $chosen_shipping_methods[ $package_key ] ];
$item = new WC_Order_Item_Shipping();
$item->legacy_package_key = $package_key; // @deprecated For legacy actions.
$item->set_props(
array(
'method_title' => $shipping_rate->label,
'method_id' => $shipping_rate->method_id,
'instance_id' => $shipping_rate->instance_id,
'total' => wc_format_decimal( $shipping_rate->cost ),
'taxes' => array(
'total' => $shipping_rate->taxes,
),
)
);
foreach ( $shipping_rate->get_meta_data() as $key => $value ) {
$item->add_meta_data( $key, $value, true );
}
/**
* Action hook to adjust item before save.
*
* @since 3.0.0
*/
do_action( 'woocommerce_checkout_create_order_shipping_item', $item, $package_key, $package, $order );
// Add item to order and save.
$order->add_item( $item );
}
}
}
我觉得最后一个代码末尾的动作挂钩会让我在那里,但是我无法弄清楚哪个功能要乱用或者我可以使用来替换它。< / p>