我为我的产品页面创建了一个新功能。
它说当 _shipping_costs 为空(无数字)时,请更改“请求价格”上的按钮。
在存档页面上您可以清楚地看到它完美运行。
但我在单个产品页面上艰难地更改了按钮。
这是下一步的构建方式。
新按钮功能:
function custom_add_price_on_request_button() {
global $product;
$shipping_cost = get_post_meta($product->get_id(), '_shipping_cost', true);
if ($shipping_cost === '') {
echo '<a class="button price-on-request-button" href="mailto:[email protected]"><span>Prijs op</span><span>aanvraag</span></a>';
}
}
add_action('woocommerce_after_shop_loop_item', 'custom_add_price_on_request_button', 11);
移除按钮+添加新按钮功能:
function custom_remove_and_add_cart_button_single() {
global $product;
$shipping_cost = get_post_meta($product->get_id(), '_shipping_cost', true);
if ($shipping_cost === '') {
// Remove the default add to cart button
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
remove_action('woocommerce_single_product_summary', 'woocommerce_single_variation_add_to_cart_button', 30);
// Add a custom 'Prijs op aanvraag' button
add_action('woocommerce_single_product_summary', 'custom_add_price_on_request_button', 35);
}
}
add_action('woocommerce_before_single_product_summary', 'custom_remove_and_add_cart_button_single', 1);
正如您所看到的,它显示 IF Shippingcosts === ''(空)而不是删除“添加到购物车”按钮并将其更改为“新功能按钮”。
正如您在存档页面上看到的那样,它工作正常,但在单个产品页面上则不然。有人看到我使用的 Woocommerce Hook 有什么问题吗?
我找到了一些包含 Wooks 信息的网站,但我看不到如何修复它:https://wcsuccessacademy.com/woocommerce-visual-hook-guide-single-product-page/
我也尝试过添加调试,但它们当然不会显示,因为它不是通过打开页面直接主动调用日志。
function custom_remove_cart_form_single() {
global $product;
$shipping_cost = get_post_meta($product->get_id(), '_shipping_cost', true);
if (is_admin()) {
return; // Prevent logging in the admin area
}
echo '<div class="custom-debug">';
echo 'Product ID: ' . $product->get_id() . '<br>';
echo 'Shipping Cost Meta: ' . print_r($shipping_cost, true) . '<br>';
if ($shipping_cost === '') {
echo 'Removing Add to Cart button for product ID: ' . $product->get_id() . '<br>';
// Remove the default add to cart button
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
remove_action('woocommerce_single_product_summary', 'woocommerce_single_variation_add_to_cart_button', 30);
// Add custom 'Prijs op aanvraag' button
add_action('woocommerce_single_product_summary', 'custom_add_price_on_request_button', 35);
}
echo '</div>';
}
add_action('woocommerce_before_single_product_summary', 'custom_remove_cart_form_single', 1);