1,数量选择器之后添加图标
add_action( 'woocommerce_after_add_to_cart_quantity', 'add_content_after_add_to_cart_quantity' );
/*
* 数量之后添加图标
*/
function add_content_after_add_to_cart_quantity() {
// Echo content.
echo '<div style="float: right"><img src="//cdn.shopify.com/s/files/1/1660/4869/t/41/assets/fs-badge-h-blue.png?v=14692974008898943639" style="padding-left:5px;height:20px;" alt="Free Shipping">
<img src="https://s3.bmp.ovh/imgs/2022/03/d4365a80e77709a4.png" style="padding-left:5px;height:20px;" alt="Best Deals"></div>';
}
2,价格之后添加内容
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
$wording = ' <img src="https://s3.bmp.ovh/imgs/2022/03/f16070d609ee6bb0.png" style="padding-left:5px;height:30px;" alt="Best Price">';
return $price . $wording;
}
3,结算界面手机号码移到最前
// WC - Checkout - Phone on first place
add_filter( 'woocommerce_checkout_fields', 'phone_after_email' );
function phone_after_email( $checkout_fields ) {
$checkout_fields['billing']['billing_phone']['priority'] = 4;
$checkout_fields['shipping']['shipping_phone']['priority'] = 4;
return $checkout_fields;
}
4,加购按钮显示产品金额
//加购按钮显示金额
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
// Variable products
if( $product->is_type('variable') ) {
// shop and archives
if( ! is_product() ){
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
return $button_text . ' - From ' . strip_tags( $product_price );
}
// Single product pages
else {
$variations_data =[]; // Initializing
// Loop through variations data
foreach($product->get_available_variations() as $variation ) {
// Set for each variation ID the corresponding price in the data array (to be used in jQuery)
$variations_data[$variation['variation_id']] = $variation['display_price'];
}
?>
<script>
var globPrice = 0;
jQuery(function($) {
var jsonData = <?php echo json_encode($variations_data); ?>,
inputVID = 'input.variation_id';
$(document).ready(function () {
$('input').change( function(){
if( '' != $(inputVID).val() ) {
var vid = $(inputVID).val(),
vprice = '';
$.each( jsonData, function( index, price ) {
if( index == $(inputVID).val() ) {
vprice = price.toFixed(2);
}
});
var xx = "<?php echo get_woocommerce_currency_symbol(); ?>";
globPrice = vprice;
$(".single_add_to_cart_button").html("<span>" + xx + vprice + " ADD TO CART" + "</span>");
}
});
$(document).on('change', "#regular_delivery_option_select", function () {
var attribute_value = $(this).data("custom_data");
var input_value = $(this).val();
if(attribute_value['subscription_scheme']){
//console.log(attribute_value['subscription_scheme']);
var btnString = $(".single_add_to_cart_button").html();
btnString = btnString.replace(/<[^>]*>/g, "");
var btnVars = btnString.split(' ');
var lenMinOne = btnVars.length - 1;
var lenMinTwo = btnVars.length - 2;
globPrice = globPrice || parseFloat(btnVars[lenMinOne]);
//if(globCount == 0){globCount = 1;globPrice = (globPrice * (100/75)) - (100/75);}
var vprice = (globPrice - (globPrice * (parseFloat(attribute_value['subscription_scheme'].discount)/100))).toFixed(2);
//$(".single_add_to_cart_button").html("<span>" + btnVars[lenMinTwo] + vprice + ' ADD TO CART' + "</span>");
$(".single_add_to_cart_button").html("<span>JUST ADD TO CART</span>");
}
});
});
});
</script><?php
return $button_text;
}
}
// All other product types
else {
$product_price = wc_price( wc_get_price_to_display( $product ) );
return $button_text . ' ' . strip_tags( $product_price );
}
}