例如,我试图为Phone Model属性重写产品变体url,例如这个url:
index.php?product=example&attribute_pa_model=iphone-x
当我在浏览器中直接打开它时,这是可行的。所以我想要的原始网址是:
/product/example/iphone-x
我尝试使用下面的代码,但它不起作用。
function add_model_taxonomy_args($args) {
$args['query_var'] = 'attribute_pa_model';
return $args;
}
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );
function custom_rewrite_rules() {
add_rewrite_tag('%attribute_pa_model%', '([a-zA-Z0-9-]+)');
add_rewrite_rule('^product/(.+?)/(.+?)/?, 'index.php?product=$matches[1]&attribute_pa_model=$matches[2]', 'top');
}发布于 2018-09-04 10:03:16
这是因为在此URL上设置了$_REQUEST['attribute_pa_model'],在本例中,值为iphone-x:
index.php?product=example&attribute_pa_model=iphone-x但是在这个网址上,$_REQUEST['attribute_pa_model']没有被设置,所以产品变化的自动选择不起作用:
/product/example/iphone-x因此,在该URL上,您可以使用woocommerce_dropdown_variation_attribute_options_args筛选选定的值,如下所示:
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'auto_select_attribute_pa_model' );
function auto_select_attribute_pa_model( $args ) {
// If it's not the pa_model taxonomy, don't filter the $args.
if ( empty( $args['selected'] ) && 'pa_model' === $args['attribute'] ) {
$args['selected'] = get_query_var( 'attribute_pa_model' );
}
return $args;
}在我的测试中,这是不必要的,可以删除:
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );https://wordpress.stackexchange.com/questions/313048
复制相似问题