当我添加$product->get_id()
时,我得到了HTTP Error 500
并在错误日志中收到此消息:
[13-Mar-2018 08:47:07 UTC] PHP致命错误:调用成员函数 get_id()on null in /home/healthnwellness/public_html/wp-content/themes/betheme-child/functions.php 在第926行
我的功能在functions.php
:
//Function return price (commission based) as per their purchase count
function return_custom_price($price, $product) {
global $product;
$current_user = wp_get_current_user();
$count_purchase = wc_product_sold_count();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) )
{
if($count_purchase == 1){
$price = get_post_meta( $product->get_id(), '_comm_1_kit');
$post_id = $product->get_id();
$price = ($price[0]);
return $price;
}
elseif($count_purchase == 2){
$price = get_post_meta( $product->get_id(), '_comm_2_kit');
$post_id = $product->get_id();
$price = ($price[0]);
return $price;
}
elseif($count_purchase >= 3){
$price = get_post_meta($product->get_id(), '_comm_3_kit');
$post_id = $product->get_id();
$price = ($price[0]);
return $price;
}
}
else
{
$price = get_post_meta($product->get_id(), '_discounted_price');
$post_id = $product->get_id();
$price = ($price[0]);
return $price;
}
}
如何解决此错误?我做错了什么?
感谢任何帮助。
注意:wc_product_sold_count()
函数来from this answer,并且已更改为仅返回计数。
答案 0 :(得分:0)
rtype
错误的任务。 $product->get_id() = $_SESSION["iddd"];
返回一个int,并尝试使用其他值设置它。
它现在也出现在get_id()
之前,因此它将为空。
答案 1 :(得分:0)
您的代码中存在一些缺少的部分和许多错误。
您需要使用laso来稍微更改一下wc_product_sold_count()
from this answer。
所以试试这个:
function wc_product_sold_count_for_user( $product, $user_id ) {
global $wpdb;
$product_id = $product->get_id(); // Current Product ID
// The SQL request
return $wpdb->get_var( "
SELECT SUM(woim2.meta_value)
FROM {$wpdb->prefix}woocommerce_order_items AS woi
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim2 ON woi.order_item_id = woim2.order_item_id
INNER JOIN {$wpdb->prefix}postmeta pm ON woi.order_id = pm.post_id
INNER JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.ID
WHERE woi.order_item_type LIKE 'line_item'
AND p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-completed','wc-processing')
AND pm.meta_key = '_customer_user'
AND pm.meta_value = '$user_id'
AND woim.meta_key = '_product_id'
AND woim.meta_value = '$product_id'
AND woim2.meta_key = '_qty'
");
}
//Function return price (commission based) as per their purchase count
add_filter('woocommerce_product_get_price','return_custom_price', 10, 2);
function return_custom_price( $price, $product ) {
// If user is not logged in return the default price
if ( ! is_user_logged_in() ) return $price;
$customer = wp_get_current_user();
$count_purchase = wc_product_sold_count_for_user( $product, $customer->ID );
if ( wc_customer_bought_product( $customer->user_email, $customer->ID, $product->get_id() ) ){
if( $count_purchase == 1 )
$price = get_post_meta( $product->get_id(), '_comm_1_kit', true );
elseif( $count_purchase == 2 )
$price = get_post_meta( $product->get_id(), '_comm_2_kit', true );
elseif( $count_purchase >= 3 )
$price = get_post_meta($product->get_id(), '_comm_3_kit', true );
} else {
$price = get_post_meta($product->get_id(), '_discounted_price', true );
}
return $price;
}
此代码位于您的活动子主题(或主题)的function.php文件中。经过测试和工作。