我们出售餐盒订阅,“文化盒”。客户还可以购买一次性的单盒。同一购物车中可以同时拥有订阅和多个单盒。
结帐时,在表格之前,我们有一排通知框,每个通知框都以购物车中的类别为条件。框 #1 显示您是否获得订阅,框 #2 显示您是否购买单个框。
我们还需要显示一个框#3,显示下一个交货日/月/日期,它从后端选项页面上的 ACF 组中提取值。
这是我们的 Box 1 & 2 代码。 (我们不是真正的 PHP 程序员...)
add_action( 'woocommerce_before_checkout_form', 'jbc_list_subs_note1', 12 );
function jbc_list_subs_note1() {
// set your products IDs here:
$product_ids = array(16773);
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( in_array( $item->id, $product_ids ) )
$bool = true;
}
// If the special ID is detected in one items of the cart
// It displays the message
if ($bool)
echo '<div class="jbc-check-subs-note1">
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">You'll receive</p>
</div>
<div class="jbc-check-subs-note1-div2">
<p class="jbc-check-subs-note-text-mid-l"> 1 Culture Box</p>
</div>
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">each month until you cancel.</p>
</div>
</div>';
}
?>
<?php
add_action( 'woocommerce_before_checkout_form', 'jbc_list_subs_note2', 12 );
function jbc_list_subs_note2() {
// set your special category name, slug or ID here:
$special_cat = '168';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( $special_cat, 'product_cat', $item->id ) )
$bool = true;
}
// If the special cat is detected in one items of the cart
// It displays the message
if ($bool)
echo '<div class="jbc-check-subs-note2">
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">You are ordering our</p>
</div>
<div class="jbc-check-subs-note2-div2">
<p class="jbc-check-subs-note-text-mid-l">Featured Box</p>
</div>
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">as a one-time purchase.</p>
</div>
</div>';
}
我们当前的 Box #3 代码是这样的(当然不起作用):
<?php
add_action( 'woocommerce_before_checkout_form', 'jbc_list_subs_note3', 12 );
function jbc_list_subs_note3() {
echo '<div class="jbc-check-subs-note2">
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">Our next deliveries are scheduled for</p>
</div>
<div class="jbc-check-subs-note2-div2">
<?php
$Delivery = get_field('JBC_order_delivery');
if( $Delivery ): ?>
<div id="Delivery">
<?php if ( have_rows( 'JBC_order_delivery', 'option' ) ) : ?>
<?php while ( have_rows( 'JBC_order_delivery', 'option' ) ) : the_row(); ?>
<?php the_sub_field( 'JBC_order_delivery-day' ); ?>
<?php the_sub_field( 'JBC_order_delivery-month' ); ?>
<?php the_sub_field( 'JBC_order_delivery-date' ); ?>
<?php endwhile; ?>
<?php endif;
?>
</div>
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">via courier</p>
</div>
</div>';
}
我们需要做什么才能让 ACF 选项字段值显示在第 3 框内?
您的代码中有多个错误,您可以将代码合并到一个函数中,因为您使用的是同一个钩子。
尝试以下修改后的代码:
add_action( 'woocommerce_before_checkout_form', 'jbc_list_subs_notes', 12 );
function jbc_list_subs_notes() {
// set your products IDs here:
$targeted_product_ids = array(16773);
$product_found = false;
// set your special category name, slug or ID here:
$targeted_categories = array(168);
$category_found = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// Check for products Ids
if ( in_array( $item['product_id'], $targeted_product_ids ) ) {
$product_found = true;
}
// Check for products categories
if ( has_term( $targeted_categories, 'product_cat', $item['product_id'] ) ) {
$category_found = true;
}
}
// If the special ID is detected in one items of the cart
// It displays the message
if ( $product_found ) {
echo '<div class="jbc-check-subs-note1">
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">You'll receive</p>
</div>
<div class="jbc-check-subs-note1-div2">
<p class="jbc-check-subs-note-text-mid-l"> 1 Culture Box</p>
</div>
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">each month until you cancel.</p>
</div>
</div>';
}
// If the special cat is detected in one items of the cart
// It displays the message
if ( $category_found ) {
echo '<div class="jbc-check-subs-note2">
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">You are ordering our</p>
</div>
<div class="jbc-check-subs-note2-div2">
<p class="jbc-check-subs-note-text-mid-l">Featured Box</p>
</div>
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">as a one-time purchase.</p>
</div>
</div>';
}
// Delivery section
if( $Delivery = get_field('JBC_order_delivery') ) {
echo '<div class="jbc-check-subs-note2">
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">Our next deliveries are scheduled for</p>
</div>
<div class="jbc-check-subs-note2-div2">
<div id="Delivery">';
if ( have_rows( 'JBC_order_delivery', 'option' ) ) :
while ( have_rows( 'JBC_order_delivery', 'option' ) ) : the_row();
the_sub_field( 'JBC_order_delivery-day' );
the_sub_field( 'JBC_order_delivery-month' );
the_sub_field( 'JBC_order_delivery-date' );
endwhile;
endif;
echo '</div>
<div class="jbc-check-subs-note1-div1">
<p class="jbc-check-subs-note-txt-top-s">via courier</p>
</div>
</div>';
}
}
它应该可以工作(无法真正测试ACF字段)。