Php 将单选按钮添加到电子商务结账时的特定发货方式
我增加了一种新的运送方式“从商店取货”,可以免费运送到WooCommerce 我有两个商店,“Barsha”和“Deira”。 我希望当客户选择从商店取货时,能够选择他将访问的商店 下面是我添加到cart-shipping.php模板文件中的内容:Php 将单选按钮添加到电子商务结账时的特定发货方式,php,wordpress,woocommerce,checkout,shipping-method,Php,Wordpress,Woocommerce,Checkout,Shipping Method,我增加了一种新的运送方式“从商店取货”,可以免费运送到WooCommerce 我有两个商店,“Barsha”和“Deira”。 我希望当客户选择从商店取货时,能够选择他将访问的商店 下面是我添加到cart-shipping.php模板文件中的内容: <?php <hr><p>Please choose the pickup store:</p> <input type="radio" i
<?php
<hr><p>Please choose the pickup store:</p>
<input type="radio" id="barsha" sname="store" value="barsha">
<label for="barsha">Barsha<a href=""> Check Location</a></label><br>
<input type="radio" id="deira" sname="store" value="deira">
<label for="deira">Deira<a href=""> Check Location</a></label>
$sname= sname;
if ( $sname = 'barsha'); {
printf ('barsha')
} else {
printf ('deira')
}
?>
But I can't get it working. How can I add some radio buttons to a specific shipping method and save the chosen option when order is submitted?
但我不能让它工作。在提交订单时,如何将一些单选按钮添加到特定的装运方法并保存所选选项?
我想在您的案例中,您希望在“本地提货”和启用商业的发货方法下显示一些字段
与覆盖cart shipping.php
template不同,您可以更好地使用装运方法的专用操作挂钩,它将允许您显示特定装运方法的字段
选择以下代码后,您可以在“本地取货”运输方式下显示2个单选按钮。如果客户忘记选择门店,则会有一条消息提醒他选择门店提货。然后,在下订单时,所选商店将保存为自定义订单元数据,以管理员订单的形式显示在账单地址下以及所选配送方式(订单和电子邮件通知)附近的任何位置:
//将自定义字段添加到特定的选定装运方法
添加操作('woocommerce'在装运费率之后,'Picking'商店'custom'字段',100,2);
函数拾取\存储\自定义\字段($method,$index){
//仅在结帐页和本地收货装运方式上
如果(是_cart()||$method->method_id!=='local_pickup')
返回;
$selected_shipping_methods=WC()->session->get('selected_shipping_methods')[$index];
$selected_method_id=explode(“:”,$selected_shipping_methods);
$selected\u method\u id=重置($selected\u method\u id);
//仅当选择的装运方式为“本地提货”时
如果($selected\u method\u id!=“local\u pickup”)
返回;
回声'
“.”(“选择你的皮卡店”):
'
“巴沙”
'
“迪拉”
';
}
//皮卡店现场验证
添加操作('woocommerce\u checkout\u process'、'Pick\u store\u checkout\u validation');
函数拾取\存储\签出\验证(){
$selected_shipping_methods=WC()->session->get('selected_shipping_methods')['0'];
$selected_method_id=explode(“:”,$selected_shipping_methods);
$selected\u method\u id=重置($selected\u method\u id);
如果($selected\u method\u id=='local\u pickup'&&empty($\u POST['picku\u store']))
wc添加通知(“请选择皮卡店”),“错误”);
}
//将所选皮卡店另存为自定义订单元数据
添加操作('woocommerce\u checkout\u create\u order'、'Pick\u store\u update\u order\u meta');
功能拾取\存储\更新\订单\元($order){
如果(设置($邮政['Picking\u store'])和($邮政['Picking\u store'])为空($邮政['Picking\u store']))
$order->update_meta_data('picku_store',esc_attr($_POST['picku_store']);
}
//在帐单地址下显示所选的皮卡存储
添加操作('woocommerce\u admin\u order\u data\u在账单地址之后,'display\u Pick\u store\u on\u order\u edit\u pages');
功能显示(取货)(存储)在(订单)(编辑)页面上(订单){
如果($picking\u store=$order->get\u meta('picking\u store'))
echo“皮卡店:”.$Pickup\u店。“”;
}
//在所选运输方式下方显示所选的皮卡店
添加过滤器('woocommerce\u get\u order\u item\u totals','display\u Pick\u store\u on\u order\u item\u totals',1000,3);
功能显示\取货\存储\订单\项目\总计($total\行、$order、$tax\显示){
如果($picking\u store=$order->get\u meta('picking\u store')){
$new_total_rows=[];
//循环排序所有行
foreach($key=>$value的行总数){
$new_total_行[$key]=$value;
//在装运方式下插入皮卡存储
如果($key=='shipping'){
$new_total_rows['picku_store']=数组(
“标签”=>“(“皮卡店”),
“值”=>$picku\u商店
);
}
}
返回$new\u total\u行;
}
返回$total_行;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作
在结帐页面: 关于客户订单(和电子邮件通知): 在管理订单编辑页面(账单地址下):
有关答案:
if($sname='barsha');{
那应该是什么?非常感谢LoicTheAztec,工作得很有魅力,感谢你的帮助……你救了我一天
// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'pickup_store_custom_field', 100, 2 );
function pickup_store_custom_field( $method, $index ) {
// Only on checkout page and for Local pickup shipping method
if( is_cart() || $method->method_id !== 'local_pickup' )
return;
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods')[ $index ];
$chosen_method_id = explode(':', $chosen_shipping_methods);
$chosen_method_id = reset($chosen_method_id);
// Only when the chosen shipping method is "local_pickup"
if( $chosen_method_id !== 'local_pickup' )
return;
echo '<div class="wrapper-pickup_store" style="margin-top:16px">
<label class="title">' . __("Choose your pickup store") . ':</label><br>
<label for="barsha-store">
<input type="radio" id="barsha-store" name="pickup_store" value="Barsha">'
. __("Barsha") . '<a href="#"><small> '. __("Check Location") . '</small></a>
</label><br>
<label for="deira-store">
<input type="radio" id="deira-store" name="pickup_store" value="Deira">'
. __("Deira") . '<a href="#"><small> '. __("Check Location") . '</small></a>
</label>
</div>';
}
// Pickup store field validation
add_action('woocommerce_checkout_process', 'pickup_store_checkout_validation');
function pickup_store_checkout_validation() {
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods')['0'];
$chosen_method_id = explode(':', $chosen_shipping_methods);
$chosen_method_id = reset($chosen_method_id);
if( $chosen_method_id === 'local_pickup' && empty( $_POST['pickup_store'] ) )
wc_add_notice( __("Please choose a pickup store"), "error" );
}
// Save chosen Pickup store as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'pickup_store_update_order_meta' );
function pickup_store_update_order_meta( $order ) {
if( isset( $_POST['pickup_store'] ) && ! empty( $_POST['pickup_store'] ) )
$order->update_meta_data( 'pickup_store', esc_attr( $_POST['pickup_store'] ) );
}
// Display the chosen pickup store under billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_pickup_store_on_order_edit_pages' );
function display_pickup_store_on_order_edit_pages( $order ){
if( $pickup_store = $order->get_meta( 'pickup_store' ) )
echo '<p><strong>Pickup store:</strong> '.$pickup_store.'</p>';
}
// Display the chosen pickup store below the chosen shipping method everywhere
add_filter( 'woocommerce_get_order_item_totals', 'display_pickup_store_on_order_item_totals', 1000, 3 );
function display_pickup_store_on_order_item_totals( $total_rows, $order, $tax_display ){
if( $pickup_store = $order->get_meta( 'pickup_store' ) ) {
$new_total_rows = [];
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
$new_total_rows[$key] = $values;
// Inserting the pickup store under shipping method
if( $key === 'shipping' ) {
$new_total_rows['pickup_store'] = array(
'label' => __("Pickup store"),
'value' => $pickup_store
);
}
}
return $new_total_rows;
}
return $total_rows;
}