目录
Toggle继续分享wordpress建站教程。最近我自己用woocommerce制作了一个虚拟产品销售的独立站,因为是教程、模板类的产品,可下载可复制,所以单个产品就不需要数量选项,防止用户重复购买。这个需求有插件可以实现,不过我使用的是下面两段代码,分享给大家,有需要自取。
// 取消产品数量,只能购买一个
function wc_remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
上面的代码可以直接复制到主题的functions.php文件中,或者是使用wp code之类的插件添加,启用之后就可以隐藏产品的数量选项,每个产品只能添加一个到购物车。如果继续添加,它会提示错误。
但是,默认的提示消息是You cannot add another’xxx’to your cart,并不是很友好,客户也不太容易理解。所以接下来我们需要再修改一下这个提示消息。
//修改数量限制错误提示信息
function filter_woocommerce_cart_product_cannot_add_another_message( $message, $product_data ) {
// New text
$message = __( 'The template is a virtual product, which can be copied, and you only need to purchase the same template once, which is valid forever.', 'woocommerce' );
return $message;
}
add_filter( 'woocommerce_cart_product_cannot_add_another_message', 'filter_woocommerce_cart_product_cannot_add_another_message', 10, 2 );
上面的代码中【The template is a virtual product, which can be copied, and you only need to purchase the same template once, which is valid forever.】的内容可以自行修改。但有一点要注意,提示文本中的某些标点可能会和代码的相影响,比如【don’t】中的那个小点和代码中的点就有冲突,如果提示文本出现了这样的词,请修改一下。
同样代码添加到主题的functions.php文件中,或者是使用wp code之类的插件。
如上图所示,错误提示变成了上面修改的内容,这样客户就好理解了。
上面的代码仅在我自己的wordpress建站项目中使用,不保证100%能用,建议先在测试环境使用。销售虚拟产品其实woocommerce并不是最好的选择,有其它专门的虚拟产品销售插件可以用,这个看自己习惯吧,能用就行,重要的是网站有流量,有销量。
© Copyright 2024. 悦然网络工作室/悦然wordpress建站 专注中小企业wordpress建站 All Rights Reserved.网站地图
本站图片来源为Pexels、Pixabay、Freepik、Unsplash等图片库的免费许可,CC0协议;还有部分为自己手绘,版权碰瓷请自重!法律服务:law@yueranseo.com 蜀ICP备20016391号-1 川公网安备 51011502000367号