Frage

I want to make custom button for some products in place of add to cart button on shop page and set dynamic relinking to some other page. So I made a checkbox at the product page and if that checkbox is enabled then the custom button with different link will be visible to that product. here is the code for checkbox:

add_action( 'add_meta_boxes', 'wdm_add_meta_box' );
add_action( 'add_meta_boxes', 'wdm_add_customize_enable_metabox' );
function wdm_add_customize_enable_metabox() {
    add_meta_box(
        'Checkbox_metabox',
        'Check if you want to enable customization button',
        'wdm_enable_callback',
        'product',
        'normal',
        'high'
    );
}

function wdm_enable_callback( $product ) {
    $custom = get_post_custom( $product -> ID );
    if( isset($custom[ "_wcm_custom_design_checkbox" ][0] ) ) {
        $meta_box_check = $custom[ "_wcm_custom_design_checkbox" ][0];
    }
    else {
        $meta_box_check = FALSE;
    }
    ?>
    <tr>
        <td>Enable design panel at frontend?</td>
        <td><input type="checkbox" name="wcm_enable_checkbox" id="wcm_enable_checkbox" <?php if ( $meta_box_check == true ) { ?> checked="checked"<?php } ?> /></td>
    </tr>
    <tr>
    <?php
}

add_action( 'save_post', 'wdm_save_meta_check_box_data', 10,2 );
function wdm_save_meta_check_box_data( $post_id, $product ){
    if ( $product -> post_type == 'product' ) {
        if ( isset($_POST["wcm_enable_checkbox"] ) && $_POST["wcm_enable_checkbox"] ) {
            update_post_meta( $post_id, '_wcm_custom_design_checkbox', $_POST["wcm_enable_checkbox"] );
        }
        else {
            update_post_meta( $post_id, '_wcm_custom_design_checkbox', '');
        }
    }
}

The above code is working fine. I used this code into theme add_to_cart loop and here my only problem is how to get custom add_to_cart_url() for custom links and how to change the add_to_cart_text() text to whatever I want.

Here are the changes I made to add_to_cart.php:

global $product, $post;
$hasCustomization =  get_post_meta( $post -> ID, '_wcm_custom_design_checkbox', true );

if ( $hasCustomization == 'on' ) {
    echo apply_filters(
        'woocommerce_loop_add_to_cart_link',
        sprintf(
            '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>',
            esc_url( $product -> add_to_cart_url() ),//Here i want my own urls
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            $product->is_purchasable() ? '' : '',//own css for custom text to show
            esc_attr( $product->product_type ),
            esc_html( $product->add_to_cart_text() )// Custom Text
        ),
        $product
    );
}

else{
    echo apply_filters(
        'woocommerce_loop_add_to_cart_link',
        sprintf(
            '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>',
            esc_url( $product -> add_to_cart_url() ),
            esc_attr( $product -> id ),
            esc_attr( $product -> get_sku() ),
            $product -> is_purchasable() ? 'add_to_cart_button' : '',
            esc_attr( $product -> product_type ),
            esc_html( $product -> add_to_cart_text() )
        ),
        $product
    );
}
?>
War es hilfreich?

Lösung

Per Ashish's answer that's been posted, below is the update code for clarification with the correct answer by switching add_to_cart_url() to get_permalink():

global $product, $post;
$hasCustomization =  get_post_meta( $post -> ID, '_wcm_custom_design_checkbox', true );

if ( $hasCustomization == 'on' ) {
    echo apply_filters(
        'woocommerce_loop_add_to_cart_link',
        sprintf(
            '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>',
            esc_url( $product -> get_permalink() ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            $product->is_purchasable() ? '' : '',to show
            esc_attr( $product->product_type ),
            esc_html( $product->add_to_cart_text() )// Custom Text
        ),
        $product
    );
}

else{
    echo apply_filters(
        'woocommerce_loop_add_to_cart_link',
        sprintf(
            '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>',
            esc_url( $product -> add_to_cart_url() ),
            esc_attr( $product -> id ),
            esc_attr( $product -> get_sku() ),
            $product -> is_purchasable() ? 'add_to_cart_button' : '',
            esc_attr( $product -> product_type ),
            esc_html( $product -> add_to_cart_text() )
        ),
        $product
    );
}
?>

Andere Tipps

Got it to work with just changing "add_to_cart_url()" to "get_permalink()" inside if statement. By doing this it gets the page url and links it to custom button. Thankx all for support :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top