我正在尝试在结帐屏幕中显示以两件事为条件的自定义字段。
现在我已经处理了上面的#1 问题:
/**
* Check to see what is in the cart
*
* @param $product_id
*
* @return bool
*/
function conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
$check_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
$check_in_cart = true;
}
}
return $check_in_cart;
}
function checkout_register_names( $checkout ) {
$check_in_cart = conditional_product_in_cart(1769);
// Product id 1769 is in cart so show custom fields
if ($check_in_cart === true ) {
// Display custom fields for 1769...
woocommerce_form_field( 'golf_field_one', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Golfer #1'),
'placeholder' => __('Name'),
), $checkout->get_value( 'golf_field_one' ));
woocommerce_form_field( 'golf_field_two', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Golfer #2'),
'placeholder' => __('Name'),
), $checkout->get_value( 'golf_field_two' ));
//etc...
}
$check_in_cart = conditional_product_in_cart(1770);
// Product id 1770 is in cart so show custom fields
if ($check_in_cart === true ) {
// Display custom fields for 1770...
woocommerce_form_field( 'dinner_field_one', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Dinner Name #1'),
'placeholder' => __('Name'),
), $checkout->get_value( 'dinner_field_one' ));
woocommerce_form_field( 'dinner_field_two', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Dinner Name #2'),
'placeholder' => __('Name'),
), $checkout->get_value( 'dinner_field_two' ));
//etc...
}
}
只有当该产品在客户的购物车中时,上面的代码才会有条件地显示我在每个产品下设置的所有 woocommerce_form_field()。
现在,我需要做的是根据购物车中每种产品 #1769 或 #1770 的数量显示一定数量的 woocommerce_form_field() 。
因此,如果有两 (2) 个产品 #1769,则应该显示两个字段。如果有两 (2) 个产品 #1769 和一 (1) 个产品 #1770,则应该显示三个总字段(两个用于产品 #1769,一个用于产品 #1770)。
至多,每个给定客户的购物车中最多只会添加四个产品,因此将每个表单字段包装在一个 if() 中并没有什么大不了的,它会检查如下内容:
if([quantity of product 1769] >= 1) {
show first woocommerce_form_field()
}
if([quantity of product 1769 >= 2) {
show second woocommerce_form_field()
} //etc...
// Repeat for product 1770...
我尝试将 $qty_in_cart = $values['quantity']; 添加到第一个 function conditional_product_in_cart 中的 foreach(),但是那似乎不想给我任何东西。当我检查 if (isset($qty_in_cart)) 时,它没有设置。
我觉得我很接近,但就是想不通我错过了什么。任何帮助将不胜感激。
最佳答案
我肩负着同样的使命。 我在 WooTickets 和 WooCommerce 上使用的这段代码可能对您有所帮助:
if (
in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) &&
in_array( 'wootickets/wootickets.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
) {
/**
* Add the field to the checkout
**/
add_action('woocommerce_after_order_notes', 'wt_attendee_details');
function wt_attendee_details( $checkout ) {
$attendee_count = wt_count_attendees();
if($attendee_count > 0) {
echo "</div></div>"; //close the Billing Address section to create new group of fields
echo "<div id='attendee_details'><div>"; //automatically be closed from 2 Billing Address's div - </div></div>
echo '<h3>'.__('All Event Attendees and/or clients who are ordering DVDs, please add your name and email again.').'</h3>';
for($n = 1; $n <= $attendee_count; $n++ ) {
woocommerce_form_field( 'attendee_name_'.$n, array(
'type' => 'text',
'class' => array('form-row form-row-first'),
'label' => __('Name'),
'placeholder' => __('name'),
'required' => true,
), $checkout->get_value( 'attendee_name_'.$n ));
woocommerce_form_field( 'attendee_email_'.$n, array(
'type' => 'text',
'class' => array('form-row validate-email'),
'label' => __('Email'),
'placeholder' => __('email'),
'required' => true,
), $checkout->get_value( 'attendee_email_'.$n ));
woocommerce_form_field( 'attendee_phone_'.$n, array(
'type' => 'text',
'class' => array('form-row form-row-last'),
'label' => __('Phone'),
'placeholder' => __(''),
), $checkout->get_value( 'attendee_phone_'.$n ));
}
echo "<style type='text/css'>
#attendee_details .form-row {
float: left;
margin-right: 2%;
width: 31%;
}
#attendee_details .form-row-last {
margin-right: 0;
}
</style>";
}
//echo "Attendees: " . $attendee_count;
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'wt_attendee_fields_process');
function wt_attendee_fields_process() {
global $woocommerce;
$attendee_count = wt_count_attendees();
for($n = 1; $n <= $attendee_count; $n++ ) {
if (!$_POST['attendee_email_'.$n] || !$_POST['attendee_name_'.$n])
$error = true;
break;
}
if($error) {
$woocommerce->add_error( __('Please complete the attendee details.') );
}
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'wt_attendee_update_order_meta');
function wt_attendee_update_order_meta( $order_id ) {
$attendee_count = wt_count_attendees();
for($n = 1; $n <= $attendee_count; $n++ ) {
if ($_POST['attendee_name_'.$n]) update_post_meta( $order_id, $n.' Attendee Name', esc_attr($_POST['attendee_name_'.$n]));
if ($_POST['attendee_email_'.$n]) update_post_meta( $order_id, $n.' Attendee Email', esc_attr($_POST['attendee_email_'.$n]));
if ($_POST['attendee_phone_'.$n]) update_post_meta( $order_id, $n.' Attendee Phone', esc_attr($_POST['attendee_phone_'.$n]));
}
}
function wt_count_attendees() {
global $woocommerce;
$attendee_count = 0;
if (sizeof($woocommerce->cart->get_cart())>0) :
foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
$_product = $values['data'];
if ($_product->exists() && $values['quantity']>0) :
if (get_post_meta($_product->id, '_tribe_wooticket_for_event') > 0)
$attendee_count += $values['quantity'];
endif;
endforeach;
endif;
return $attendee_count;
}
}
?>
关于php - Woocommerce 在结帐页面上获取购物车中特定商品的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824303/
在读取/解析文件(使用Ruby)时忽略某些行的最佳方法是什么?我正在尝试仅解析Cucumber.feature文件中的场景,并希望跳过不以Scenario/Given/When/Then/And/But开头的行。下面的代码有效,但它很荒谬,所以我正在寻找一个聪明的解决方案:)File.open(file).each_linedo|line|line.chomp!nextifline.empty?nextifline.include?"#"nextifline.include?"Feature"nextifline.include?"Inorder"nextifline.include?
Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
require'mechanize'agent=Mechanize.newlogin=agent.get('http://www.schoolnet.ch/DE/HomeDE.htm')agent.clicklogin.link_withtext:/Login/然后我得到Mechanize::UnsupportedSchemeError。 最佳答案 Mechanize不支持javascript但您可以将搜索字段添加到表单并为其分配搜索词并使用mechanize提交表单form=page.forms.firstform.add_fie
据我们所知,Jekyll默认分页仅支持index.html,我想创建blog.html并在那里包含分页。有什么解决办法吗? 最佳答案 如果您创建一个名为/blog的目录并在其中放置一个index.html文件,那么您可以向_config.yml表示paginate_path:"blog/page:num"。不是使用根文件夹中的默认index.html作为分页器模板,而是使用/blog/index.html。分页器将根据需要生成类似/blog/page2/和/blog/page3/的页面。这将使您到达yourwebsite.com/b
我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用rubyonrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_
我在关注RyanbatesRailsCast的devise和omniauth(第235集-devise-and-omniauth-revised)。当我尝试使用Twitter登录时,标题中不断出现错误。defself.new_with_session(params,session)ifsession["devise.user_attributes"]new(session["devise.user_attributes"],without_protection:true)do|user|user.attributes=paramsuser.valid?end完整跟踪:C:/Ruby20
如果特定语言环境中缺少翻译,如何配置i18n以使用en语言环境翻译?当前已插入翻译缺失消息。我正在使用RoR3.1。 最佳答案 找到相似的question这里是答案:#application.rb#railswillfallbacktoconfig.i18n.default_localetranslationconfig.i18n.fallbacks=true#railswillfallbacktoen,nomatterwhatissetasconfig.i18n.default_localeconfig.i18n.fallback
情况:使用Rspec、FactoryGirl和VCR测试Rails应用程序。每次创建用户时,都会通过Stripe的API创建关联的Stripe客户。测试时,添加VCR.use_cassette或describe"...",vcr:{cassette_name:'stripe-customer'}do...到涉及用户创建的每个规范。我的实际解决方案如下:RSpec.configuredo|config|config.arounddo|example|VCR.use_cassette('stripe-customer')do|cassette|example.runendendend但这是
Ruby中如何“一般地”计算以下格式(有根、无根)的JSON对象的数量?一般来说,我的意思是元素可能不同(例如“标题”被称为其他东西)。没有根:{[{"title":"Post1","body":"Hello!"},{"title":"Post2","body":"Goodbye!"}]}根包裹:{"posts":[{"title":"Post1","body":"Hello!"},{"title":"Post2","body":"Goodbye!"}]} 最佳答案 首先,withoutroot代码不是有效的json格式。它将没有包