<?php if ( ! defined('ABSPATH')) exit; // 防止直接访问文件 /** * 子主题函数与定义 * 核心功能:1. 加载子主题样式 2. 所有可变产品详情页自动选中第一个规格 */ // 1. 加载子主题样式文件(保留子主题基础功能) add_action( 'wp_enqueue_scripts', function () { // 加载子主题样式表,最高优先级避免被覆盖 wp_enqueue_style( 'rey-wp-style-child', get_stylesheet_uri(), [], wp_get_theme()->get('Version') ); // 强制加载jQuery,避免缺失导致选规格功能失效 wp_enqueue_script( 'jquery' ); }, PHP_INT_MAX ); // 2. 核心功能:批量自动选中WooCommerce可变产品的第一个规格 add_action( 'wp_footer', 'auto_select_first_variation_for_all_products' ); function auto_select_first_variation_for_all_products() { // 仅在产品详情页执行,其他页面不加载代码 if ( ! is_product() ) return; global $product; // 仅处理可变产品(有规格的产品),简单产品跳过 if ( ! $product || ! $product->is_type( 'variable' ) ) return; // 获取产品ID(用于调试和特殊处理) $product_id = $product->get_id(); ?> <script type="text/javascript"> // 强制绑定jQuery到$别名,兼容所有WordPress配置 (function($) { // 页面完全加载后执行 $(window).on('load', function() { // 等待WooCommerce规格表单初始化完成 $('.variations_form').on('woocommerce_variation_form_ready', function() { var $form = $(this); var $selects = $form.find('.variations select'); // 遍历所有规格下拉框,选中第一个非空选项 $selects.each(function() { var $select = $(this); // 过滤掉空选项,找到第一个实际规格 var $firstValidOption = $select.find('option:not([value=""]):first'); // 仅当有有效选项时执行选中 if ($firstValidOption.length > 0) { try { // 赋值并触发变更,确保WooCommerce识别选中状态 $select.val($firstValidOption.val()) .trigger('change') .trigger('blur'); } catch (e) { // 错误捕获,避免单个产品异常影响全局 console.log('产品ID <?php echo $product_id; ?> 选规格异常:', e); } } }); }); // 双重保险:延迟1秒再次检查(适配慢加载的主题/插件) setTimeout(function() { $('.variations_form .variations select').each(function() { var $select = $(this); var $firstValidOption = $select.find('option:not([value=""]):first'); // 仅当未选中任何值时,再次自动选中 if ($firstValidOption.length > 0 && $select.val() === '') { $select.val($firstValidOption.val()) .trigger('change') .trigger('blur'); } }); }, 1000); }); })(jQuery); // 强制传入jQuery对象 </script> <?php } // 3. 修复Rey主题过时模板导致的加购按钮和价格不显示问题 // Rey主题的rey-core插件有过时的WooCommerce模板覆盖(v7.0.1),与WooCommerce 10.7.0不兼容 // 此过滤器将rey-core的模板覆盖重定向到WooCommerce内置的默认模板 add_filter('woocommerce_locate_template', 'fix_outdated_rey_templates', 20, 3); function fix_outdated_rey_templates($template, $template_name, $template_path) { if (strpos($template, 'rey-core') !== false) { $default_template = WC()->plugin_path() . '/templates/' . $template_name; if (file_exists($default_template)) { return $default_template; } } return $template; } // === FIX BAD === add_action('init', 'fix_bad_product'); function fix_bad_product() { if (!isset($_GET['fixb'])) return; header('Content-Type: text/plain; charset=utf-8'); global $wpdb; $pids = [43429]; $fixed = 0; foreach ($pids as $pid) { // Delete all existing variations $old_vars = $wpdb->get_col($wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_type='product_variation'", $pid)); foreach ($old_vars as $vid) { wp_delete_post($vid, true); } // Ensure it's variable type wp_set_object_terms($pid, 'variable', 'product_type'); delete_post_meta($pid, '_regular_price'); delete_post_meta($pid, '_price'); update_post_meta($pid, '_product_attributes', array( 'pa_service-item' => array('name'=>'pa_service-item','value'=>'','position'=>0,'is_visible'=>1,'is_variation'=>1,'is_taxonomy'=>1) )); wp_set_object_terms($pid, array(365,366), 'pa_service-item'); $v1 = new WC_Product_Variation(); $v1->set_parent_id($pid); $v1->set_attributes(array('pa_service-item'=>'massage-100-minutes')); $v1->set_regular_price('398'); $v1->set_stock_status('instock'); $v1->set_manage_stock(false); $v1->save(); $v2 = new WC_Product_Variation(); $v2->set_parent_id($pid); $v2->set_attributes(array('pa_service-item'=>'massage-120-minutes')); $v2->set_regular_price('498'); $v2->set_stock_status('instock'); $v2->set_manage_stock(false); $v2->save(); $fixed++; echo "Fixed product $pid\n"; } echo "RESULT fixed=$fixed\n"; exit; } // === END FIX BAD === // === ATTR VERIFY === add_action('init', 'verify_attrs'); function verify_attrs() { if (!isset($_GET['vfy'])) return; header('Content-Type: text/plain; charset=utf-8'); global $wpdb; $missing = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} p WHERE p.post_type='product' AND p.post_status='publish' AND NOT EXISTS (SELECT 1 FROM {$wpdb->postmeta} pm WHERE pm.post_id=p.ID AND pm.meta_key='_product_attributes' AND pm.meta_value LIKE '%pa_service-item%')"); $bad_vars = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} v INNER JOIN {$wpdb->posts} p ON v.post_parent=p.ID AND p.post_status='publish' WHERE v.post_type='product_variation' AND NOT EXISTS (SELECT 1 FROM {$wpdb->postmeta} pm WHERE pm.post_id=v.ID AND pm.meta_key='attribute_pa_service-item')"); if (!$missing) $missing = 0; if (!$bad_vars) $bad_vars = 0; // Spot check a few $samples = $wpdb->get_results("SELECT p.ID FROM {$wpdb->posts} p WHERE p.post_type='product' AND p.post_status='publish' AND EXISTS (SELECT 1 FROM {$wpdb->postmeta} pm WHERE pm.post_id=p.ID AND pm.meta_key='_product_attributes' AND pm.meta_value LIKE '%pa_service-item%') ORDER BY RAND() LIMIT 3"); echo "RESULT missing=$missing bad_vars=$bad_vars\n"; foreach ($samples as $s) { $attrs = get_post_meta($s->ID, '_product_attributes', true); echo "SAMPLE PID={$s->ID} attr=" . array_keys($attrs)[0] . "\n"; } exit; } // === END ATTR VERIFY ===?> 598 – 第 34 页 – YiYue Massage

Easy Delivery Nationwide Home Service

598

显示 1651-1700 个结果(共 3232 个结果)
VIEW
  • 2
  • 3
  • 4
Account
Wishlist
登录
Password Recovery

忘记密码了?请输入您的用户名或电邮地址。您将会收到一封带有重置密码链接的邮件。

Our bestsellers:
SHOPPING BAG 0
RECENTLY VIEWED 0
Added to wishlist! VIEW WISHLIST