Php 基于Woocommerce中与用户相关的购买动态更改产品价格
当我添加这个Php 基于Woocommerce中与用户相关的购买动态更改产品价格,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,当我添加这个$product->get_id()时,我得到了HTTP错误500 并在错误日志中收到此消息: [13-Mar-2018 08:47:07 UTC]PHP致命错误:调用成员函数 在中获取null上的\u id() /home/healthnwellness/public_html/wp content/themes/betheme child/functions.php 在线926 我的函数位于functions.php中: //Function return price (comm
$product->get_id()
时,我得到了HTTP错误500
并在错误日志中收到此消息:
[13-Mar-2018 08:47:07 UTC]PHP致命错误:调用成员函数
在中获取null上的\u id()
/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\u product\u salled\u count()
函数出现并已更改为仅返回计数
$product->get_id() = $_SESSION["iddd"];
这是一个错误的任务。get_id()
返回一个int,您可以尝试用另一个值设置它
它也出现在
global$产品之前现在,代码>将为空。代码中缺少一些部分,并且有很多错误
您需要laso稍微更改一下函数wc\u product\u salled\u count()
因此,请尝试以下方法:
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文件中。经过测试,效果良好。我现在就删除了这个,请查看并解决感谢扫描您的退货\u自定义\u价格挂钩?因为$product未传递到此函数,或者为空“添加过滤器('woocommerce\u get\u price'、'return\u custom\u price',10,2);”这是你的电话号码hook@Mukhyyar如果删除global$product会发生什么情况?
我认为这不属于这里,因为prouct是作为参数传递的