• Resolved cutu234

    (@cutu234)


    I’ve just created a PHP test and want it to run only on Woocommerce product pages. The variant shows price and related information just above the add-to-cart button. The conversion goal is, of course, a purchase.

    Now, I have some problems understanding the test scope setting. I would choose “Run on ‘wp'”, but how exactly whould I use the function field? Would it be like so?

    if (is_product()) {
        return true;
    }

    Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter cutu234

    (@cutu234)

    Just to give you more context. Here’s the code for the test variant:

    add_action('woocommerce_single_product_summary', function () {
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_product_units', 10.1);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_price_unit', 10.2);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_shipping_costs_info', 10.3);
    }, 5);

    The only difference to the original is the position that changed from 29 to 10. The remove_action is not really needed here, but I wanted to keep the code as consistent as possible.

    Plugin Author David Aguilera

    (@davilera)

    Your scope function always has to return something:

    if ( is_product() ) {
    return true;
    }
    return false;

    But it’s actually way easier this way:

    return is_product();
    Thread Starter cutu234

    (@cutu234)

    Thanks, David.

    It works, but the hook is now duplicated. Do I need some additional condition in the functions.php for the original code?

    add_action('woocommerce_single_product_summary', function () {
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 29);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_product_units', 29.1);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_price_unit', 29.2);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_shipping_costs_info', 29.3);
        }, 5);
    Thread Starter cutu234

    (@cutu234)

    I could use something like this for the original code:

    if ( isset($_GET['nab']) && $_GET['nab'] == '0' ) {...

    Not sure whether this is safe to use. The price is crucial information. This has to be super robust. 🙂

    Plugin Author David Aguilera

    (@davilera)

    How is it duplicated?

    Plugin Author David Aguilera

    (@davilera)

    Please open a ticket and we’ll look at your site directly.

    Thread Starter cutu234

    (@cutu234)

    Will do.

    Thread Starter cutu234

    (@cutu234)

    David helped me with a robust solution that keeps all modifications inside the test configuration. This was a bit tricky in this specific case, since several plugins have some impact on the product page. This is why I had to use some code to place all pricing information right above the add-to-cart button. So, this is not your typical Woocommerce setup.

    I use the following code in the B variant:

    <?php
    add_action('wp_head', function() {
    echo '<style>
    .woocommerce-product-details__short-description ~ .price,
    .woocommerce-product-details__short-description ~ .product-units,
    .woocommerce-product-details__short-description ~ .wc-gzd-additional-info {display: none !important;}
    .inquiry {margin-bottom: 1.5em;}</style>';
    }, 9999);
    add_action('woocommerce_single_product_summary', function () {
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 29);
        add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_product_units', 10.1);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_price_unit', 10.2);
        add_action('woocommerce_single_product_summary', 'woocommerce_gzd_template_single_shipping_costs_info', 10.3);
    }, 5);

    This removes the duplicate and moves the pricing section to the top. Please note that most hooks used here are specific for the Germanized plugin.

    • This reply was modified 3 weeks, 6 days ago by cutu234.
    • This reply was modified 3 weeks, 6 days ago by cutu234.
    Plugin Author David Aguilera

    (@davilera)

    Thanks for sharing your solution with everyone!

Viewing 9 replies - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.