如何在 HTML/PHP 中引用自定义产品类型值(WordPress 和 Woocommerce)
How to reference a custom product type value in HTML/PHP (WordPress & Woocommerce)
我有一个自定义按钮,而不是 'add to cart' 按钮。它应该为品牌打开一个 link:id='_brands_link' 并且在按钮中,在“Bestel bij”之后应该有品牌名称 id='_brands_name'(查看骨积中创建的字段):
// name
function add_brands_name_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_name',
'label' => __( 'Brand name', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_name', true ),
'default' => '',
'placeholder' => 'Enter brand name',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
// link
function add_brands_link_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_link',
'label' => __( 'Product link', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_link', true ),
'default' => '',
'placeholder' => 'Enter product link',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');
我现在有这个按钮的功能了:
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="https://www.google.com">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij Google <?php ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
所以在行动中应该 _brands_link 而不是 https://www.google.com。而不是按钮中的“Google”,应该有 _brands_name
以下是我的自定义产品类型的所有代码,以防万一:
// BEGIN CODE
class WC_Product_brands extends WC_Product_Simple {
// Return the product type
public function get_type() {
return 'brands';
}
}
// add the product type to the dropdown
function add_type_to_dropdown( $types ) {
$types['brands'] = __( 'Brand product', 'vicodemedia' );
return $types;
}
add_filter( 'product_type_selector', 'add_type_to_dropdown');
// add the product type as a taxonomy
function install_taxonomy() {
// If there is no brands product type taxonomy, add it.
if ( ! get_term_by( 'slug', 'brands', 'product_type' ) ) {
wp_insert_term( 'brands', 'product_type' );
}
}
register_activation_hook( __FILE__, 'install_taxonomy');
// name
function add_brands_name_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_name',
'label' => __( 'Brand name', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_name', true ),
'default' => '',
'placeholder' => 'Enter brand name',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
// link
function add_brands_link_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_link',
'label' => __( 'Product link', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_link', true ),
'default' => '',
'placeholder' => 'Enter product link',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');
// Generl Tab not showing up
add_action( 'woocommerce_product_options_general_product_data', function(){
echo '<div class="options_group show_if_brands clear"></div>';
} );
// add show_if_advanced calass to options_group
function enable_product_js() {
global $post, $product_object;
if ( ! $post ) { return; }
if ( 'product' != $post->post_type ) :
return;
endif;
$is_brands = $product_object && 'brands' === $product_object->get_type() ? true : false;
?>
<script type='text/javascript'>
jQuery(document).ready(function () {
//for link tab
jQuery('#general_product_data .pricing').addClass('show_if_brands');
<?php if ( $is_brands ) { ?>
jQuery('#general_product_data .pricing').show();
<?php } ?>
});
</script>
<style>
.woocommerce_options_panel .options_group {
border-top: 0px;
border-bottom: 0px;
}
</style>
<?php
}
add_action( 'admin_footer', 'enable_product_js');
// save data on submission
function save_brands_link( $post_id ) {
$link = isset( $_POST['_brands_link'] ) ? sanitize_text_field( $_POST['_brands_link'] ) : '';
update_post_meta( $post_id, '_brands_link', $link );
}
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_link');
// save data on submission
function save_brands_name( $post_id ) {
$name = isset( $_POST['_brands_name'] ) ? sanitize_text_field( $_POST['_brands_name'] ) : '';
update_post_meta( $post_id, '_brands_name', $name );
}
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_name');
/*
//button
function custom_product_button() {
?>
<a href="<?php the_field('_brands_link'); ?>" class="button alt">Bestel bij
<?php the_field('_brands_name'); ?></a>
<?php
}
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
*/
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="https://www.google.com">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij Google <?php ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
// END CODE
结论:
我需要了解如何正确引用我为 _brands_link 和 _brands_name 创建的 类 的对象。我不明白应该怎么做,这样它才能在 HTML 中显示为文本 (_brands_name) 和 link 形式的 action = "" (_brands_link):
function brands_add_to_cart() {
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="_brands_link">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij _brands_name
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
由于将它们保存为 post 元数据,您也可以将其作为元数据返回
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="<?php echo get_post_meta(get_the_ID(), '_brands_link', true); ?>">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij <?php echo get_post_meta(get_the_ID(), '_brands_name', true); ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
我有一个自定义按钮,而不是 'add to cart' 按钮。它应该为品牌打开一个 link:id='_brands_link' 并且在按钮中,在“Bestel bij”之后应该有品牌名称 id='_brands_name'(查看骨积中创建的字段):
// name
function add_brands_name_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_name',
'label' => __( 'Brand name', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_name', true ),
'default' => '',
'placeholder' => 'Enter brand name',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
// link
function add_brands_link_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_link',
'label' => __( 'Product link', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_link', true ),
'default' => '',
'placeholder' => 'Enter product link',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');
我现在有这个按钮的功能了:
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="https://www.google.com">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij Google <?php ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
所以在行动中应该 _brands_link 而不是 https://www.google.com。而不是按钮中的“Google”,应该有 _brands_name
以下是我的自定义产品类型的所有代码,以防万一:
// BEGIN CODE
class WC_Product_brands extends WC_Product_Simple {
// Return the product type
public function get_type() {
return 'brands';
}
}
// add the product type to the dropdown
function add_type_to_dropdown( $types ) {
$types['brands'] = __( 'Brand product', 'vicodemedia' );
return $types;
}
add_filter( 'product_type_selector', 'add_type_to_dropdown');
// add the product type as a taxonomy
function install_taxonomy() {
// If there is no brands product type taxonomy, add it.
if ( ! get_term_by( 'slug', 'brands', 'product_type' ) ) {
wp_insert_term( 'brands', 'product_type' );
}
}
register_activation_hook( __FILE__, 'install_taxonomy');
// name
function add_brands_name_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_name',
'label' => __( 'Brand name', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_name', true ),
'default' => '',
'placeholder' => 'Enter brand name',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
// link
function add_brands_link_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_link',
'label' => __( 'Product link', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_link', true ),
'default' => '',
'placeholder' => 'Enter product link',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');
// Generl Tab not showing up
add_action( 'woocommerce_product_options_general_product_data', function(){
echo '<div class="options_group show_if_brands clear"></div>';
} );
// add show_if_advanced calass to options_group
function enable_product_js() {
global $post, $product_object;
if ( ! $post ) { return; }
if ( 'product' != $post->post_type ) :
return;
endif;
$is_brands = $product_object && 'brands' === $product_object->get_type() ? true : false;
?>
<script type='text/javascript'>
jQuery(document).ready(function () {
//for link tab
jQuery('#general_product_data .pricing').addClass('show_if_brands');
<?php if ( $is_brands ) { ?>
jQuery('#general_product_data .pricing').show();
<?php } ?>
});
</script>
<style>
.woocommerce_options_panel .options_group {
border-top: 0px;
border-bottom: 0px;
}
</style>
<?php
}
add_action( 'admin_footer', 'enable_product_js');
// save data on submission
function save_brands_link( $post_id ) {
$link = isset( $_POST['_brands_link'] ) ? sanitize_text_field( $_POST['_brands_link'] ) : '';
update_post_meta( $post_id, '_brands_link', $link );
}
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_link');
// save data on submission
function save_brands_name( $post_id ) {
$name = isset( $_POST['_brands_name'] ) ? sanitize_text_field( $_POST['_brands_name'] ) : '';
update_post_meta( $post_id, '_brands_name', $name );
}
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_name');
/*
//button
function custom_product_button() {
?>
<a href="<?php the_field('_brands_link'); ?>" class="button alt">Bestel bij
<?php the_field('_brands_name'); ?></a>
<?php
}
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
*/
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="https://www.google.com">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij Google <?php ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
// END CODE
结论: 我需要了解如何正确引用我为 _brands_link 和 _brands_name 创建的 类 的对象。我不明白应该怎么做,这样它才能在 HTML 中显示为文本 (_brands_name) 和 link 形式的 action = "" (_brands_link):
function brands_add_to_cart() {
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="_brands_link">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij _brands_name
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
由于将它们保存为 post 元数据,您也可以将其作为元数据返回
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="<?php echo get_post_meta(get_the_ID(), '_brands_link', true); ?>">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij <?php echo get_post_meta(get_the_ID(), '_brands_name', true); ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}