执行JS - Woocommerce的PHP函数



我有一个JS函数,我试图通过我的functions.php从我的子主题内执行。这是我第一次使用PHP,如果我的代码很差,我很抱歉。

我的目标是尝试将page_title更改为在下拉过滤器中选择的内容。在此工作之后,我计划将category_name添加到. innerhtml的末尾。这只会在选择了下拉菜单的情况下发生。如果没有,那么分类将通过woocommerce本地处理的方式显示。

var machineEl = document.getElementsByClassName('woof_select_pa_machine').firstChild;
var machineValue = machineEl.value;

machineEl.addEventListener('change', function dynamic_page_title() {
if (machineValue != 'Product Machine') {

document.getElementsByClassName('woocommerce-products-header__title').innerHTML = machineValue;
}  
});

这是我尝试在function.php中执行的第一个脚本。

<?php
echo '<script type="text/javascript">',
'dynamic_page_title_js();',
'</script>';
?>

或者,我试图使用以下内容来完成相同的事情,但意图仅限于某些页面。虽然不确定我最后是否需要这个。我在找到的其他线程中使用了这组代码。

<?php
add_action ( 'wp_enqueue_scripts', 'dynamic_page_title_php');
function dynamic_page_title_php() {
//$wp_query->get_queried_object()->cat_name;
global $page_title;
if( is_page() || is_single() )
{
switch($page_title->title_name ) // post_name is the post slug which is more consistent for matching to here
{
case 'shop':
wp_enqueue_script('shop', get_template_directory_uri() . '/js/dynamic_page_title_js.js', array('jquery'), '', false);
break;

}
} 

}
?>

这是我想要覆盖的类的html。

<h1 class="woocommerce-products-header__title page-title">Sample Page</h1>

我决定另辟蹊径,将问题分解。我使用了一个js函数来更新page_title的过滤器选择。然后,我使用了另一个线程提供的代码(Woocommerce:如何在类别页面和"过滤"中显示标题上的产品属性名称)。通过'?pa_attribute='在地址栏上)来处理我的function.php文件中的属性部分。php函数处理加载时用属性名(在本例中是机器类型)替换shop的页面。js会更新这个属性,并在filter select中添加类别。

当用户通过导航栏导航到一个类别时,还需要在functions.php上做一些工作来将类别名称添加到page_title。我发布了一个关于如何解决这最后一部分的新问题(Woocommerce:如何在标题上显示产品属性名称和类别名称),但通过独立利用这两者,它或多或少完成了我的意图。

另外,我目前正在执行的JS woof- woocommerce -product-filter插件。它是在Ajax完成后在JS中编写的。但是我确实计划将它移到它自己的文件中,并且只从组织方面调用该函数。

PHP

add_filter( 'woocommerce_page_title', 'custom_woocommerce_page_title', 15, 2 );
function custom_woocommerce_page_title( $page_title ) {
if ( is_archive() ) {
$exists_attr = false;
foreach ( $_GET as $index => $value ) {
if ( substr( $index, 0, 3 ) === 'pa_' ) {
$attr_id = wc_attribute_taxonomy_id_by_name( $index );
if ( $attr_id === 0 && $cat_id ) {
continue;
}
if ( ! $exists_attr /* && ! $exists_cat */) {
$exists_attr = true;
$page_title .= ' ';
} else {
$page_title .= ' ';
}
$term = get_term_by( 'slug', esc_html( $value ), $index );
$page_title =  $term->name;                     

}
}
}
return $page_title;
}

JS

(function() {
var machineEl = document.getElementsByClassName('woof_select woof_select_pa_machine')[0];
var processEl = document.getElementsByClassName('woof_select woof_select_pa_processing')[0];
var optionMachine = machineEl.querySelector("option[selected='selected']");
var optionProcess = processEl.querySelector("option[selected='selected']");
var machineValue = optionMachine.innerHTML;
var processValue = optionProcess.innerHTML;
var result = document.getElementsByClassName('woocommerce-products-header__title page-title')[0];
if (machineValue != 'Product Machine' && processValue != 'Product Processing') {
result.innerHTML = machineValue + " " + processValue;            
}
else if (machineValue != 'Product Machine') {
result.innerHTML = machineValue;            
}
else if (processValue != 'Product Processing') {
result.innerHTML = processValue;            
}       
})()

一旦我有类别张贴到page_title,我将更新这个答案。

最新更新