我试图将WordPress中的“添加到购物车”按钮替换为“发送查询”,该按钮发送关于产品的查询,绕过购物车选项并在联系人表单中使用,
使用简单的CSS禁用“添加到购物车”按钮。
我试着在每个产品中添加“联系人表格7”,但这并没有解决,
我没有想法了,有人能引导我走一条可行的路吗?
发布于 2019-06-17 13:40:55
我把这个作为回答,因为它渴望得到评论。我不知道这是否符合你的需要,因为它有点复杂,我也链接到一个独立的插件,我成功地使用了。因此,如果答案不合适,可以自由地投票另一个!
您说过您刚刚通过CSS禁用了add按钮,但这是不安全的,因为该按钮仍然在那里,仅不可见。相反,您应该通过可用的WooCommerce钩子删除按钮,类似于:(更新)
// remove add to cart button for every product
add_action( 'init', 'prfx_remove_add_to_cart_button');
function prfx_remove_add_to_cart_button() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10); // catalog page
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); // single product page
}或者您也可以使用WooCommerce过滤器并禁用购买这些产品(“添加到购物车”按钮消失),例如:
add_filter('woocommerce_is_purchasable', 'prfx_is_product_purchasbale');
function prfx_is_product_purchasbale($purchasable, $product) {
// you could check agains a meta option, or maybe if a shortcode exists in this product
if ($product->id == 132) {
$purchasable = false;
}
return $purchasable;
}无论如何,我使用以下设置实现了类似的系统:
我使用了以下插件:
“联系人表单7从URL获取并显示参数”插件上一次更新是很久以前的,而且这段旧代码已经出现了一些问题。其他人和我自己也创建了这个插件的固定版本,例如:https://github.com/LWS-Web/contact-form-7-get-and-show-parameter-from-url
该插件可用于以下方面:
在使用Contact表单7 WordPress插件时,有时需要将参数从URL传递到隐藏字段或在表单中显示。这个插件非常适合传递诸如订单号、选定的包,甚至安全信息等信息。
The的想法是:在产品页面上创建一个链接,其中包含产品标题,例如产品的ID。如果单击链接,联系人表单将打开,产品的标题和ID仍可通过URL参数获得。标题和ID可以显示在表单和邮件上。
以下是我所做的事情的总结:
1.创建一个新的联系人表单7,并将其插入到一个带有段塞式product-inquiry的页面中。
2.在单个产品页面上创建一个链接,例如:
Product Inquiry因此,在前端/浏览器中,这个链接看起来像/指向:https://example.com/product-inquiry/?product-title=My Product&product-id=132
(UPDATE)您应该使用WooCommerce挂钩来插入新按钮而不是旧按钮。另外,如果你使用变体,它会很快变得复杂。例如,您可以使用以下内容插入查询按钮:
// add inquiry button into every product
// we use the same hooks as before, to insert the new content on the same spot
add_action( 'woocommerce_after_shop_loop_item', 'prfx_add_inquiry_button', 10); // catalog page
add_action( 'woocommerce_single_product_summary', 'prfx_add_inquiry_button', 30); // single product page
function prfx_add_inquiry_button() {
$current_product_id = get_the_ID(); // get ID of the current post
$current_product_title = get_the_title(); // get the title of the current post
$product = wc_get_product( $current_product_id ); // get the product object
// create a button for variable/variant products
if ($product->is_type( 'variable' )) {
$variations = $product->get_available_variations(); // get all available variations of the current product
if ($variations) {
foreach ($variations as $variation) {
if ($variation['variation_is_active'] == 1) {
echo '';
// use this to see all available data
// print_r($variation);
$variation_price_html = $variation['price_html']; // variation price html, for example "€150,00 incl. vat"
$variation_id = $variation['variation_id']; // variation ID, for example "2006"
$variation_title = get_the_title( $variation_id ); // variation title, for example "Beanie with Logo - red, medium"
// we create a button for each single available variation
echo '';
echo $variation_title; // show title
echo '
';
echo $variation_price_html; // show price html
echo '';
echo '
';
}
}
}
// create a button for simple products
} elseif ($product->is_type( 'simple' )) {
echo '';
echo $current_product_title;
echo '';
}
}如果单击该链接,则打开带有查询表单的页面,您将在url中看到产品标题和ID作为参数可用。
因此,现在您可以使用一些短代码(来自联系人表单7 Get和插件)来显示和使用这些URL参数。
[getparam product-title] >获取“product”URL参数的值。(在前端不可见) [showparam product-title] >在联系人表单中显示/使用该值。
[getparam product-id] ...和上面的[showparam product-id]一样..。与上述相同
3.编辑查询表单并在表单上插入一些带有短代码的文本,如下所示:
[getparam product-title][getparam product-id]
Inquiry for product: [showparam product-title] ([showparam product-id]) 这将比在前端显示文本更像Inquiry for product: My Product (132)。
4.最后,编辑表单的邮件设置。在这里,您可以使用像[product-title]和[product-id]这样的短代码将这些值输入发送邮件!可用的短代码列在页面的顶部。
https://wordpress.stackexchange.com/questions/340682
复制相似问题