GVKun编程网logo

php – 如果没有促销价格,如何显示woocommerce销售价格或正常价格

15

在这篇文章中,我们将带领您了解php–如果没有促销价格,如何显示woocommerce销售价格或正常价格的全貌,同时,我们还将为您介绍有关php–WooCommerce–显示价格包含产品页面的税、ph

在这篇文章中,我们将带领您了解php – 如果没有促销价格,如何显示woocommerce销售价格或正常价格的全貌,同时,我们还将为您介绍有关php – WooCommerce – 显示价格包含产品页面的税、php – Woocommerce从外部来源更新价格、php – WooCommerce以定制价格添加到购物车产品、php – WooCommerce价格压倒不起作用的知识,以帮助您更好地理解这个主题。

本文目录一览:

php – 如果没有促销价格,如何显示woocommerce销售价格或正常价格

php – 如果没有促销价格,如何显示woocommerce销售价格或正常价格

我正在使用一个woocommerce产品插件插件,我想在插件的下拉部分显示产品的价格.
目前我的代码就是这个
<?PHP
$loop = 0;
$current_value = isset( $_POST['addon-' . sanitize_title( $addon['field-name'] ) ] ) ? wc_clean( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) : '';
global $product;
?>
<phttps://www.jb51.cc/tag/PHP/" target="_blank">PHP echo sanitize_title( $addon['field-name'] ); ?>">
    <selectname="addon-<?PHP echo sanitize_title( $addon['field-name'] ); ?>">

        <?PHP if ( ! isset( $addon['required'] ) ) : ?>
            <option value=""><?PHP _e('None','woocommerce-product-addons'); ?></option>
        <?PHP else : ?>
            <!--<option value=""><?PHP _e('Select an option...','woocommerce-product-addons'); ?></option>-->
        <?PHP endif; ?>

        <?PHP foreach ( $addon['options'] as $i => $option ) :
            $loop ++;
            $price = apply_filters( 'woocommerce_product_addons_option_price',$option['price'] > 0 ? ' + ' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . '' : '',$option,$i,'select'
            );
            ?>
            <option data-raw-price="<?PHP echo esc_attr( $option['price'] ); ?>" data-price="<?PHP echo get_product_addon_price_for_display( $option['price'] ); ?>" value="<?PHP echo sanitize_title( $option['label'] ) . '-' . $loop; ?>" <?PHP selected( $current_value,sanitize_title( $option['label'] ) . '-' . $loop ); ?>><?PHP echo wptexturize( $option['label'] . ' (' ); echo balanceTags($product->get_price_html()) . $price ?>)</option>
        <?PHP endforeach; ?>

    </select>
</p>

我正在使用这个回声

$product->get_price_html()

这样做虽然是显示$“促销价”$“价格”但我只是想显示销售价格或只是产品价格,如果没有销售价格.看看下面的代码,我将如何实现这一目标?

很简单.我们将编写一个自定义功能,如果产品是否在售,将首先确定.然后它将根据之前定义的销售条件返回常规或销售价格.所以功能将是 –
/**
 * Returns product price based on sales.
 * 
 * @return string
 */
function the_dramatist_price_show() {
    global $product;
    if( $product->is_on_sale() ) {
        return $product->get_sale_price();
    }
    return $product->get_regular_price();
}

现在调用此函数the_dramatist_price_show()而不是$product-> get_price_html().如果没有货币符号,您将获得基于销售的价格.

希望有所帮助.

php – WooCommerce – 显示价格包含产品页面的税

php – WooCommerce – 显示价格包含产品页面的税

我正在使用WooCommerce for wordpress.

我列出的项目不包括增值税.

我需要在产品页面上单独显示价格,增值税和增值税(如结帐页面).

我找不到这样做的插件.

我怎样才能做到这一点?

WooCommerce v3.0.0及更高版本
从WooCommerce 3.0版开始,不推荐使用函数 woocommerce_price(),方法 get_price_including_tax()也是如此.您应该使用 wc_get_price_including_tax:
<?PHP echo wc_price( wc_get_price_including_tax( $product ) ); ?>

在WooCommerce v3.0.0之前
您需要修改模板.不要修改核心WooCommerce模板,而是使用WooCommerce模板覆盖系统将其复制到您的主题.有关这方面的帮助,请参阅使用template override system的WooCommerce文档.

在price.PHP模板中,您将在需要价格的地方添加这段代码,包括税(VAT):

<?PHP echo woocommerce_price( $product->get_price_including_tax() ); ?>

注意:您修改的price.PHP模板应位于wp-content / themes / [您的主题文件夹] /woocommerce/single-product/price.PHP中

php – Woocommerce从外部来源更新价格

php – Woocommerce从外部来源更新价格

我有从外部数据库更新产品价格的问题.我需要在每次访问此位置时检查产品的价格.为此,我使用the_post钩子.例如,单品的价格为’1718′.

function chd_the_post_action( $post ) {
    if ( $post && $post->post_type == 'product' ) {
        $product = wc_get_product( $post->ID );  
        if ( $product ) {
            $price = '1718';
            $product->set_price( "$price" );    
            $product->set_regular_price( "$price" );    
            $product->set_sale_price( "$price" );    
            $product->save();
        }
    }
}

此代码更新了数据库中的产品价格,但它不会在同一时刻更改页面上的价格视图,而是仅在页面重新加载后,因为post和product变量是由setup_postdata()设置的.
因此我使用woocommerce钩子显示更新价格:

