【问题标题】:woocommerce custom product fieldwoocommerce 定制产品领域
【发布时间】:2014-07-10 23:26:09
【问题描述】:

我正在尝试使用 woocommerce 插件将自定义字段添加到产品管理屏幕,因此我可以有一个下拉菜单来选择新的或用作产品的条件。

我在管理屏幕上显示了下拉菜单,但它不会在产品前端显示新的或使用过的。

我将此代码添加到functions.php:

// Select
woocommerce_wp_select( array( 
‘id’ => ‘_conditionselect’,
‘label’ => __( ‘Condition’, ‘woocommerce’ ),
‘options’ => array(
‘one’ => __( ‘New’, ‘woocommerce’ ),
‘two’ => __( ‘Used’, ‘woocommerce’ ),
)
)
);
}

function woo_add_custom_general_fields_save( $post_id ){
// Select
$woocommerce_select = $_POST['_conditionselect'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, ‘_conditionselect’, esc_attr( $woocommerce_select ) );
}

我将其添加到了 short-description.php:

<?php _e( 'Condition: ', ‘woocommerce’ ); ?> 
<?php
echo get_post_meta( get_the_ID(), ‘_conditionselect’, true ); 
?>

知道为什么这不起作用吗?

好的,我不确定我做了什么,但现在“两个”这个词出现在前端的“条件:”旁边。但它只出现在其中一种产品上。我以为它来自这段代码:

‘two’ => __( ‘Used’, ‘woocommerce’ ), 

所以我把“二”改成了“二手”,但前端还是显示“二”。

【问题讨论】:

    标签: php wordpress woocommerce product custom-fields


    【解决方案1】:

    看完你写的代码。我观察到“get_post_meta”的语法在当前上下文中是正确的。即使您将“两个”更改为“已使用”,它仍然在前端显示“两个”的原因是您在进行这些更改后没有更新该产品。这导致显示以前的自定义字段值。

    woocommerce_wp_select 的正确语法是。

    // Stock status
            woocommerce_wp_select( array( 'id' => '_stock_status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
                'instock' => __( 'In stock', 'woocommerce' ),
                'outofstock' => __( 'Out of stock', 'woocommerce' )
            ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
    

    'instock' 和 'outofstock' 是将存储在 db 中的选项的值,'In stock' 和 'Out of stock' 显示在前端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      相关资源
      最近更新 更多