Php 通过WooCommerce 3+;中特定类别的挂钩更改产品价格;
这个代码有什么问题?我对这个代码有一些问题Php 通过WooCommerce 3+;中特定类别的挂钩更改产品价格;,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,这个代码有什么问题?我对这个代码有一些问题 function my_woocommerce_get_price($price, $_product) { $kurs = 1.67; $new_price = $price / $kurs; $product_categories = array('laminat'); if( has_term( $product_categories, 'product_cat
function my_woocommerce_get_price($price, $_product) {
$kurs = 1.67;
$new_price = $price / $kurs;
$product_categories = array('laminat');
if( has_term( $product_categories, 'product_cat', $product->get_id() ) ) {
return $new_price; // новая цена
} else {
return $price;
}
}
add_filter('woocommerce_get_price', 'my_woocommerce_get_price', 100, 2);
当我试图保存代码时,出现了这个错误
Uncaught Error: Call to a member function get_id() on null in wp-content/themes/mrDoor/functions.php:128
Stack trace:
#0 wp-includes/class-wp-hook.php(287): my_woocommerce_get_price('919', Object(WC_Product_Simple))
#1 wp-includes/plugin.php(255): WP_Hook->apply_filters('919', Array)
#2 wp-content/plugins/woocommerce/includes/class-wc-deprecated-filter-hooks.php(142): apply_filters_ref_array('woocommerce_get...', Array)
#3 wp-content/plugins/woocommerce/includes/class-wc-deprecated-filter-hooks.php(129): WC_Deprecated_Filter_Hooks->trigger_hook('woocommerce_get...', Array)
#4 wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-deprecated-hooks.php(75): WC_Deprecated_Filter_Hooks->handle_deprecated_hook('woocommerce_pro...', 'woocommerce_get...', Array, '919')
#5 wp-include
您的代码中存在多个错误,自WooCommerce 3以来,hook
WooCommerce\u get\u price
已过时、弃用,并被以下内容取代:
forwoocommerce\u product\u get\u price
post typeproduct
forwoocommerce\u product\u variation\u get\u price
post typeproduct\u variation
相关:描述“一些问题”,然后我们可以帮助您。如果有错误消息,请提供完整的错误消息和相应的行。
$product->get\u id()
应为$\u product->get\u id()
或$\u product
应为$product
。简言之,它们并不相等,因此您将收到一条错误消息。如上文评注所示,请阅读并确认。然后写下你的问题我喜欢你在$new\u price
之后如何写。。。它们的意思都是一样的……试着打印$u产品的值,正如@7uc1f3r所提到的,它将是$u产品->get\u id()$_产品不能为空。您使用的挂钩已过时且已弃用(请参阅下面的答案以更改特定产品类别的价格)
// Simple, grouped and external products (and product variations)
add_filter( 'woocommerce_product_get_price', 'custom_product_price', 100, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_product_price', 100, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_product_price', 100, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_product_price', 100, 2 );
function custom_product_price( $price, $product ) {
$category_terms = array('laminat');
// Get the parent variable product for product variations
$product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
if( has_term( $category_terms, 'product_cat', $product_id ) ) {
$price *= 1.67; // новая цена
}
return $price;
}
// Variable products (price range)
add_filter('woocommerce_variation_prices_price', 'custom_variable_price_range', 100, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price_range', 100, 3 );
function custom_variable_price_range( $price, $variation, $product ) {
$category_terms = array('laminat');
if( has_term( $category_terms, 'product_cat', $product->get_id() ) ) {
$price *= 1.67; // новая цена
}
return $price;
}
// Handling price caching, for better performances
add_filter( 'woocommerce_get_variation_prices_hash', 'custom_variable_product_prices_hash', 100, 3 );
function custom_variable_product_prices_hash( $price_hash, $product, $for_display ) {
$category_terms = array('laminat');
if( has_term( $category_terms, 'product_cat', $product->get_id() ) ) {
$price_hash[] = '1.67';
}
return $price_hash;
}