woocommerce php,要覆盖写在woocommerce-functions.php文件中的函数

这篇博客介绍了如何在不直接修改WooCommerce核心文件的情况下,通过使用WordPress的钩子系统、子主题和模板覆盖来定制和扩展WooCommerce的功能。作者提到了删除并替换操作、使用过滤器和动作钩子以及创建子主题来重写特定函数,例如针对移动设备显示视频播放按钮的函数。此外,还提供了关于如何在子主题中覆盖特定函数的代码示例。

我想修改/覆盖写在woocommerce-functions.php文件中的函数, 但是我不想修改woocommerce-functions.php文件。那就是我想在插件或主题中实现这一点。

#1

可以覆盖woocommerce功能, 我最近做了这件事, 并将所有woocommerce扩展功能添加到主题的functions.php文件中, 以便woocommerce插件文件保持不变并可以安全更新。

此页面提供了一个示例, 说明如何删除他们的操作并将其替换为你自己的操作-http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp

该页面提供了扩展其功能而不删除其功能以及使用子主题的示例-http://uploadwp.com/customizing-the-woocommerce-checkout-page/

希望这可以帮助 :)

#2

WooCommerce提供了一个模板系统。可以覆盖woocommerce功能。无需修改核心文件即可自定义WooCommerce的一种好方法是使用钩子-

如果使用挂钩添加或操作代码, 则可以将自定义代码添加到主题functions.php文件中。

使用动作挂钩-

要执行自己的代码, 请使用动作钩子do_action(‘action_name’);进行插入。

参见下面的示例, 了解将代码放置在何处:

add_action('action_name', 'your_function_name');

function your_function_name()

{

// Your code

}

使用过滤钩

过滤器挂钩在整个代码中都使用apply_filter(‘filter_name’, $ variable)进行调用;

要操纵传递的变量, 可以执行以下操作:

add_filter('filter_name', 'your_function_name');

function your_function_name( $variable )

{

// Your code

return $variable;

}

在这里你可以获取WooCommerce操作和筛选器挂钩-https://docs.woothemes.com/wc-apidocs/hook-docs.html

#3

如果你有子主题, 则可以将相关文件复制到主题并重写副本。该副本将优先于WooCommerce版本使用。

#4

我需要为移动设备上的视频添加”播放”按钮(默认情况下, 此按钮仅显示在桌面上)。

我需要重写wp-content / themes / gon / framework / theme_functions.php中的函数:

function ts_template_single_product_video_button(){

if( wp_is_mobile() ){

return;

}

global $product;

$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);

if( !empty($video_url) ){

$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;

echo '';

}

}

我发现此说明指出, 如果你使用挂钩添加或操作代码, 则可以将自定义代码添加到主题的functions.php文件中。

我已经有了wp-content / themes / gon-child / functions.php(即原始的gon主题已复制到gon-child), 所以我要做的是:

// Enable tour video on mobile devices

remove_action('ts_before_product_image', 'ts_template_single_product_video_button', 1);

add_action('ts_before_product_image', 'ts_template_single_product_video_button_w_mobile', 1);

function ts_template_single_product_video_button_w_mobile(){

global $product;

$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);

if( !empty($video_url) ){

$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;

echo '';

}

}

?>

