我正在尝试在 Wordpress 上创建一个自定义页面,以一种形式显示 2 种订阅类型,每种类型有 3 个变体(每月、6 个月、12 个月)。每个变体都有一个单选按钮,当用户单击单选按钮时,我有一个实时更新的总价。这部分正在运行。
但是现在,我想添加 3 个其他单选按钮来选择装运方式。 (当用户选择一个时,它也会实时更新总价)。
我已经搜索了很长时间如何获取产品的运费,但没有任何效果。
有人知道 :) 吗?
最佳答案
这个问题太宽泛了。所以我可以部分回答,因为你应该需要自己做一些工作,然后再问更具体的问题......
Now the correct way to set shipping data on product page, is to use Ajax to update the data, as the action is made by the customer on client side (browser), avoiding 'post' and reload the page. But this should be your work...
1) 客户位置 (对于运输区域):
您应该需要先获取客户位置或送货区域。
然后您需要在 WC()->session 和 WC()->customer 对象中更新客户国家。这可以通过以下方式完成:
## Get the geolocated customer country code *(if enabled)*:
$country_code = WC()->customer->get_billing_country();
// or
// $country_code = WC()->customer->get_shipping_country();
## Set a new country code
$new_country_code = 'FR';
## 1. WC_session: set customer billing and shipping country
// Get the data
$customer_session = WC()->session->get( 'customer' );
// Change some data
$customer_session['country'] = $new_country_code; // Billing
$customer_session['shipping_country'] = $new_country_code; // Shipping
// Set the changed data
$customer_session = WC()->session->set( 'customer', $customer_session );
## 2. WC_Customer: set customer billing and shipping country
WC()->customer->set_billing_country( $new_country_code );
WC()->customer->set_shipping_country( $new_country_code );
2) 运输方式 (按运输区域,有费用):
在 Woocommerce 中,运输区域的运输方法仅在客户将产品添加到购物车时可用......
基于此答案代码:Display shipping methods to frontend as in the admin panel?
我们可以创建一个自定义数组,其中包含必要的数据,用于通过运输区域获取运输方式、成本和所需的一切。
下面的代码更完整,包括运输方式的费用:
// Initializing variable
$zones = $data = $classes_keys = 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() );
// Shipping Classes
$shipping = new \WC_Shipping();
$shipping_classes = $shipping->get_shipping_classes();
// The Shipping Classes for costs in "Flat rate" Shipping Method
foreach($shipping_classes as $shipping_class) {
//
$key_class_cost = 'class_cost_'.$shipping_class->term_id;
// The shipping classes
$classes_keys[$shipping_class->term_id] = array(
'term_id' => $shipping_class->term_id,
'name' => $shipping_class->name,
'slug' => $shipping_class->slug,
'count' => $shipping_class->count,
'key_cost' => $key_class_cost
);
}
// For 'No class" cost
$classes_keys[0] = array(
'term_id' => '',
'name' => 'No shipping class',
'slug' => 'no_class',
'count' => '',
'key_cost' => 'no_class_cost'
);
foreach ( $shipping_zones as $shipping_zone ) {
$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']; // array
$zone_location_name = $shipping_zone['formatted_zone_location'];
// Set the data in an array:
$data[$zone_id]= array(
'zone_id' => $zone_id,
'zone_name' => $zone_name,
'zone_location_name' => $zone_location_name,
'zone_locations' => $zone_locations,
'shipping_methods' => array()
);
foreach ( $shipping_zone['shipping_methods'] as $sm_obj ) {
$method_id = $sm_obj->id;
$instance_id = $sm_obj->get_instance_id();
$enabled = $sm_obj->is_enabled() ? true : 0;
// Settings specific to each shipping method
$instance_settings = $sm_obj->instance_settings;
if( $enabled ){
$data[$zone_id]['shipping_methods'][$instance_id] = array(
'$method_id' => $sm_obj->id,
'instance_id' => $instance_id,
'rate_id' => $sm_obj->get_rate_id(),
'default_name' => $sm_obj->get_method_title(),
'custom_name' => $sm_obj->get_title(),
);
if( $method_id == 'free_shipping' ){
$data[$zone_id]['shipping_methods'][$instance_id]['requires'] = $instance_settings['requires'];
$data[$zone_id]['shipping_methods'][$instance_id]['min_amount'] = $instance_settings['min_amount'];
}
if( $method_id == 'flat_rate' || $method_id == 'local_pickup' ){
$data[$zone_id]['shipping_methods'][$instance_id]['tax_status'] = $instance_settings['tax_status'];
$data[$zone_id]['shipping_methods'][$instance_id]['cost'] = $sm_obj->cost;
}
if( $method_id == 'flat_rate' ){
$data[$zone_id]['shipping_methods'][$instance_id]['class_costs'] = $instance_settings['class_costs'];
$data[$zone_id]['shipping_methods'][$instance_id]['calculation_type'] = $instance_settings['type'];
$classes_keys[0]['cost'] = $instance_settings['no_class_cost'];
foreach( $instance_settings as $key => $setting )
if ( strpos( $key, 'class_cost_') !== false ){
$class_id = str_replace('class_cost_', '', $key );
$classes_keys[$class_id]['cost'] = $setting;
}
$data[$zone_id]['shipping_methods'][$instance_id]['classes_&_costs'] = $classes_keys;
}
}
}
}
// Row output (for testing)
echo '<pre>'; print_r($data); echo '</pre>';
custom shipping methods
Now if you are using custom shipping methods (enabled sometimes by 3rd party shipping plugins) you will need to make some changes in the code…Costs and taxes calculation
You should need to make the taxes calculations, as the costs are displayed just as they are set in shipping settings…
3) 产品页面
客户所在地:
您首先需要有一个位置选择器(以定义送货区域)或根据 Woocommerce 地理位置设置位置。
送货方式:
定义运输区域后,您可以获得相应的运输方式和费率 (成本),在此产品页面上显示您的运输方式单选按钮。
为此,您需要更改单个产品页面:
WC_Session 和 WC_Customer).您应该需要使用以下代码 (Ajax) 获取/设置/更新“chosen_shipping_methods”。
获取选择的送货方式:
$chosen_shipping = WC()->session->get('chosen_shipping_methods')[0];
设置/更新选择的运送方式(通过 Javascript/Ajax 和 admin-ajax.php):
// HERE the new method ID
$method_rate_id = array('free_shipping:10');
// Set/Update the Chosen Shipping method
WC()->session->set( 'chosen_shipping_methods', $method_rate_id );
关于php - 在产品页面上显示运费 - WooCommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47409734/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
注意:本文主要掌握DCN自研无线产品的基本配置方法和注意事项,能够进行一般的项目实施、调试与运维AP基本配置命令AP登录用户名和密码均为:adminAP默认IP地址为:192.168.1.10AP默认情况下DHCP开启AP静态地址配置:setmanagementstatic-ip192.168.10.1AP开启/关闭DHCP功能:setmanagementdhcp-statusup/downAP设置默认网关:setstatic-ip-routegeteway192.168.10.254查看AP基本信息:getsystemgetmanagementgetmanaged-apgetrouteAP配
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
基础版云数据库RDS的产品系列包括基础版、高可用版、集群版、三节点企业版,本文介绍基础版实例的相关信息。RDS基础版实例也称为单机版实例,只有单个数据库节点,计算与存储分离,性价比超高。说明RDS基础版实例只有一个数据库节点,没有备节点作为热备份,因此当该节点意外宕机或者执行重启实例、变更配置、版本升级等任务时,会出现较长时间的不可用。如果业务对数据库的可用性要求较高,不建议使用基础版实例,可选择其他系列(如高可用版),部分基础版实例也支持升级为高可用版。基础版与高可用版的对比拓扑图如下所示。优势 性能由于不提供备节点,主节点不会因为实时的数据库复制而产生额外的性能开销,因此基础版的性能相对于
我刚刚按照thebootsygempage上的安装说明进行操作在我保存并查看帖子内容之前,一切看起来都不错。这是输出在View中的样子:HeaderSubhead:似乎没有呈现任何html格式,因为它被引号或类似的东西转义了-其他人有这个问题吗?我没有在github页面或SO上看到任何问题来指出我正确的方向。除了遵循gem安装说明之外,我还没有做任何事情,但也许我错过了什么或者只是犯了一个愚蠢的错误。如果你还有什么想知道的,请尽管问。干杯 最佳答案 你需要有这样的东西,转义html: 关