Cart / Checkout: Custom Product Default to Autoship and Frequency with Message

Defaults to a product with a custom meta flag (xx_autoship_only_flag) to Autoship on Product and Cart pages. Hide Autoship options and display a custom Autoship message based on product type. 

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

/**
* Custom Code Snippet to:
* 1. Default the Autoship Options to Autoship on Product & Cart Pages
* 2. Hide Autoship options on Product and Cart Pages
* 3. Display Autoship & Save Message on Product & Cart Pages
* 4. Only applies to products with a custom metadata field of xx_autoship_only_flag set to 'yes'
*/

/**
* Default the Autoship option for both Simple and
* Variable products by default.
*/
function xx_default_autoship_for_all( $product ){
    return 'yes';
  }
  add_filter('autoship_default_product_schedule_options_choice_value', 'xx_default_autoship_for_all', 10 ,1);
  
  /**
  * Outputs custom css to hide the Autoship Radio Options for a specific Autoship Product
  * NOTE Uses the 'xx_autoship_only_flag' metadata field for a product.
  * @param WC_Product $product The woocommerce product
  */
  function xx_hide_non_autoship_option( $product ){
  
    // Get the Metadata flag for the product.
    $autoship_only = get_post_meta( $product->get_id(), 'xx_autoship_only_flag', true );
  
    // Exclude these two lines to hide for all autoship products.
    if ( 'yes' != $autoship_only )
    return
  
    ob_start();?>
  
    <style>
    .autoship-schedule-options > div:nth-of-type(1),
    .autoship-schedule-options > div:nth-of-type(2) { display: none; }
    </style>
  
    <?php
    echo ob_get_clean();
  
  }
  add_action('autoship_before_schedule_options', 'xx_hide_non_autoship_option', 10, 1 );
  add_action('autoship_before_schedule_options_variable', 'xx_hide_non_autoship_option', 10, 1 );
  
  
  /**
  * Outputs custom div to hide the Autoship Radio Options for a specific Autoship Product
  * on the cart page
  * NOTE Uses the 'xx_autoship_only_flag' metadata field for a product.
  * @param WC_Product $product The woocommerce product
  */
  function xx_hide_non_autoship_option_on_cart( $product ){
  
    // Get the Metadata flag for the product.
    $autoship_only = get_post_meta( $product->get_id(), 'xx_autoship_only_flag', true );
  
    // Exclude these two lines to hide for all autoship products.
    if ( 'yes' == $autoship_only )
    echo '<div style="display:none">';
  
  }
  add_action('autoship_before_cart_schedule_options', 'xx_hide_non_autoship_option', 9, 1 );
  
  /**
  * Outputs custom div to hide the Autoship Radio Options for a specific Autoship Product
  * on the cart page
  * NOTE Uses the 'xx_autoship_only_flag' metadata field for a product.
  * @param WC_Product $product The woocommerce product
  */
  function xx_hide_non_autoship_option_on_cart_after( $product ){
  
    // Get the Metadata flag for the product.
    $autoship_only = get_post_meta( $product->get_id(), 'xx_autoship_only_flag', true );
  
    // Exclude these two lines to hide for all autoship products.
    if ( 'yes' == $autoship_only )
    echo '</div>';
  
  }
  add_action('autoship_after_cart_schedule_options', 'xx_hide_non_autoship_option', 9, 1 );
  
  /**
  * Outputs custom message for always Autoship Products
  * NOTE Uses the 'xx_autoship_only_flag' metadata field for a product.
  * @param WC_Product $product    The wc product
  */
  function xx_show_always_autoship_option_notice( $product ){
  
    // Get the Metadata flag for the product.
    $autoship_only = get_post_meta( $product->get_id(), 'xx_autoship_only_flag', true );
  
    // Exclude these two lines to hide for all autoship products.
    if ( 'yes' == $autoship_only )
    return
  
    ?>
  
    <div class="autoship-type">
      <label class="autoship-label">
  
      <?php
  
        // Output the Extended Discount String based on product type
        echo $product->is_type('variable') ?
        autoship_checkout_recurring_variable_discount_string( $product->get_id() ):
        autoship_checkout_recurring_discount_string( $product->get_id() );
  
        // Output the Autoship Product Message
        echo autoship_product_message_string( $product->get_id() );
  
      ?>
  
      </label>
    </div>
  
    <?php
  
  }
  add_action('autoship_after_schedule_radio_options', 'xx_show_always_autoship_option_notice', 10, 1 );
  add_action('autoship_after_schedule_radio_options_variable', 'xx_show_always_autoship_option_notice', 10, 1 );
  add_action('autoship_after_cart_schedule_radio_options', 'xx_show_always_autoship_option_notice', 10, 1 );

Still need help? Contact Us Contact Us