【问题标题】:Woocommerce Additional Message on Product VariationWoocommerce 关于产品变化的附加信息
【发布时间】:2017-12-06 10:21:08
【问题描述】:

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

我已经尝试过添加一个额外的属性类型:

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

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

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

【问题讨论】:

  • 是的,这是一个不错的解决方案,这也需要保存为产品元数据并添加到订单中,最终我自己想通了,但如果将来有人需要,这是一个很好的话题.谢谢@LoicTheAztec
  • 我有女仆类似的完整答案相关,所以如果你搜索一下,你会发现所有的过程和钩子......

标签: php wordpress woocommerce product custom-fields


【解决方案1】:

使用挂在 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 操作挂钩,如果您想在添加到购物车按钮下显示此自定义字段,

但您应该需要保存将此自定义字段添加到购物车时在购物车对象中,并在购物车和结帐页面中显示

【讨论】:

    猜你喜欢
    • 2018-10-17
    • 2018-06-06
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2019-08-21
    • 2014-08-14
    相关资源
    最近更新 更多