我以前使用这个答案来隐藏基于所选运输方法的结账字段,它工作正常,直到更新(3.4.2当前版本)我认为不确定已经改变但它不再按预期工作.
以前当选择本地拾取时,某些字段被隐藏并且是可选的,当选择传送时,它将再次通过动态显示这些字段而不重新加载页面.
现在它按要求显示和隐藏字段,但是,当选择传递时,它会显示标记为必需的正确字段,但旁边还有(可选)符号,这使其成为可选字段.见下图.
这是我修改过的剪贴板:
add_filter('woocommerce_default_address_fields', 'custom_default_checkout_fields', 10, 1 );
function custom_default_checkout_fields( $address_fields ) {
$custom_fields = array( 'country', 'address_1', 'address_2', 'state', 'postcode');
foreach($custom_fields as $field)
$address_fields[$field]['required'] = false;
return $address_fields;
}
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {
$pickpoint = 'local_pickup:2';
$free_delivery = 'free_shipping:1';
$flat_rate = 'flat_rate:3';
$required = esc_attr__( 'required', 'woocommerce' );
?>
<script>
jQuery(function($){
var shippingMethod = $('input[name^="shipping_method"]:checked'),
required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
shippingChecked = $('input#ship-to-different-address-checkbox');
shippingChecked.change( function(){
console.log('Shipping Checked: '+shippingChecked.prop('checked'));
}); …Run Code Online (Sandbox Code Playgroud) 我创建了一个函数来显示一些带有短代码的产品,但我遇到的问题是错误消息未显示在该页面上。例如,如果某些字段是必需的,那么它只会显示在购物车/结帐页面上。
这是我的一些代码:
while ( $query->have_posts() ) : $query->the_post();
global $product;
?>
<div style="border-bottom:thin dashed black;margin-bottom:15px;">
<h2><?php the_title(); ?> <span><?php echo $product->get_price_html();?></span></h2>
<p><?php the_excerpt();?></p>
<?php global $product;
if( $product->is_type( 'simple' ) ){
woocommerce_simple_add_to_cart();
}
Run Code Online (Sandbox Code Playgroud)
我需要添加什么才能在使用短代码的页面上显示错误消息?
我创建了一个短代码,通过以下查询按类别显示产品:
$atts = shortcode_atts( array (
'type' => 'product',
'posts' => -1,
'category' => '',
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'posts_per_page' => $atts['posts'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
) ),
) );
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但是当我使用此处显示的自定义排序对产品进行排序时,我尝试使用按属性排序
我补充说:'orderby' => 'modified',并从这里尝试了其他一些。
两个问题:
使用自定义排序时需要使用哪个属性进行排序
和
orderby添加到上述查询时,我需要做什么才能使我的属性起作用?因为我使用它的哪个属性值总是按发布的降序排序。
我试图更新数组中的值(如果找到),如果没有,则向其中添加一个新数组。
这是我一直在尝试的一些代码:
var cartItems = {};
var items = []
cartItems.items = items;
$('.proAdd').click(function(){
var name = $(this).attr("data-name");
var price = parseFloat($(this).attr("data-price"));
var quantity = parseInt($(this).attr("data-quantity"));
var item = {"name": name,"price": price,"quantity": quantity}
items.forEach(function(item) {
if (item.name === name) {
item.quantity = 2
return;
}else{
cartItems.items.push(item);
}
});
Run Code Online (Sandbox Code Playgroud)
在此版本中,注释被推送。如果我取出 else 分支,那么它会更新它,但也会推送它。我为此创建了一个小提琴。
也尝试过这个,但它说 x.quantity 未定义:
var index = items.findIndex(x => x.name==name)
if (index != -1){
x.quantity = 2
}
else {
cartItems.items.push(item);
}
Run Code Online (Sandbox Code Playgroud) php ×3
woocommerce ×3
wordpress ×3
jquery ×2
product ×2
array-push ×1
javascript ×1
shipping ×1
shortcode ×1
sorting ×1