的SKU顯示在模板single-product/meta.php與像分類,標籤等數據...
步驟1 - 你應該從這個模板中刪除。
這可以通過將其複製到yourtheme/woocommerce/single-product/meta.php來完成overriding this woocommerce template via your theme。
一旦刪除SKU的一部分,模板代碼如下:
<?php
/**
* Single Product Meta
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/meta.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.0.0
*/
if (! defined('ABSPATH')) {
exit;
}
global $product;
?>
<div class="product_meta">
<?php do_action('woocommerce_product_meta_start'); ?>
<?php echo wc_get_product_category_list($product->get_id(), ', ', '<span class="posted_in">' . _n('Category:', 'Categories:', count($product->get_category_ids()), 'woocommerce') . ' ', '</span>'); ?>
<?php echo wc_get_product_tag_list($product->get_id(), ', ', '<span class="tagged_as">' . _n('Tag:', 'Tags:', count($product->get_tag_ids()), 'woocommerce') . ' ', '</span>'); ?>
<?php do_action('woocommerce_product_meta_end'); ?>
</div>
步驟2 - 將重新排序的價格,把SKU早在其他位置的代碼。
您需要首先刪除現有的代碼(因爲它包含在這裏)。
下面是代碼:
add_action('woocommerce_single_product_summary', 'custom_single_product_summary_actions', 1);
function custom_single_product_summary_actions(){
// Reordering the price (done by you)
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 45);
// Put back the SKU:
add_action('woocommerce_single_product_summary', 'woocommerce_single_custom_sku', 9);
function woocommerce_single_custom_sku(){
global $product;
if (wc_product_sku_enabled() && ($product->get_sku() || $product->is_type('variable'))) :
?>
<p><span class="sku_wrapper"><?php esc_html_e('SKU:', 'woocommerce'); ?> <span class="sku"><?php echo ($sku = $product->get_sku()) ? $sku : esc_html__('N/A', 'woocommerce'); ?></span></span></p>
<?php
endif;
}
}
此功能發生在您的活動子主題(或主題)的function.php文件或也以任何插件文件。
所有代碼都在Woocommerce 3+上測試過並且可以正常工作。
你是直接編輯Woocommerce文件嗎?正確的做法是重寫主題中的woocommerce模板.. https://www.skyverge.com/blog/how-to-override-woocommerce-template-files/ –
SKU顯示在模板[單一產品/ meta.php](https://github.com/woocommerce/woocommerce/blob/release/3.1/templates/single-product/meta.php)與其他數據,如類別,標籤... 所以你應該刪除它[通過您的主題覆蓋此woocommerce模板](https://docs.woocommerce.com/document/template-structure/)。然後將其添加回到您方便的掛鉤功能中。 – LoicTheAztec
不,我不直接編輯Woocommerce文件。我只是將代碼添加到我的主題文件夾中的functions.php文件中。一旦我得到它的工作,我讀了一個插件,我可以使用它將跟蹤我所有的代碼,甚至在一個Woocomerce /主題更新。 – Michael