Override External Product URL to “Add to Cart” product button

前端 未结 3 1019
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 00:45

I work on site that use External products from Amazon, but want instead pointing users to that external URL, first to add to cart that product. I have this function, that ch

3条回答
  •  生来不讨喜
    2020-12-04 01:02

    I fixed myself. For External products, to replace default "Buy This Product" with other generic text, add this functions into functions.php file into theme:

    add_filter( 'woocommerce_product_add_to_cart_text' , 
    'wpf_custom_add_cart_text_archive',11);
    
    
    function wpf_custom_add_cart_text_archive() {
    
       global $product;
    
    
       $product_type = $product->product_type;
    
    
       switch ( $product_type ) {
    
              case 'external':
    
                     return __( 'Add to Cart', 'woocommerce' );
    
                     break;
    
              case 'grouped':
    
                     return __( 'View products', 'woocommerce' );
    
                     break;
    
              case 'simple':
    
                     return __( 'Add to cart', 'woocommerce' );
    
                     break;
    
              case 'variable':
    
                     return __( 'Select options', 'woocommerce' );
    
                     break;
    
              default:
    
                     return __( 'Read more', 'woocommerce' );
    
       }
    
    }
    
       add_filter( 'woocommerce_product_single_add_to_cart_text', 
    'wpf_custom_add_cart_text',11);
    

    and this one:

    function wpf_custom_add_cart_text() {
    
      return __( 'Add to Cart', 'woocommerce' );
    
     }
    

    to replace text everywhere.

提交回复
热议问题