gpt4 book ai didi

php - 在 Woocommerce 中添加基于产品类别的自定义结帐字段

转载 作者:行者123 更新时间:2023-12-04 01:47:07 25 4
gpt4 key购买 nike

我需要根据产品类别添加自定义结帐字段。我发现此代码适用于产品,但从特定类别的每个新产品添加 ID 非常耗时。

function is_in_cart() {
// Add your special product IDs here
$ids = array( 12468, 9687, 9693);

foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
if( in_array( $cart_item['data']->get_id(), $ids ) )
return true;
}
return false;
}

/**
* Add the field to the checkout
*/
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );

function custom_checkout_field( $checkout ) {
if( ! is_in_cart() ){
woocommerce_form_field( 'special_field', array(
'type' => 'textarea',
'class' => array('form-row-wide'),
'label' => pll__('Special text'),
'required' => false,
'label_class' => array('specialfieldclass'),
'clear' => false,
), $checkout->get_value( 'special_field' ));
}
}

如何修改此代码以使用产品类别?

最佳答案

尝试以下方法来处理产品类别(也处理父术语):

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;

if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Custom conditional function that check for product categories
function is_in_cart() {
// HERE your product categories
$categories = array( 'clothing' );

foreach( WC()->cart->get_cart() as $cart_item ){
if( has_product_categories( $cart_item['product_id'], $categories ) )
return true;
}
return false;
}

// Add a checkout field before order notes
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );
function custom_checkout_field( $checkout ) {
if( ! is_in_cart() ){
woocommerce_form_field( 'special_field', array(
'type' => 'textarea',
'class' => array('form-row-wide'),
'label' => pll__('Special text'),
'required' => false,
'label_class' => array('specialfieldclass'),
'clear' => false,
), $checkout->get_value( 'special_field' ));
}
}

代码进入事件子主题(或事件主题)的 functions.php 文件。它应该有效。

关于php - 在 Woocommerce 中添加基于产品类别的自定义结帐字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54854456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com