我正在尝试根据购物车中产品的库存状态在购物车中输出预计交货日期。
我有点成功,但现在我被困住了。
这是我到目前为止写的。它进入 function.php 文件
function lieferzeit() {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach ($cart_items as $variation) {
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
}
echo $stock; //outputs the in stock amount successfully
}
add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');
现在我想添加估计日期,但我卡住了
function lieferzeit() {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach ($cart_items as $variation) {
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
}
for ($i=0; $i < count($stock) ; $i++) {
echo "Voraussichtliche Lieferung Date! ";
}
}
add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');
这里必须定义日期输出。从今天+1 天到今天+4 天。但我不知道如何管理它。 最好的输出是这种格式:
预计周五发货。 14.7 - 周三。 19.7
我什至不确定
for ($i=0; $i < count($stock) ; $i++) {
是正确的方法。
我有两种产品,一种可以在 1-4 天内发货,另一种可以在 14-21 天内发货。现在是第二个问题。当两种类型都在购物车中时,应选择较长的运输时间。
有什么想法吗?
更新:
该代码应检查购物车中每件商品的库存数量。 如果所有商品的库存数量都大于 0,则它应反射(reflect)作为日期给出的 1 到 4 个工作日的估计运输时间。
如果购物车中有一件商品的库存数量为 0 或以下,它应该回显作为日期给出的 14 - 21 个工作日的估计运输时间。即使购物车中所有其他商品的库存数量都大于 0。
工作日应该是周一到周五。如果代码也能识别假期,那就太好了,例如圣诞节、新年等。
谢谢
The solution from LoicTheAztec works perfect. Now I tried to add some more option to it.
如果function lieferzeit()的输出就好了将显示在管理订单详细信息页面中。
在我发现的侧边栏中创建自定义管理面板
add_action( 'add_meta_boxes', 'add_meta_boxes' );
function add_meta_boxes()
{
add_meta_box(
'woocommerce-order-my-custom',
__( 'Order Custom' ),
'order_my_custom',
'shop_order',
'side',
'default'
);
}
function order_my_custom()
{
echo $lieferzeit;
}
这到目前为止有效,并且在管理页面中有一个订单自定义选项卡。现在我尝试存储 function lieferzeit() 的输出在变量中。
$from = str_replace($days_en, $days_ge, $from);
$to = str_replace($days_en, $days_ge, $to);
$lieferzeit = array($from, $to);
但似乎function add_meta_boxes()和 function order_my_custom()对变量一无所知 $lieferzeit .
是否有另一种方法来存储和调用 function lieferzeit() 的输出? ?
最佳答案
更新 4(2018 年 9 月)
下面的代码将检查购物车中每件商品的库存数量。
1) 如果所有购物车商品都“有货”,它将回应作为日期给出的 1 到 4 个工作日的预计运送时间。
2) 如果一个购物车商品“缺货”,它将回显作为日期给出的 14 到 21 个工作日的预计运送时间。
但我不会认节假日
这是代码:
add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');
function lieferzeit() {
$all_items_in_stock = true; // initializing
// Iterating through cart items (to get the stock info)
foreach (WC()->cart->get_cart() as $cart_item) {
// The cart item stock quantity
$stock = $cart_item['data']->get_stock_quantity();
if( $stock <= 0 ){
// if an item is out of stock
$all_items_in_stock = false;
break; // We break the loop
}
}
// Items "in stock" (1 to 4 week days)
if( $all_items_in_stock ){
for( $start=0, $count=-1 ; $count < 4; $start++ ){
$weekdays = date('w', strtotime("+$start days"));
if( $weekdays > 0 && $weekdays < 6 ){
$count++;
echo date('D j (w)', strtotime("+$start days")).', ';
if($count == 1){
$from = date('D. j/n', strtotime("+$start days") );
} elseif($count == 4) {
$to = date('D. j/n', strtotime("+$start days") );
}
}
}
} else { // 1 is Items Out of stock (14 to 21 week days)
for( $start=0, $count=-1 ; $count < 21; $start++ ){
$weekdays = date('w', strtotime("+$start days"));
if( $weekdays > 0 && $weekdays < 6 ){
$count++;
if($count == 14){
$from = date('D. j/n', strtotime("+$start days") );
} elseif($count == 21) {
$to = date('D. j/n', strtotime("+$start days") );
}
}
}
}
## TRANSLATION ##
// DAYS IN ENGLISH (Source)
$days_en = array('Mon','Tue','Wed','Thu','Fri');
// TRANSLATE the DAYS in GERMAN (replacement)
$days_ge = array('Mmm','Ttt','Www','Thh','Fff');
$from = str_replace( $days_en, $days_ge, $from );
$to = str_replace( $days_en, $days_ge, $to );
## OUTPUT ##
echo "<br><br>Estimated shipping $from - $to";
}
代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。
这段代码已经过测试并且可以工作
关于php - 显示基于 WooCommerce 购物车商品库存的预计交货日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051121/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格: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并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的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
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s