理想情况下,对具有给定标签的所有产品和变体给予20%的折扣,然后正确显示它。
现在我有一个控制实际价格的插件,我正在尝试更改价格的直观显示方式...
我想出了部分解决方案来更改商店和单个产品页面中的显示价格。
有人可以清理吗?它会引发致命错误,但会产生预期的效果。...希望更改商店和单个产品页面上具有给定标签的产品的显示价格。
2019-11-21T22:45:39+00:00 CRITICAL Uncaught Error: Call to a member function is_type() on null in */public_html/wp-content/themes/child/functions.php:325
Stack trace:
#0 */public_html/wp-includes/class-wp-hook.php(288): price_sale_comforter('<span class="wo...')
#1 */public_html/wp-includes/plugin.php(208): WP_Hook->apply_filters('<span class="wo...', Array)
#2 */public_html/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(1750): apply_filters('woocommerce_get...', '<span class="wo...', Object(WC_Product_Variation))
#6 */public_html/wp-content/themes/child/functions.php on line 325
function price_sale_comforter( $price ) {
$product_tag_term = 'comforter'; // CHANGE TO PRODUCT TAG
$original_price = '194.95'; // CHANGE TO ORIGINAL PRICE
$new_price = '155.96'; // CHANGE TO NEW PRICE
$percent_off = '20'; // CHANGE TO DISCOUNT PERCENT AMOUNT
global $product;
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if ( has_term( $product_tag_term , 'product_tag', $product_id ) ) {
if ( $product) {
if (is_shop()) {
echo '<a href="'.get_permalink($product_id).'"class="ivpa_attribute ivpa_html">
<span class="price"><span class="woocommerce-Price-amount amount"><del><span class="woocommerce-Price-currencySymbol">$</span>'.$original_price.'</span></del> <ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>'.$new_price.' '.$percent_off.'% Off!</span></ins></span>
<span class="onsale">Sale!</span>
</a>';
} //closing IF is_shop
} //closing IF $product
if (is_product()) {
$price = '<span class="dynamic-sale-single-product"><del>' . $price .= '</del></span>';
return $price;
} // closing IF is_product
} // closing IF has_term
else {
return $price;
} // closing ELSE
} // closing function
add_filter( 'woocommerce_get_price_html', 'price_sale_comforter' );
答案 0 :(得分:1)
我无法在此处测试代码,但我将从一个简单的
开始 if (is_null($product)) return $price;
此行之后:
global $product;