我的产品类型IF
条件无法正常工作。意图如下:
IF
产品类型= variable
,执行X,
elseif
产品类型= simple
,做Y.
完整代码如下:
$product = new WC_Product( get_the_ID() );
if( $product->is_type( 'variable' ) ){
/* as per https://wordpress.stackexchange.com/questions/276941/woocommerce-add-extra-field-to-variation-product/277556
based on http://www.remicorson.com/woocommerce-custom-fields-for-variations/ */
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => "custom_MPN{$loop}",
'name' => "custom_MPN[{$loop}]",
'value' => get_post_meta( $variation->ID, 'custom_MPN', true ),
'label' => __( 'MPN variation', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'Manufacturer Product Number (hand-coded field).', 'woocommerce' ),
'wrapper_class' => 'form-row form-row-full',
)
);
woocommerce_wp_text_input(
array(
'id' => "custom_GTIN{$loop}",
'name' => "custom_GTIN[{$loop}]",
'value' => get_post_meta( $variation->ID, 'custom_GTIN', true ),
'label' => __( 'GTIN variation', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'Global Trade Item Number (hand-coded field).', 'woocommerce' ),
'wrapper_class' => 'form-row form-row-full',
)
);
}
function save_variation_settings_fields( $variation_id, $loop ) {
$text_field = $_POST['custom_MPN'][ $loop ];
if ( ! empty( $text_field ) ) {
update_post_meta( $variation_id, 'custom_MPN', esc_attr( $text_field ));
}
$text_field = $_POST['custom_GTIN'][ $loop ];
if ( ! empty( $text_field ) ) {
update_post_meta( $variation_id, 'custom_GTIN', esc_attr( $text_field ));
}
}
function load_variation_settings_fields( $variation ) {
$variation['custom_MPN'] = get_post_meta( $variation[ 'variation_id' ], 'custom_MPN', true );
$variation['custom_GTIN'] = get_post_meta( $variation[ 'variation_id' ], 'custom_GTIN', true );
return $variation;
}
} elseif( $product->is_type( 'simple' ) ){
/* Product identification meta data below the product title: GTIN */
// https://businessbloomer.com/woocommerce-add-text-add-cart-single-product-page/
add_action( 'woocommerce_single_product_summary', 'custom_data_above_add_to_cart_button', 6 );
function custom_data_above_add_to_cart_button( $product ) {
// https://wordpress.stackexchange.com/a/165757/135006
// Be wary - this solution was needed to make code work: https://stackoverflow.com/a/6110690/9095603
// GTIN
if ( get_post_meta( get_the_id(), 'wccaf_gtin', true ) ) {
echo '<div class="wccaf_gtin">GTIN: '.get_post_meta( get_the_id(), 'wccaf_gtin', true ).'</div>';
} else {
echo '<div class="wccaf_gtin">GTIN unspecified.</div>';
}
// MPN
if ( get_post_meta( get_the_id(), 'wccaf_mpn', true ) ) {
echo '<div class="wccaf_mpn">MPN: '.get_post_meta( get_the_id(), 'wccaf_mpn', true ).'</div>';
} else {
echo '<div class="wccaf_mpn">MPN unspecified.</div>';
}
}
}
编辑:条件未应用的结果是显示了我想要抑制的某些值,如果产品有很多变化(例如颜色,大小),如屏幕截图所示:
编辑2 - print_r($product)
给出:
WC_Product Object ( [object_type:protected] => product [post_type:protected] => product [cache_group:protected] => products [data:protected] => Array ( [name] => [slug] => [date_created] => [date_modified] => [status] => [featured] => [catalog_visibility] => visible [description] => [short_description] => [sku] => [price] => [regular_price] => [sale_price] => [date_on_sale_from] => [date_on_sale_to] => [total_sales] => 0 [tax_status] => taxable [tax_class] => [manage_stock] => [stock_quantity] => [stock_status] => instock [backorders] => no [sold_individually] => [weight] => [length] => [width] => [height] => [upsell_ids] => Array ( ) [cross_sell_ids] => Array ( ) [parent_id] => 0 [reviews_allowed] => 1 [purchase_note] => [attributes] => Array ( ) [default_attributes] => Array ( ) [menu_order] => 0 [virtual] => [downloadable] => [category_ids] => Array ( ) [tag_ids] => Array ( ) [shipping_class_id] => 0 [downloads] => Array ( ) [image_id] => [gallery_image_ids] => Array ( ) [download_limit] => -1 [download_expiry] => -1 [rating_counts] => Array ( ) [average_rating] => 0 [review_count] => 0 ) [supports:protected] => Array ( ) [id:protected] => 0 [changes:protected] => Array ( ) [object_read:protected] => 1 [extra_data:protected] => Array ( ) [default_data:protected] => Array ( [name] => [slug] => [date_created] => [date_modified] => [status] => [featured] => [catalog_visibility] => visible [description] => [short_description] => [sku] => [price] => [regular_price] => [sale_price] => [date_on_sale_from] => [date_on_sale_to] => [total_sales] => 0 [tax_status] => taxable [tax_class] => [manage_stock] => [stock_quantity] => [stock_status] => instock [backorders] => no [sold_individually] => [weight] => [length] => [width] => [height] => [upsell_ids] => Array ( ) [cross_sell_ids] => Array ( ) [parent_id] => 0 [reviews_allowed] => 1 [purchase_note] => [attributes] => Array ( ) [default_attributes] => Array ( ) [menu_order] => 0 [virtual] => [downloadable] => [category_ids] => Array ( ) [tag_ids] => Array ( ) [shipping_class_id] => 0 [downloads] => Array ( ) [image_id] => [gallery_image_ids] => Array ( ) [download_limit] => -1 [download_expiry] => -1 [rating_counts] => Array ( ) [average_rating] => 0 [review_count] => 0 ) [data_store:protected] => WC_Data_Store Object ( [instance:WC_Data_Store:private] => WC_Product_Data_Store_CPT Object ( [internal_meta_keys:protected] => Array ( [0] => _visibility [1] => _sku [2] => _price [3] => _regular_price [4] => _sale_price [5] => _sale_price_dates_from [6] => _sale_price_dates_to [7] => total_sales [8] => _tax_status [9] => _tax_class [10] => _manage_stock [11] => _stock [12] => _stock_status [13] => _backorders [14] => _sold_individually [15] => _weight [16] => _length [17] => _width [18] => _height [19] => _upsell_ids [20] => _crosssell_ids [21] => _purchase_note [22] => _default_attributes [23] => _product_attributes [24] => _virtual [25] => _downloadable [26] => _download_limit [27] => _download_expiry [28] => _featured [29] => _downloadable_files [30] => _wc_rating_count [31] => _wc_average_rating [32] => _wc_review_count [33] => _variation_description [34] => _thumbnail_id [35] => _file_paths [36] => _product_image_gallery [37] => _product_version [38] => _wp_old_slug [39] => _edit_last [40] => _edit_lock ) [extra_data_saved:protected] => [updated_props:protected] => Array ( ) [meta_type:protected] => post [object_id_field_for_meta:protected] => ) [stores:WC_Data_Store:private] => Array ( [coupon] => WC_Coupon_Data_Store_CPT [customer] => WC_Customer_Data_Store [customer-download] => WC_Customer_Download_Data_Store [customer-download-log] => WC_Customer_Download_Log_Data_Store [customer-session] => WC_Customer_Data_Store_Session [order] => WC_Order_Data_Store_CPT [order-refund] => WC_Order_Refund_Data_Store_CPT [order-item] => WC_Order_Item_Data_Store [order-item-coupon] => WC_Order_Item_Coupon_Data_Store [order-item-fee] => WC_Order_Item_Fee_Data_Store [order-item-product] => WC_Order_Item_Product_Data_Store [order-item-shipping] => WC_Order_Item_Shipping_Data_Store [order-item-tax] => WC_Order_Item_Tax_Data_Store [payment-token] => WC_Payment_Token_Data_Store [product] => WC_Product_Data_Store_CPT [product-grouped] => WC_Product_Grouped_Data_Store_CPT [product-variable] => WC_Product_Variable_Data_Store_CPT [product-variation] => WC_Product_Variation_Data_Store_CPT [shipping-zone] => WC_Shipping_Zone_Data_Store [webhook] => WC_Webhook_Data_Store ) [current_class_name:WC_Data_Store:private] => WC_Product_Data_Store_CPT [object_type:WC_Data_Store:private] => product-simple ) [meta_data:protected] => )
答案 0 :(得分:0)
这是一个经过测试的工作条件,适用于您的情况:
add_action( 'wp_head', 'wc_variable_simple_conditions' );
function wc_variable_simple_conditions() {
if(is_product()){
global $post;
$post_id = $post->ID;
$product = wc_get_product( $post_id );
$type = $product->get_type();
if( $type == 'variable' ){
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
} elseif( $type == 'simple' ){
add_action( 'woocommerce_single_product_summary', 'custom_data_above_add_to_cart_button', 6 );
}
}
}
如果使用此功能无法解决问题,则可能需要修改功能。