2017-04-11 78 views

回答

1

更新:新增WC 3+兼容性

有3相关的定制挂钩函数你的情况,你将需要自定义:

### Custom Product link ### 

// Removing the default hooked function 
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10); 
// Add back the hook to a custom function 
add_action ('woocommerce_before_shop_loop_item', 'custom_loop_product_link', 10); 
function custom_loop_product_link() { 
    $custom_link = get_permalink(16); // link to a custom page 
    echo '<a href="' . $custom_link . '" class="woocommerce-LoopProduct-link">'; 
} 

### Custom add-to-cart link ### 

add_filter('woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2); 
function customizing_add_to_cart_button($link, $product){ 
    // CUSTOM ADD TO CART text and link 
    $add_to_cart_url = site_url('/custom_link/'); 
    $button_text = __('View now', 'woocommerce'); 

    // compatibility with WC +3 
    $product_id = method_exists($product, 'get_id') ? $product->get_id() : $product->id; 
    $product_type = method_exists($product, 'get_type') ? $product->get_type() : $product->product_type; 

    $link = sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>', 
     esc_url($add_to_cart_url), 
     esc_attr($product_id), 
     esc_attr($product->get_sku()), 
     esc_attr(isset($quantity) ? $quantity : 1), 
     esc_attr($product_type), 
     esc_html($button_text) 
    ); 

    return $link; 
} 

### Custom link Redirect after add-to-cart ### 

add_filter('woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1); 
function my_custom_add_to_cart_redirect($url) { 
    $url = get_permalink(16); // an example: here redirect is on page ID 16 
    return $url; 
} 

该代码在你的活动子主题(或主题)的function.php文件中,或者也在任何插件文件中。

+0

谢谢。你是否有这样的代码,这也适用于商店页面的图片和产品标题? – Chrissieg

相关问题