我正处于完成一个网站的风口浪尖,并且在过去一周左右的时间里一直在调整它的表现。到目前为止,我已经成功地将我的网页速度分数从E变为实心B.
问题是......它仍然需要永远和一天的加载。更准确地说,它在等待网站回复时会挂起很长时间。
我相信我已经将其缩小为我编写的用于控制会员折扣的自定义php脚本的问题。它覆盖了许多Woocommerce功能,例如按会员级别动态更改产品/购物车价格,以及动态插入产品变体描述和其他产品页面信息。它对我的店铺运营几乎是不可或缺的,所以我真的需要找到一种方法来最小化它对网站性能的干扰。
编辑根据请求,我发布了一大块脚本,包括可能最有问题的功能,并在顶部转换了变量声明。无法包含整件事情,但它并不符合SO的角色限制。我对PHP仍然很陌生,所以它不是最干净的,可能会被简化。
$user_id = get_current_user_id();
$wp_user = wp_get_current_user();
//DECLARE MEMBERSHIP TYPE
$admin = in_array( 'administrator', (array) $wp_user->roles );
$free = in_array( 'free_member', (array) $wp_user->roles );
$bronze = in_array( 'bronze_member', (array) $wp_user->roles );
$silver = in_array( 'silver_member', (array) $wp_user->roles );
$gold = in_array( 'gold_member', (array) $wp_user->roles );
$platinum = in_array( 'platinum_member', (array) $wp_user->roles );
$doubleplatinum = in_array( 'double_platinum_member', (array) $wp_user->roles );
$diamond = in_array( 'diamond_member', (array) $wp_user->roles );
$freesync = in_array( 'free_sync_member', (array) $wp_user->roles );
$silversync = in_array( 'silver_sync_member', (array) $wp_user->roles );
$goldsync = in_array( 'gold_sync_member', (array) $wp_user->roles );
$nonpaid ='$free';
//SET VARIATION PRICES FOR EACH MEMBERSHIP TYPE
$pricedemo = 0;
$pricedemofree = .99;
$pricedemomp3bronze = $pricedemofree;
$pricedemomp3silver = 0;
$pricedemomp3gold = 0;
$pricedemomp3platinum = 0;
$pricedemomp3dblplatinum = 0;
$pricedemomp3diamond = 0;
$pricebasicmp3free =25;
$pricebasicmp3bronze =15;
$pricebasicmp3silver =15;
$pricebasicmp3gold =10;
$pricebasicmp3platinum = 10;
$pricebasicmp3dblplatinum =5;
$pricebasicmp3diamond = 1;
$pricebasicwavfree=35;
$pricebasicwavbronze =25;
$pricebasicwavsilver =25;
$pricebasicwavgold =10;
$pricebasicwavplatinum =10;
$pricebasicwavdblplatinum =5;
$pricebasicwavdiamond =1;
$pricepremiummp3free =85;
$pricepremiummp3bronze =70;
$pricepremiummp3silver =50;
$pricepremiummp3gold =40;
$pricepremiummp3platinum =30;
$pricepremiummp3dblplatinum =20;
$pricepremiummp3diamond = 10;
$pricepremiumwavfree =100;
$pricepremiumwavbronze =85;
$pricepremiumwavsilver =70;
$pricepremiumwavgold =40;
$pricepremiumwavplatinum =30;
$pricepremiumwavdblplatinum =20;
$pricepremiumwavdiamond =10;
$pricedemofreesync = .99;
$pricedemosilversync =0;
$pricedemogoldsync =0;
$pricesyncmp3freesync = 15;
$pricesyncmp3silversync =5;
$pricesyncmp3goldsync =1;
$pricesyncwavfreesync = 25;
$pricesyncwavsilversync =10;
$pricesyncwavgoldsync =5;
$demomp3 = '';
$basicmp3 = '';
$basicwav = '';
$premiummp3 = '';
$premiumwav = '';
$syncmp3 = '';
$syncwav = '';
$demomp3cart = '';
$basicmp3cart = '';
$basicwavcart = '';
$premiummp3cart = '';
$premiumwavcart = '';
$syncmp3cart = '';
$syncwavcart = '';
$getcat ='';
$category = '';
$categories = array();
$showmessage= false;
//OVERRIDE CART/CHECKOUT PRICE CALCULATIONS
add_filter( 'woocommerce_before_calculate_totals', 'change_cart_price' );
function change_cart_price( $cart_items) {
global $post, $woocommerce, $demomp3, $basicmp3, $basicwav, $premiummp3, $premiumwav, $syncmp3, $syncwav, $demomp3cart, $basicmp3cart, $basicwavcart, $premiummp3cart, $premiumwavcart, $syncmp3cart, $syncwavcart, $admin ,$free, $nonpaid ,$bronze, $silver, $gold, $platinum, $doubleplatinum, $diamond, $freesync, $silversync, $goldsync, $user_id, $wp_user, $pricebasicmp3bronze, $pricebasicmp3silver, $pricebasicmp3gold, $pricebasicmp3platinum, $pricebasicmp3dblplatinum, $pricebasicmp3diamond, $pricebasicwavbronze, $pricebasicwavsilver, $pricebasicwavgold, $pricebasicwavplatinum, $pricebasicwavdblplatinum, $pricebasicwavdiamond, $pricepremiummp3bronze, $pricepremiummp3silver, $pricepremiummp3gold, $pricepremiummp3platinum, $pricepremiummp3dblplatinum, $pricepremiummp3diamond, $pricepremiumwavbronze, $pricepremiumwavsilver, $pricepremiumwavgold, $pricepremiumwavplatinum, $pricepremiumwavdblplatinum, $pricepremiumwavdiamond, $freedemo, $pricedemomp3bronze, $pricedemomp3silver, $pricedemomp3gold, $pricedemomp3platinum, $pricedemomp3dblplatinum, $pricedemomp3diamond, $pricesyncmp3freesync, $pricesyncmp3silversync, $pricesyncmp3goldsync, $pricesyncwavfreesync, $pricesyncwavsilversync, $pricesyncwavgoldsync, $pricedemofreesync, $pricedemosilversync, $pricedemogoldsync, $categories, $category, $getcat;
// Loop through cart items
foreach ($cart_items->cart_contents as $key => $item) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_qty = $item['qty'];
$product_variation_id = $item['variation_id'];
$price = $item['data']->price;
//find attributes
$option1 = 'pa_license-options';
$option2 = 'pa_delivery-format';
$getmeta1 = get_post_meta($product_variation_id , 'attribute_'.$option1, true);
$getmeta2 = get_post_meta($product_variation_id , 'attribute_'.$option2, true);
$attribute1 = get_term_by('slug', $getmeta1, $option1);
$attribute2 = get_term_by('slug', $getmeta2, $option2);
$license = $attribute1->name;
$format = $attribute2->name;
if ($format === "mp3" && $license === "Demo License" ){
$democart = $item['data']->price;
if ( $free || $freesync ) {
$item['data']->price = $pricedemofree;
}
else if ( $bronze || $silver || $gold || $platinum || $doubleplatinum || $diamond || $silversync || $goldsync ) {
$item['data']->price = $freedemo;
}
}
if ($format === "mp3" && $license === "Basic License" ){
$basicmp3cart = $item['data']->price;
if ( $free ) {
$item['data']->price = $pricebasicmp3free;
}
if ( $bronze) {
$item['data']->price = $pricebasicmp3bronze;
}
else if ( $silver ) {
$item['data']->price = $pricebasicmp3silver;
}
else if ( $gold ) {
$item['data']->price = $pricebasicmp3gold;
}
else if ( $platinum ) {
$item['data']->price = $pricebasicmp3platinum;
}
else if ( $doubleplatinum ) {
$item['data']->price = $pricebasicmp3dblplatinum;
}
else if ( $diamond ) {
$item['data']->price = $pricebasicmp3diamond;
}
else if ( $silversync ) {
$item['data']->price = '';
}
else if ( $goldsync ) {
$item['data']->price = '';
}
}
if ($format === "WAV" && $license === "Basic License" ){
$basicwavcart = $item['data']->price;
if ( $free ) {
$item['data']->price = $pricebasicwavfree;
}
else if ( $bronze) {
$item['data']->price = $pricebasicwavbronze;
}
else if ( $silver ) {
$item['data']->price = $pricebasicwavsilver;
}
else if ( $gold ) {
$item['data']->price = $pricebasicwavgold;
}
else if ( $platinum ) {
$item['data']->price = $pricebasicwavplatinum;
}
else if ( $doubleplatinum ) {
$item['data']->price = $pricebasicwavdblplatinum;
}
else if ( $diamond ) {
$item['data']->price = $pricebasicwavdiamond;
}
else if ( $silversync ) {
$item['data']->price = '';
}
else if ( $goldsync ) {
$item['data']->price = '';
}
}
if ($format === "mp3" && $license === "Premium License" ){
$premiummp3cart = $item['data']->price;
if ( $free ) {
$item['data']->price = $pricepremiummp3free;
}
else if ( $bronze) {
$item['data']->price = $pricepremiummp3bronze;
}
else if ( $silver ) {
$item['data']->price = $pricepremiummp3silver;
}
else if ( $gold ) {
$item['data']->price = $pricepremiummp3gold;
}
else if ( $platinum ) {
$item['data']->price = $pricepremiummp3platinum;
}
else if ( $doubleplatinum ) {
$item['data']->price = $pricepremiummp3dblplatinum;
}
else if ( $diamond ) {
$item['data']->price = $pricepremiummp3diamond;
}
else if ( $silversync ) {
$item['data']->price = '';
}
else if ( $goldsync ) {
$item['data']->price = '';
}
}
if ($format === "WAV" && $license === "Premium License" ){
$premiumwavcart = $item['data']->price;
if ( $free ) {
$item['data']->price = $pricepremiumwavfree;
}
else if ( $bronze) {
$item['data']->price = $pricepremiumwavbronze;
}
else if ( $silver ) {
$item['data']->price = $pricepremiumwavsilver;
}
else if ( $gold ) {
$item['data']->price = $pricepremiumwavgold;
}
else if ( $platinum ) {
$item['data']->price = $pricepremiumwavplatinum;
}
else if ( $doubleplatinum ) {
$item['data']->price = $pricepremiumwavdblplatinum;
}
else if ( $diamond ) {
$item['data']->price = $pricepremiumwavdiamond;
}
else if ( $silversync ) {
$item['data']->price = '';
}
else if ( $goldsync ) {
$item['data']->price = '';
}
}
if ($format === "mp3" && $license === "Sync License" ){
$syncmp3cart = $item['data']->price;
if ( $free ) {
$item['data']->price = '';
}
if ( $bronze) {
$item['data']->price = '';
}
else if ( $silver ) {
$item['data']->price = '';
}
else if ( $gold ) {
$item['data']->price = '';
}
else if ( $platinum ) {
$item['data']->price = '';
}
else if ( $doubleplatinum ) {
$item['data']->price = '';
}
else if ( $diamond ) {
$item['data']->price = '';
}
else if ( $silversync ) {
$item['data']->price = $pricesyncmp3silversync;
}
else if ( $goldsync ) {
$item['data']->price = $pricesyncmp3goldsyncsync;
}
}
if ($format === "WAV" && $license === "Sync License" ){
$syncwavcart = $item['data']->price;
if ( $free ) {
$item['data']->price = '';
}
if ( $bronze) {
$item['data']->price = '';
}
else if ( $silver ) {
$item['data']->price = '';
}
else if ( $gold ) {
$item['data']->price = '';
}
else if ( $platinum ) {
$item['data']->price = '';
}
else if ( $doubleplatinum ) {
$item['data']->price = '';
}
else if ( $diamond ) {
$item['data']->price = '';
}
else if ( $silversync ) {
$item['data']->price = $pricesyncwavsilversync;
}
else if ( $goldsync ) {
$item['data']->price = $pricesyncwavgoldsyncsync;
}
}
}
}
//OVERRIDE PRODUCT PRICES
add_filter('woocommerce_get_price','change_price', 10, 2);
function change_price($price, $productd){
global $post, $woocommerce, $demomp3, $basicmp3, $basicwav, $premiummp3, $premiumwav, $syncmp3, $syncwav, $demomp3cart, $basicmp3cart, $basicwavcart, $premiummp3cart, $premiumwavcart, $syncmp3cart, $syncwavcart, $admin ,$free, $free ,$bronze, $silver, $gold, $platinum, $doubleplatinum, $diamond, $freesync, $silversync, $goldsync, $user_id, $wp_user, $pricebasicmp3bronze, $pricebasicmp3silver, $pricebasicmp3gold, $pricebasicmp3platinum, $pricebasicmp3dblplatinum, $pricebasicmp3diamond, $pricebasicwavbronze, $pricebasicwavsilver, $pricebasicwavgold, $pricebasicwavplatinum, $pricebasicwavdblplatinum, $pricebasicwavdiamond, $pricepremiummp3bronze, $pricepremiummp3silver, $pricepremiummp3gold, $pricepremiummp3platinum, $pricepremiummp3dblplatinum, $pricepremiummp3diamond, $pricepremiumwavbronze, $pricepremiumwavsilver, $pricepremiumwavgold, $pricepremiumwavplatinum, $pricepremiumwavdblplatinum, $pricepremiumwavdiamond, $freedemo, $pricedemomp3bronze, $pricedemomp3silver, $pricedemomp3gold, $pricedemomp3platinum, $pricedemomp3dblplatinum, $pricedemomp3diamond, $pricesyncmp3freesync, $pricesyncmp3silversync, $pricesyncmp3goldsync, $pricesyncwavfreesync, $pricesyncwavsilversync, $pricesyncwavgoldsync, $pricedemofreesync, $pricedemosilversync, $pricedemogoldsync, $categories, $category, $getcat;
$post_id = $variation->ID;
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
//declare variables for later use
foreach ( $variations as $variation ) {
$variation_id = absint( $variation->ID );
$variable_id = $this['variation_id'];
$variation_post_status = esc_attr( $variation->post_status );
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
//find attributes
$option1 = 'pa_license-options';
$option2 = 'pa_delivery-format';
$getmeta1 = get_post_meta($variation_id , 'attribute_'.$option1, true);
$getmeta2 = get_post_meta($variation_id , 'attribute_'.$option2, true);
$attribute1 = get_term_by('slug', $getmeta1, $option1);
$attribute2 = get_term_by('slug', $getmeta2, $option2);
$license = $attribute1->name;
$format = $attribute2->name;
//get the prices of each variation by the attribute combinations they have
if ($format === "mp3" && $license === "Demo License" ){
//retrieve variation price and assign it to the previously created variable
$demomp3 = get_post_meta($variation_id, '_regular_price', true );
}
if ($format === "mp3" && $license === "Basic License" ){
$basicmp3 = get_post_meta($variation_id, '_regular_price', true );
}
if ($format === "WAV" && $license === "Basic License" ){
$basicwav = get_post_meta($variation_id, '_regular_price', true );
}
if ($format === "mp3" && $license === "Premium License" ){
$premiummp3 = get_post_meta($variation_id, '_regular_price', true );
}
if ($format === "WAV" && $license === "Premium License" ){
$premiumwav = get_post_meta($variation_id, '_regular_price', true );
}
if ($format === "mp3" && $license === "Sync License" ){
$syncmp3 = get_post_meta($variation_id, '_regular_price', true );
}
if ($format === "WAV" && $license === "Sync License" ){
$syncwav = get_post_meta($variation_id, '_regular_price', true );
}
}
$getcat = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $getcat as $category ) $categories[] = $category->slug;
//Simple product doesn't need to be modified
if($productd->product_type == 'simple'){
return $price;
}
//adjust prices of variable products
if ( $productd->product_type == 'variation' || $productd->product_type == 'variable' ) {
if ( in_array( 'licensing', $categories ) ){
$regprice = get_post_meta( $productd->variation_id, '_regular_price',true);
//target the current variation by matching its price to the one stored in the variable
if ($regprice == $demomp3) {
//apply price adjustment and return the new value
if ( $free || $freesync || $bronze ) {
$price = $price;
}
else if ( $silver || $gold || $platinum || $doubleplatinum || $diamond || $silversync || $goldsync ) {
$freedemo = $price - $price;
$price = $freedemo;
}
return $price;
}
else if ($regprice == $basicmp3) {
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricebasicmp3bronze;
}
else if ( $silver ) {
$price = $pricebasicmp3silver;
}
else if ( $gold ) {
$price = $pricebasicmp3gold;
}
else if ( $platinum ) {
$price = $pricebasicmp3platinum;
}
else if ( $doubleplatinum ) {
$price = $pricebasicmp3dblplatinum;
}
else if ( $diamond ) {
$price = $pricebasicmp3diamond;
}
return $price;
}
else if ($regprice == $basicwav) {
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricebasicwavbronze;
}
else if ( $silver ) {
$price = $pricebasicwavsilver;
}
else if ( $gold ) {
$price = $pricebasicwavgold;
}
else if ( $platinum ) {
$price = $pricebasicwavplatinum;
}
else if ( $doubleplatinum ) {
$price = $pricebasicwavdblplatinum;
}
else if ( $diamond ) {
$price = $pricebasicwavdiamond;
}
return $price;
}
else if ($regprice == $premiummp3) {
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricepremiummp3bronze;
}
else if ( $silver ) {
$price = $pricepremiummp3silver;
}
else if ( $gold ) {
$price = $pricepremiummp3gold;
}
else if ( $platinum ) {
$price = $pricepremiummp3platinum;
}
else if ( $doubleplatinum ) {
$price = $pricepremiummp3dblplatinum;
}
else if ( $diamond ) {
$price = $pricepremiummp3diamond;
}
return $price;
}
else if ($regprice == $premiumwav) {
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricepremiumwavbronze;
}
else if ( $silver ) {
$price = $pricepremiumwavsilver;
}
else if ( $gold ) {
$price = $pricepremiumwavgold;
}
else if ( $platinum ) {
$price = $pricepremiumwavplatinum;
}
else if ( $doubleplatinum ) {
$price = $pricepremiumwavdblplatinum;
}
else if ( $diamond ) {
$price = $pricepremiumwavdiamond;
}
return $price;
}
else if ($regprice == $syncmp3) {
if ( $freesync ) {
$price = $price;
}
else if ( $silversync ) {
$price = $pricesyncmp3sliver;
}
else if ( $goldsync ) {
$price = $pricesyncmp3gold;
}
else if ( $free ) {
$price = '';
}
else if ( $bronze) {
$price = '';
}
else if ( $silver ) {
$price = '';
}
else if ( $gold ) {
$price = '';
}
else if ( $platinum ) {
$price = '';
}
else if ( $doubleplatinum ) {
$price = '';
}
else if ( $diamond ) {
$price = '';
}
return $price;
}
else if ($regprice == $syncwav) {
if ( $freesync ) {
$price = $price;
}
else if ( $silversync ) {
$price = $pricesyncwavsliversync;
}
else if ( $goldsync ) {
$price = $pricesyncwavgoldsync;
}
else if ( $free ) {
$price = '';
}
else if ( $bronze) {
$price = '';
}
else if ( $silver ) {
$price = '';
}
else if ( $gold ) {
$price = '';
}
else if ( $platinum ) {
$price = '';
}
else if ( $doubleplatinum ) {
$price = '';
}
else if ( $diamond ) {
$price = '';
}
return $price;
}
else{
return $price;
}
}
else{
return $price;
}
}
}
//OVERRIDE PRODUCT PRICE HTML DISPLAY
add_filter('woocommerce_get_price_html', 'change_display_price', 10, 2);
function change_display_price( $price, $product ) {
global $demomp3, $basicmp3, $basicwav, $premiummp3, $premiumwav, $syncmp3, $syncwav, $admin ,$free, $free ,$bronze, $silver, $gold, $platinum, $doubleplatinum, $diamond, $freesync, $silversync, $goldsync, $user_id, $wp_user, $pricebasicmp3bronze, $pricebasicmp3silver, $pricebasicmp3gold, $pricebasicmp3platinum, $pricebasicmp3dblplatinum, $pricebasicmp3diamond, $pricebasicwavbronze, $pricebasicwavsilver, $pricebasicwavgold, $pricebasicwavplatinum, $pricebasicwavdblplatinum, $pricebasicwavdiamond, $pricepremiummp3bronze, $pricepremiummp3silver, $pricepremiummp3gold, $pricepremiummp3platinum, $pricepremiummp3dblplatinum, $pricepremiummp3diamond, $pricepremiumwavbronze, $pricepremiumwavsilver, $pricepremiumwavgold, $pricepremiumwavplatinum, $pricepremiumwavdblplatinum, $pricepremiumwavdiamond, $freedemo, $pricedemomp3bronze, $pricedemomp3silver, $pricedemomp3gold, $pricedemomp3platinum, $pricedemomp3dblplatinum, $pricedemomp3diamond, $pricesyncmp3freesync, $pricesyncmp3silversync, $pricesyncmp3goldsync, $pricesyncwavfreesync, $pricesyncwavsilversync, $pricesyncwavgoldsync, $pricedemofreesync, $pricedemosilversync, $pricedemogoldsync, $categories, $category, $getcat, $showmessage;
$originalprice = '<del style="display: block; line-height: 100%;"><span class="woocommerce-Price-amount amount"> <span class="woocommerce-Price-currencySymbol">$</span>' . $basicmp3 . '</span>-<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>' . $premiumwav . '</span></del>';
$originalpricesync = '<del style="display: block; line-height: 100%;"><span class="woocommerce-Price-amount amount"> <span class="woocommerce-Price-currencySymbol">$</span>' . $syncmp3 . '</span>-<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>' . $syncwav . '</span></del>';
if ( $bronze) {
$yourprice = '<span class="yourprice bronzetext">Bronze Price: </span>';
}
else if ( $silver ) {
$yourprice = '<span class="yourprice silvertext">Silver Price: </span>';
}
else if ( $gold ) {
$yourprice = '<span class="yourprice goldtext">Gold Price: </span>';
}
else if ( $platinum ) {
$yourprice = '<span class="yourprice platinumtext">Platinum Price: </span>';
}
else if ( $doubleplatinum ) {
$yourprice = '<span class="yourprice dblplatinumtext">Double Platinum Price: </span>';
}
else if ( $diamond ) {
$yourprice = '<span class="yourprice diamondtext">Diamond Price: </span>';
}
else if ( $silversync ) {
$yourprice = '<span class="yourprice diamondtext">Silver Price: </span>';
}
else if ( $goldsync ) {
$yourprice = '<span class="yourprice diamondtext">Gold Price: </span>';
}
$memberpriceopen = $yourprice . '<ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>';
$memberpricemid = '</span>-<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>';
$memberpriceclose = '</span></ins>';
if($product->product_type == 'simple'){
return $price;
}
//adjust prices of variable products
if ( $product->product_type == 'variation' || $product->product_type == 'variable' ) {
if ( in_array( 'licensing', $categories ) ){
if ( $free ) {
return $price;
}
else if ( $bronze ) {
$pricelow = $pricebasicmp3bronze;
$pricehigh = $pricepremiumwavbronze;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $silver ) {
$pricelow = $pricebasicmp3silver;
$pricehigh = $pricepremiumwavsilver;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $gold ) {
$pricelow = $pricebasicmp3gold;
$pricehigh = $pricepremiumwavgold;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $platinum ) {
$pricelow = $pricebasicmp3platinum;
$pricehigh = $pricepremiumwavplatinum;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $doubleplatinum ) {
$pricelow = $pricebasicmp3dblplatinum;
$pricehigh = $pricepremiumwavdblplatinum;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $diamond ) {
$pricelow = $pricebasicmp3diamond;
$pricehigh = $pricepremiumwavdiamond;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $freesync ) {
$pricelow = $pricesyncmp3silversync;
$pricehigh = $pricesyncwavsilversync;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $silversync ) {
$pricelow = $pricesyncmp3silversync;
$pricehigh = $pricesyncwavsilversync;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else if ( $goldsync ) {
$pricelow = $pricesyncmp3goldsync;
$pricehigh = $pricesyncwavgoldsync;
$price = $originalprice . $memberpriceopen . $pricelow . $memberpricemid . $pricehigh . $memberpriceclose;
return $price;
}
else{
return $price;
}
}
else{
return $price;
}
}
}
我无法复制实时结果,因为他们需要在我的网站上拥有会员资格。所以我拍了一个截图来说明上面的代码产生了什么。
答案 0 :(得分:0)
似乎我已经找到了解决方案。我简化了控制产品价格的功能,事情已经大大加快了
//OVERRIDE PRODUCT PRICES
add_filter('woocommerce_get_price','change_price', 10, 2);
function change_price($price, $product){
global $post, $woocommerce, $demomp3, $basicmp3, $basicwav, $premiummp3, $premiumwav, $syncmp3, $syncwav, $demomp3cart, $basicmp3cart, $basicwavcart, $premiummp3cart, $premiumwavcart, $syncmp3cart, $syncwavcart, $admin ,$free, $free ,$bronze, $silver, $gold, $platinum, $doubleplatinum, $diamond, $freesync, $silversync, $goldsync, $user_id, $wp_user, $pricebasicmp3bronze, $pricebasicmp3silver, $pricebasicmp3gold, $pricebasicmp3platinum, $pricebasicmp3dblplatinum, $pricebasicmp3diamond, $pricebasicwavbronze, $pricebasicwavsilver, $pricebasicwavgold, $pricebasicwavplatinum, $pricebasicwavdblplatinum, $pricebasicwavdiamond, $pricepremiummp3bronze, $pricepremiummp3silver, $pricepremiummp3gold, $pricepremiummp3platinum, $pricepremiummp3dblplatinum, $pricepremiummp3diamond, $pricepremiumwavbronze, $pricepremiumwavsilver, $pricepremiumwavgold, $pricepremiumwavplatinum, $pricepremiumwavdblplatinum, $pricepremiumwavdiamond, $freedemo, $pricedemomp3bronze, $pricedemomp3silver, $pricedemomp3gold, $pricedemomp3platinum, $pricedemomp3dblplatinum, $pricedemomp3diamond, $pricesyncmp3freesync, $pricesyncmp3silversync, $pricesyncmp3goldsync, $pricesyncwavfreesync, $pricesyncwavsilversync, $pricesyncwavgoldsync, $pricedemofreesync, $pricedemosilversync, $pricedemogoldsync;
//find attributes
$option1 = 'pa_license-options';
$option2 = 'pa_delivery-format';
$getmeta1 = get_post_meta($product->variation_id , 'attribute_'.$option1, true);
$getmeta2 = get_post_meta($product->variation_id , 'attribute_'.$option2, true);
$attribute1 = get_term_by('slug', $getmeta1, $option1);
$attribute2 = get_term_by('slug', $getmeta2, $option2);
$license = $attribute1->name;
$format = $attribute2->name;
//Simple product doesn't need to be modified
if($product->product_type == 'simple'){
return $price;
}
//adjust prices of variable products
if ( $product->product_type == 'variation' || $product->product_type == 'variable' ) {
//target the current variation by matching its price to the one stored in the variable
if ($format === "mp3" && $license === "Demo License" ){
//apply price adjustment and return the new value
if ( $free || $freesync || $bronze ) {
$price = $price;
}
else if ( $silver || $gold || $platinum || $doubleplatinum || $diamond || $silversync || $goldsync ) {
$freedemo = $price - $price;
$price = $freedemo;
}
return $price;
}
else if ($format === "mp3" && $license === "Basic License" ){
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricebasicmp3bronze;
}
else if ( $silver ) {
$price = $pricebasicmp3silver;
}
else if ( $gold ) {
$price = $pricebasicmp3gold;
}
else if ( $platinum ) {
$price = $pricebasicmp3platinum;
}
else if ( $doubleplatinum ) {
$price = $pricebasicmp3dblplatinum;
}
else if ( $diamond ) {
$price = $pricebasicmp3diamond;
}
return $price;
}
else if ($format === "WAV" && $license === "Basic License" ){
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricebasicwavbronze;
}
else if ( $silver ) {
$price = $pricebasicwavsilver;
}
else if ( $gold ) {
$price = $pricebasicwavgold;
}
else if ( $platinum ) {
$price = $pricebasicwavplatinum;
}
else if ( $doubleplatinum ) {
$price = $pricebasicwavdblplatinum;
}
else if ( $diamond ) {
$price = $pricebasicwavdiamond;
}
return $price;
}
else if ($format === "mp3" && $license === "Premium License" ){
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricepremiummp3bronze;
}
else if ( $silver ) {
$price = $pricepremiummp3silver;
}
else if ( $gold ) {
$price = $pricepremiummp3gold;
}
else if ( $platinum ) {
$price = $pricepremiummp3platinum;
}
else if ( $doubleplatinum ) {
$price = $pricepremiummp3dblplatinum;
}
else if ( $diamond ) {
$price = $pricepremiummp3diamond;
}
return $price;
}
else if ($format === "WAV" && $license === "Premium License" ){
if ( $free ) {
$price = $price;
}
else if ( $bronze ) {
$price = $pricepremiumwavbronze;
}
else if ( $silver ) {
$price = $pricepremiumwavsilver;
}
else if ( $gold ) {
$price = $pricepremiumwavgold;
}
else if ( $platinum ) {
$price = $pricepremiumwavplatinum;
}
else if ( $doubleplatinum ) {
$price = $pricepremiumwavdblplatinum;
}
else if ( $diamond ) {
$price = $pricepremiumwavdiamond;
}
return $price;
}
else if ($format === "mp3" && $license === "Sync License" ){
if ( $freesync ) {
$price = $price;
}
else if ( $silversync ) {
$price = $pricesyncmp3sliver;
}
else if ( $goldsync ) {
$price = $pricesyncmp3gold;
}
else if ( $free ) {
$price = '';
}
else if ( $bronze) {
$price = '';
}
else if ( $silver ) {
$price = '';
}
else if ( $gold ) {
$price = '';
}
else if ( $platinum ) {
$price = '';
}
else if ( $doubleplatinum ) {
$price = '';
}
else if ( $diamond ) {
$price = '';
}
return $price;
}
else if ($format === "WAV" && $license === "Sync License" ){
if ( $freesync ) {
$price = $price;
}
else if ( $silversync ) {
$price = $pricesyncwavsliversync;
}
else if ( $goldsync ) {
$price = $pricesyncwavgoldsync;
}
else if ( $free ) {
$price = '';
}
else if ( $bronze) {
$price = '';
}
else if ( $silver ) {
$price = '';
}
else if ( $gold ) {
$price = '';
}
else if ( $platinum ) {
$price = '';
}
else if ( $doubleplatinum ) {
$price = '';
}
else if ( $diamond ) {
$price = '';
}
return $price;
}
else{
return $price;
}
}
}