Woocommerce 关于产品变化的附加信息

Woocommerce Additional Message on Product Variation

我有一个使用 Woocommcerce 构建的 Wordpress 网站。我想为用户添加一些额外的字段,以便能够个性化他们的产品(一些文本输入让他们可以随心所欲地写)。我认为这需要使用自定义属性来完成,但我不确定。

我试过这个来添加一个额外的属性类型:

add_filter("product_attributes_type_selector" , function( $array ){
    $array["textfield"] = __( 'Textfield', 'woocommerce' );
    return $array ;
});

我不知道从哪里开始,或者这是否是正确的方法。

重要提示:我不想使用任何插件,所以请不要推荐任何插件

使用挂钩在 woocommerce_before_add_to_cart_button 操作挂钩中的自定义函数执行此操作,这将在数量字段之前添加自定义输入文本字段并添加到变量产品上的购物车按钮仅:

// Adding a custom input text field before quantities field and add to cart button on variable products only
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_fields' , 10, 0 );
function custom_product_fields() {
    global $product;

    if( ! $product->is_type( 'variable' ) ) return; // Only for variable products

    echo '<div class="product-custom-fields">
        <label class="my-custom-field1" for="custom_textfield1">'.__( 'Label text: ', 'woocommerce' ).'<br>
            <input type="text" name="custom_textfield1" class="input-text" placeholder="'.__( 'Enter a text', 'woocommerce' ).'" value="">
        </label>
    </div>
    <br clear="all">';
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

经过测试并有效……所以你会得到类似的东西:

您将使用 woocommerce_after_add_to_cart_button 操作挂钩,如果您想在添加到购物车按钮下显示此自定义字段,

But you should need to save this custom field in cart object when added to cart and to display it in cart and checkout pages too.