目前我使用content-single-product.php和content-product.php来覆盖每个产品,因为我使用ACF来检查某些内容是否属实。
他们说我应该使用functions.php或者其他东西,因为在模板中覆盖是不好的。我怎样才能改变我在powers.php中使用的当前代码呢?
内容单prduct.php:
// Hide price if checked
$showPriceNo = get_field('show_price');
if ($showPriceNo) {
echo "JAAAA";
//remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
//add_filter( 'woocommerce_variable_sale_price_html', 'businessbloomer_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'remove_price', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'remove_price', 10, 2 );
}
//Remove buying button
$showBuyButtonNo = get_field('show_buy_button');
if ($showBuyButtonNo) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
//Remove related products
$showRelatedProductsNo = get_field('show_related_products');
if ($showRelatedProductsNo) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
内容product.php:
// Hide price if checked
$showPriceNo = get_field('show_price');
if ($showPriceNo) {
echo "JAAAA";
add_filter( 'woocommerce_variable_sale_price_html', 'remove_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'remove_price', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'remove_price', 10, 2 );
}
//Remove buying button
$showBuyButtonNo = get_field('show_buy_button');
if ($showBuyButtonNo) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}
谢谢!
答案 0 :(得分:1)
请检查以下代码,它将适合您。
add_filter('wp_head', 'function_to_be_removed');
function function_to_be_removed()
{
global $post;
$postid= $post->ID;
// Hide price if checked
$showPriceNo = get_field('show_price',$postid);
if ($showPriceNo) {
echo "JAAAA";
//remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
//add_filter( 'woocommerce_variable_sale_price_html', 'businessbloomer_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'remove_price', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'remove_price', 10, 2 );
}
//Remove buying button
$showBuyButtonNo = get_field('show_buy_button',$postid);
if ($showBuyButtonNo) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
//Remove related products
$showRelatedProductsNo = get_field('show_related_products',$postid);
if ($showRelatedProductsNo) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
// Hide price if checked
$showPriceNo = get_field('show_price',$postid);
if ($showPriceNo) {
echo "JAAAA";
add_filter( 'woocommerce_variable_sale_price_html', 'remove_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'remove_price', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'remove_price', 10, 2 );
}
//Remove buying button
$showBuyButtonNo = get_field('show_buy_button',$postid);
if ($showBuyButtonNo) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}
}