你刚生成cart.php而已,怎么断了? <?php /** * 简化版购物车页面 * 保持界面不变,移除复杂的选择性结算逻辑 */ if (!defined('ABSPATH')) { exit; } do_action('woocommerce_before_cart'); ?> <div class="cart-page-section container" style="max-width: 1200px; margin: 0 auto;"> <form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post"> <?php do_action('woocommerce_before_cart_table'); ?> <?php wp_nonce_field('woocommerce-cart', 'woocommerce-cart-nonce'); ?> <table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents"> <thead> <tr> <th class="product-select" style="width: 8%;"> <input type="checkbox" id="select-all-items" /> <label for="select-all-items" style="display: inline-block; margin-left: 5px; cursor: pointer;">全选</label> </th> <th class="product-info"><?php esc_html_e('Product', 'woocommerce'); ?></th> <th class="product-price"><?php esc_html_e('Price', 'woocommerce'); ?></th> <th class="product-quantity"><?php esc_html_e('Quantity', 'woocommerce'); ?></th> <th class="product-subtotal"><?php esc_html_e('Subtotal', 'woocommerce'); ?></th> <th class="product-remove"><?php esc_html_e('操作', 'woocommerce'); ?></th> </tr> </thead> <tbody> <?php do_action('woocommerce_before_cart_contents'); ?> <?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) : ?> <?php $_product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key); $product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key); if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_cart_item_visible', true, $cart_item, $cart_item_key) ) : $product_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($cart_item) : '', $cart_item, $cart_item_key); // 使用原始数值行总计作为data-price(含税小计) $line_total_value = (float) ($cart_item['line_total'] + $cart_item['line_tax']); ?> <tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters('woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key) ); ?>"> <!-- Checkbox Column --> <td class="product-select" data-title="<?php esc_attr_e('Select', 'woocommerce'); ?>"> <input type="checkbox" class="item-checkbox" name="selected_items[]" value="<?php echo esc_attr($cart_item_key); ?>" data-price="<?php echo esc_attr($line_total_value); ?>" data-unit-price="<?php echo esc_attr($line_total_value / max(1, $cart_item['quantity'])); ?>" data-quantity="<?php echo esc_attr($cart_item['quantity']); ?>" /> </td> <!-- Product Info Column --> <td class="product-info" data-title="<?php esc_attr_e('Product', 'woocommerce'); ?>"> <div class="product-image"> <?php $thumbnail = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key); if ( ! $product_permalink ) : echo $thumbnail; // PHPCS: XSS ok. else : printf('<a href="%s">%s</a>', esc_url($product_permalink), $thumbnail); // PHPCS: XSS ok. endif; ?> </div> <div class="product-name"> <?php if ( ! $product_permalink ) : echo wp_kses_post( apply_filters('woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key) . ' ' ); else : echo wp_kses_post( apply_filters('woocommerce_cart_item_name', sprintf('<a href="%s">%s</a>', esc_url($product_permalink), $_product->get_name()), $cart_item, $cart_item_key) ); endif; do_action('woocommerce_after_cart_item_name', $cart_item, $cart_item_key); echo wc_get_formatted_cart_item_data($cart_item); // PHPCS: XSS ok. ?> </div> </td> <!-- Price Column --> <td class="product-price" data-title="<?php esc_attr_e('Price', 'woocommerce'); ?>"> <?php echo apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key); // PHPCS: XSS ok. ?> </td> <!-- Quantity Column --> <td class="product-quantity" data-title="<?php esc_attr_e('Quantity', 'woocommerce'); ?>"> <?php if ( $_product->is_sold_individually() ) : $product_quantity = sprintf('1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key); else : $product_quantity = woocommerce_quantity_input( array( 'input_name' => "cart[{$cart_item_key}][qty]", 'input_value' => $cart_item['quantity'], 'max_value' => $_product->get_max_purchase_quantity(), 'min_value' => '0', 'product_name' => $_product->get_name(), ), $_product, false ); endif; echo apply_filters('woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item); // PHPCS: XSS ok. ?> <small class="qty-status" style="display:none;margin-left:8px;color:#666;">保存中…</small> </td> <!-- Subtotal Column --> <td class="product-subtotal" data-title="<?php esc_attr_e('Subtotal', 'woocommerce'); ?>"> <?php echo apply_filters('woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal($_product, $cart_item['quantity']), $cart_item, $cart_item_key); // PHPCS: XSS ok. ?> </td> <!-- Remove Item Column --> <td class="product-remove" data-title="<?php esc_attr_e('操作', 'woocommerce'); ?>"> <?php echo apply_filters('woocommerce_cart_item_remove_link', sprintf( '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">×</a>', esc_url( wc_get_cart_remove_url($cart_item_key) ), esc_attr__('Remove this item', 'woocommerce'), esc_attr($product_id), esc_attr($_product->get_sku()) ), $cart_item_key); ?> </td> </tr> <?php endif; ?> <?php endforeach; ?> <?php do_action('woocommerce_after_cart_contents'); ?> </tbody> </table> <?php do_action('woocommerce_after_cart_table'); ?> </form> </div> <!-- Sticky Footer --> <div class="cart-footer-actions sticky-footer" style="position: sticky; bottom: 0; background: white; padding: 15px; border-top: 1px solid #ddd; max-width: 1200px; margin: 0 auto;"> <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;"> <div class="footer-left"> <input type="checkbox" id="footer-select-all"> <label for="footer-select-all" style="display: inline-block; margin-left: 5px; cursor: pointer;">全选</label> <button type="button" class="button" id="remove-selected-items">刪除選中的商品</button> <button type="button" class="button" id="clear-cart">清空購物車</button> </div> <div class="coupon-section"> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="输入优惠券代码" style="padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; margin-right: 5px;" /> <button type="button" class="button" id="apply-coupon">应用优惠券</button> </div> </div> <div style="display: flex; justify-content: space-between; align-items: center;"> <div class="selected-summary" style="font-size: 16px; font-weight: bold;"> 已选商品: <span id="selected-count">0</span> 件,共计: <span id="selected-total">RM0.00</span> </div> <a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="checkout-button button alt wc-forward" id="partial-checkout">结算</a> </div> </div> <?php do_action('woocommerce_after_cart'); ?> <style> /* 保持原有样式不变 */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; text-align: center; vertical-align: middle; white-space: nowrap; } .cart-page-section table.cart td.product-info { width: 37%; vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1); z-index: 1000; display: flex; flex-direction: column; gap: 15px; } .footer-left { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; } .coupon-section { display: flex; align-items: center; gap: 5px; } .selected-summary { font-size: 16px; font-weight: bold; flex: 1; } .cart-footer-actions .button { padding: 10px 20px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; white-space: nowrap; } .cart-footer-actions .button:hover { background-color: #d32f2f; } #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; transition: background-color 0.3s; white-space: nowrap; text-align: center; font-weight: bold; } #partial-checkout:hover { background-color: #0b7dda; } #coupon_code { padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; transition: border-color 0.3s; } #coupon_code:focus { border-color: #2196F3; outline: none; } /* 数量输入框锁定效果 */ .qty.saving { background-color: #f0f8ff; cursor: not-allowed; } /* 响应式样式 */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } .cart-page-section table.cart td.product-select::before { content: "选择"; } .cart-footer-actions { flex-direction: column; align-items: flex-start; } .footer-left { width: 100%; justify-content: space-between; } .coupon-section { width: 100%; margin-top: 10px; } .coupon-section input { flex: 1; } .selected-summary { text-align: center; margin-bottom: 10px; } #partial-checkout { width: 100%; padding: 15px; } .cart-footer-actions .button { padding: 12px 15px; margin: 5px 0; width: 100%; text-align: center; } } @media (max-width: 480px) { .cart-page-section { padding: 15px; } .cart-page-section table.cart td { padding-left: 45%; } .cart-page-section table.cart td::before { font-size: 14px; } .cart-footer-actions { padding: 10px; } #coupon_code { width: 100%; } } </style> <script> jQuery(function($){ // ----------------------------- // 辅助函数 // ----------------------------- function getAjaxUrl(){ return (window.wc_cart_params && window.wc_cart_params.ajax_url) || '<?php echo esc_url( admin_url("admin-ajax.php") ); ?>'; } function getCartNonce(){ var n = $('input[name="woocommerce-cart-nonce"]').val(); if (n) return n; if (window.wc_cart_params && window.wc_cart_params.cart_nonce) return window.wc_cart_params.cart_nonce; return '<?php echo wp_create_nonce("woocommerce-cart"); ?>'; } function getSelectedKeys(){ return $('.item-checkbox:checked').map(function(){ return this.value; }).get(); } function fmtRM(n){ n = isNaN(n) ? 0 : n; return 'RM' + Number(n).toFixed(2); } // ----------------------------- // 选择持久化 (localStorage + 跨标签页同步) // ----------------------------- var LS_KEY = 'cart_selected_items'; function readSelection(){ try { return JSON.parse(localStorage.getItem(LS_KEY) || '[]'); } catch(e){ return []; } } function writeSelection(keys){ try { localStorage.setItem(LS_KEY, JSON.stringify(keys || [])); } catch(e){} } function restoreSelectionFromLS(){ var saved = readSelection(); if (!Array.isArray(saved)) saved = []; $('.item-checkbox').each(function(){ var key = this.value; $(this).prop('checked', saved.indexOf(key) !== -1); }); } window.addEventListener('storage', function(ev){ if (ev.key === LS_KEY) { restoreSelectionFromLS(); updateSelectedSummary(); } }); // ----------------------------- // 已选商品摘要 // ----------------------------- function updateSelectedSummary(){ var total = 0, count = 0; $('.item-checkbox:checked').each(function(){ var unitPrice = parseFloat($(this).data('unit-price')); var quantity = parseInt($(this).data('quantity')); if (!isNaN(unitPrice) && !isNaN(quantity)) { total += unitPrice * quantity; count++; } }); $('#selected-count').text(count); $('#selected-total').text(fmtRM(total)); var allChecked = $('.item-checkbox').length > 0 && $('.item-checkbox:checked').length === $('.item-checkbox').length; $('#select-all-items, #footer-select-all').prop('checked', allChecked); // 持久化选择 writeSelection(getSelectedKeys()); } // ----------------------------- // 全选切换 // ----------------------------- $('#select-all-items, #footer-select-all').off('change.sc').on('change.sc', function(){ var checked = $(this).prop('checked'); $('.item-checkbox').prop('checked', checked); updateSelectedSummary(); }); $(document).on('change', '.item-checkbox', updateSelectedSummary); // ----------------------------- // 删除选中商品 // ----------------------------- $('#remove-selected-items').off('click.sc').on('click.sc', function(){ var keys = getSelectedKeys(); if (!keys.length) { alert('请选择要删除的商品'); return; } if (!confirm('确定要删除选中的商品吗?')) return; $.ajax({ url: getAjaxUrl(), method: 'POST', dataType: 'json', data: { action: 'remove_selected_cart_items', selected_items: keys, security: getCartNonce() } }).done(function(res){ if (res && res.success)
最新发布
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值