function chd_get_price_filter( $price, $item ) {
    return '1718';
}
add_filter( 'woocommerce_product_get_price', 'chd_get_price_filter', 100, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'chd_get_price_filter', 100, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'chd_get_price_filter', 100, 2 );

有没有可以用更好的方式做这个动作的钩子?

解决方法:

使用update_post_Meta函数更新产品价格

update_post_Meta( $product->id, '_sale_price', '1718' );
update_post_Meta( $product->id, '_regular_price', '1718 );

php – WooCommerce以定制价格添加到购物车产品

php – WooCommerce以定制价格添加到购物车产品

我在WooCommerce wordpress中使用以下代码通过模板添加了使用add_to_cart($product_id)函数的产品数量.

global $woocommerce;
$id_arr = $_POST['up_product_chk'];
$i = 0;
for($i; $i<=count($id_arr); $i++){
   $ids = $id_arr[$i];
   $woocommerce->cart->add_to_cart($ids);  
}
wp_redirect(site_url().'/cart/');

现在,我希望通过此模板为每个产品添加自定义价格.由于现在购物车中的价格与数据库中的相同,但我想通过这一点添加我的自定义价格.有人可以帮我做同样的事.谢谢

解决方法:

使用此挂钩,您可以设置自定义价格.在functions.PHP文件中编写此代码.

add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);


function change_price($price, $productd){
     if($productd->id == '1'){
        $price = "150";
     }
     return $price;
}

这样,您可以在不影响数据库的情况下在woocommerce中设置自定义价格.

更详细的请read this article.

我希望这对你有用.

php – WooCommerce价格压倒不起作用

php – WooCommerce价格压倒不起作用

我使用woocommerce_before_add_to_cart_button hook设置了一个隐藏的输入项

function add_gift_wrap_field() {
    ?>`<input type="hidden" id="price_val" name="added_price" value="100.34">`<?PHP
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );

针对产品保存字段:

function save_gift_wrap_fee( $cart_item_data, $product_id ) {

    if( isset( $_POST['added_price'] ) ) {

        $cart_item_data = array();

        $cart_item_data[ "gift_wrap_fee" ] = "YES"; 
        $cart_item_data[ "gift_wrap_price" ] = 100;     
    }
    return $cart_item_data;

}
add_filter( 'woocommerce_add_cart_item_data', 'save_gift_wrap_fee', 99, 2 );

现在我可以在这个woocommerce_before_calculate_totals钩子中回显$_POST [‘added_price’].

我写的代码如下所示:

function calculate_gift_wrap_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = number_format($_POST['added_price'], 2); 

        $additionalPrice = floatval($additionalPrice);
        //echo $additionalPrice; exit(); shows the value
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["gift_wrap_fee"] ) ) {
                    /* Woocommerce 3.0 + */
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $additionalPrice );    //not adding the $additionalPrice here.     
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99 );

我在这做错了什么?

解决方法:

以下是基于您的代码问题的完整清洁,经过测试和运行的解决方案.

在这里,我将自定义字段键/值包含在相关购物车项目的购物车对象中,而不是在calculate_gift_wrap_fee()函数中获取Post值.

这样就不可能松开自定义字段值,并且新的价格计算是可靠的.

我已经评论过’gift_wrap_fee’和’gift_wrap_price’,因为它们现在并不是真的需要(但如果你愿意,你可以取消注释它们).

这是这段代码:

// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
    ?>
        <input type="hidden" id="price_val" name="added_price" value="100.34">
    <?PHP
}

// Adding the custom field to as custom data for this cart item in the cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
    $bool = false;
    $data = array();
    if( ! empty( $_REQUEST['added_price'] ) ) {
        $cart_item_data['custom_data']['added_price'] = $_REQUEST['added_price'];
        // Below this 2 values are not really needed (I think)
        // (You can uncomment them if needed)

        ## $cart_item_data['custom_data']['gift_wrap_fee'] = 'YES';
        ## $cart_item_data['custom_data']['gift_wrap_price'] = 100;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $data );
    }
    return $cart_item_data;
}

// Changing the cart item price based on custom field calculation
add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 10, 1 );
function calculate_gift_wrap_fee( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Iterating though cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Continue if we get the custom data for the current cart item
        if( ! empty( $cart_item['custom_data'] ) ){
            // Get the custom field "added price" value
            $added_price = number_format( $cart_item['custom_data']['added_price'], 2 );
            // The WC_Product object
            $product = $cart_item['data'];
            // Get the price (WooCommerce versions 2.5.x to 3+)
            $product_price = method_exists( $product, 'get_price' ) ? floatval(product->get_price()) : floatval($product->price);
            // New price calculation
            $new_price = $product_price + $added_price;
            // Set the calculeted price (WooCommerce versions 2.5.x to 3+)
            method_exists( $product, 'set_price' ) ? $product->set_price( $new_price ) : $product->price = $new_price;
        }
    }
}

代码放在活动子主题(或主题)的function.PHP文件中,或者放在任何插件文件中.

此代码经过测试,适用于从2.5.x到3的WooCommerce版本.

关于php – 如果没有促销价格,如何显示woocommerce销售价格或正常价格的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于php – WooCommerce – 显示价格包含产品页面的税、php – Woocommerce从外部来源更新价格、php – WooCommerce以定制价格添加到购物车产品、php – WooCommerce价格压倒不起作用的相关信息,请在本站寻找。

本文标签: