5月23日: 为woocommerce的单品页面增加一个buy now立刻购按钮,只需在主题的functions.php里增加以下代码:
/* Create Buy Now Button dynamically after Add To Cart button */
function add_content_after_addtocart() {
// get the current post/product ID
$current_product_id = get_the_ID();
// get the product based on the ID
$product = wc_get_product( $current_product_id );
// get the "Checkout Page" URL
$checkout_url = WC()->cart->get_checkout_url();
// run only on simple products
if( $product->is_type( 'simple' ) ){
echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Buy Now</a>';
//echo '<a href="'.$checkout_url.'" class="buy-now button">Buy Now</a>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
并且在自定义主题增加一个CSS样式:
.woocommerce a.button{margin-left:10px;}
.woocommerce button.button, .woocommerce input.button{margin-bottom:5px;}
因此,主题不再能随便更新,否则失效。这时候就能体现父子主题安装的好处。下一步,学习下如何增加一个子主题,把对主题所修改的内容放在子主题上,可以方便主题升级。
特此记录。
说两句?